2024-01-23 15:59:33 -07:00
|
|
|
import logging
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.request import Request
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
2023-08-22 11:17:26 -06:00
|
|
|
from apps.auth_token.auth import BasePluginAuthentication
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.user_management.models import Organization
|
|
|
|
|
from apps.user_management.sync import sync_organization
|
|
|
|
|
from common.api_helpers.mixins import GrafanaHeadersMixin
|
|
|
|
|
|
2024-01-23 15:59:33 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
class InstallView(GrafanaHeadersMixin, APIView):
|
2023-08-22 11:17:26 -06:00
|
|
|
authentication_classes = (BasePluginAuthentication,)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def post(self, request: Request) -> Response:
|
|
|
|
|
stack_id = self.instance_context["stack_id"]
|
|
|
|
|
org_id = self.instance_context["org_id"]
|
|
|
|
|
|
2023-01-05 12:42:55 +08:00
|
|
|
organization = Organization.objects_with_deleted.filter(stack_id=stack_id, org_id=org_id).first()
|
|
|
|
|
# If we receive install request to the deleted org - just restore it.
|
|
|
|
|
organization.deleted_at = None
|
2022-06-03 08:09:47 -06:00
|
|
|
organization.api_token = self.instance_context["grafana_token"]
|
2023-01-05 12:42:55 +08:00
|
|
|
organization.save(update_fields=["api_token", "deleted_at"])
|
2024-01-23 15:59:33 -07:00
|
|
|
logger.info(f"install - grafana_token replaced org={organization.pk}")
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
sync_organization(organization)
|
2024-01-23 15:59:33 -07:00
|
|
|
logger.info(
|
|
|
|
|
f"install - sync organization finished org={organization.pk} "
|
|
|
|
|
f"token_status={organization.api_token_status}"
|
|
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|