Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor plugin sync #1200

Merged
merged 10 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions engine/apps/auth_token/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def _get_user(request: Request, organization: Organization) -> User:
logger.debug(f"Could not get user from grafana request. Context {context}")
raise exceptions.AuthenticationFailed("Non-existent or anonymous user.")

@classmethod
def is_user_from_request_present_in_organization(cls, request: Request, organization: Organization) -> User:
try:
cls._get_user(request, organization)
return True
except exceptions.AuthenticationFailed:
return False


class GrafanaIncidentUser(AnonymousUser):
@property
Expand Down
24 changes: 13 additions & 11 deletions engine/apps/grafana_plugin/views/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.auth_token.auth import PluginAuthentication
from apps.grafana_plugin.permissions import PluginTokenVerified
from apps.grafana_plugin.tasks.sync import plugin_sync_organization_async
from apps.user_management.models import Organization
Expand All @@ -22,26 +23,27 @@ def post(self, request: Request) -> Response:
stack_id = self.instance_context["stack_id"]
org_id = self.instance_context["org_id"]
is_installed = False

allow_signup = True
try:
organization = Organization.objects.get(stack_id=stack_id, org_id=org_id)

if organization.api_token_status == Organization.API_TOKEN_STATUS_OK:
is_installed = True
organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING

organization.save(update_fields=["api_token_status"])
is_user = PluginAuthentication.is_user_from_request_present_in_organization(request, organization)
if not is_user:
iskhakov marked this conversation as resolved.
Show resolved Hide resolved
organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING
organization.save(update_fields=["api_token_status"])

if not organization:
DynamicSetting = apps.get_model("base", "DynamicSetting")
allow_signup = DynamicSetting.objects.get_or_create(
name="allow_plugin_organization_signup", defaults={"boolean_value": True}
)[0].boolean_value

plugin_sync_organization_async.apply_async((organization.pk,))
except Organization.DoesNotExist:
logger.info(f"Organization for stack {stack_id} org {org_id} was not found")

allow_signup = True
if not organization:
DynamicSetting = apps.get_model("base", "DynamicSetting")
allow_signup = DynamicSetting.objects.get_or_create(
name="allow_plugin_organization_signup", defaults={"boolean_value": True}
)[0].boolean_value

return Response(
status=status.HTTP_202_ACCEPTED,
data={
Expand Down