Skip to content

Commit

Permalink
Refactor plugin sync (#1200)
Browse files Browse the repository at this point in the history
# What this PR does

This PR adds a shortcut in the plugin synchronisation process, so the
existing users will be able login without waiting for the sync task.
Every request still starts the background synchronisation task, to be
able to propagate the organisation changes faster than periodic task. It
means that we don't necessarily need "force reload" button in the
interface.
For all the other cases (user does not exist, organisation token "not
ok", etc) process remains same - plugin will show "Initialising
plugin..." until the background task in successfully completed

Co-authored-by: Joey Orlando <joey.orlando@grafana.com>
  • Loading branch information
iskhakov and joeyorlando authored Jan 25, 2023
1 parent cf1a1cd commit 1fc3f6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Removed duplicate API call, in the UI on plugin initial load, to `GET /api/internal/v1/alert_receive_channels`
- Increased plugin startup speed ([#1200](https://github.com/grafana/oncall/pull/1200))

## v1.1.18 (2023-01-18)

Expand Down
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
26 changes: 15 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,29 @@ 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"])
user_is_present_in_org = PluginAuthentication.is_user_from_request_present_in_organization(
request, organization
)
if not user_is_present_in_org:
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

0 comments on commit 1fc3f6d

Please sign in to comment.