* Get rid of installation token (for OSS installations) This is done by being required to supply the grafana API URL as an environment variable on the backend. Additionally, optionally an OnCall API URL environment variable can be passed in to the frontend (this basically allows completely skipping the need to configure anything). - deduplicated a lot of the sync logic on the frontend + made error message more useful and consistent - Split PluginConfigPage component into several subcomponents (making it easier to test each individual component) - Moved RootWithLoader (from plugin/GrafanaPluginRootPage) into its own subcomponent (making it easier to test) - Added tests for pre-existing components that were touched: - PluginConfigPage component (and its new subcomponents) - state/plugin and state/rootBaseStore functions - apps.grafana_plugin django app Helm changes: - add GRAFANA_API_URL to oncall.env - some yaml autoformatting changes - remove reference to python manage.py issue_invite_for_the_frontend --override Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
75 lines
2.8 KiB
Python
75 lines
2.8 KiB
Python
import logging
|
|
|
|
from django.apps import apps
|
|
from django.conf import settings
|
|
from rest_framework import status
|
|
from rest_framework.request import Request
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
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
|
|
from common.api_helpers.mixins import GrafanaHeadersMixin
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PluginSyncView(GrafanaHeadersMixin, APIView):
|
|
permission_classes = (PluginTokenVerified,)
|
|
|
|
def post(self, request: Request) -> Response:
|
|
stack_id = self.instance_context["stack_id"]
|
|
org_id = self.instance_context["org_id"]
|
|
is_installed = False
|
|
|
|
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"])
|
|
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={
|
|
"is_installed": is_installed,
|
|
"is_user_anonymous": self.grafana_context["IsAnonymous"],
|
|
"allow_signup": allow_signup,
|
|
},
|
|
)
|
|
|
|
def get(self, _request: Request) -> Response:
|
|
stack_id = self.instance_context["stack_id"]
|
|
org_id = self.instance_context["org_id"]
|
|
token_ok = False
|
|
|
|
try:
|
|
organization = Organization.objects.get(stack_id=stack_id, org_id=org_id)
|
|
if organization.api_token_status == Organization.API_TOKEN_STATUS_PENDING:
|
|
return Response(status=status.HTTP_202_ACCEPTED)
|
|
elif organization.api_token_status == Organization.API_TOKEN_STATUS_OK:
|
|
token_ok = True
|
|
except Organization.DoesNotExist:
|
|
logger.info(f"Organization for stack {stack_id} org {org_id} was not found")
|
|
|
|
return Response(
|
|
status=status.HTTP_200_OK,
|
|
data={
|
|
"token_ok": token_ok,
|
|
"license": settings.LICENSE,
|
|
"version": settings.VERSION,
|
|
},
|
|
)
|