oncall-engine/engine/apps/grafana_plugin/views/install.py
Michael Derynck 032ced6fd0
Add more logging to plugin sync and install (#3730)
# What this PR does
Add logging to process for syncing OnCall backend with Grafana to help
troubleshoot issues in self-hosted setups.


## Which issue(s) this PR fixes

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2024-01-23 22:59:33 +00:00

35 lines
1.4 KiB
Python

import logging
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.auth_token.auth import BasePluginAuthentication
from apps.user_management.models import Organization
from apps.user_management.sync import sync_organization
from common.api_helpers.mixins import GrafanaHeadersMixin
logger = logging.getLogger(__name__)
class InstallView(GrafanaHeadersMixin, APIView):
authentication_classes = (BasePluginAuthentication,)
def post(self, request: Request) -> Response:
stack_id = self.instance_context["stack_id"]
org_id = self.instance_context["org_id"]
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
organization.api_token = self.instance_context["grafana_token"]
organization.save(update_fields=["api_token", "deleted_at"])
logger.info(f"install - grafana_token replaced org={organization.pk}")
sync_organization(organization)
logger.info(
f"install - sync organization finished org={organization.pk} "
f"token_status={organization.api_token_status}"
)
return Response(status=status.HTTP_204_NO_CONTENT)