oncall-engine/engine/apps/grafana_plugin/views/status_v2.py
Matias Bordese 35f23cdcc6
Rework organization sync and grafana plugin engine backend (#4756)
Related to
https://github.com/grafana/oncall-private/issues/2806#issuecomment-2246286918.

Prepare engine for the backend plugin enablement/migration:

 - Refactor sync code
- Improve plugin user authentication to set up user on-the-fly (when
missing)
- Implement v2 endpoints for install, sync and status (to be used via
the backend plugin)

(most of the changes come from
https://github.com/grafana/oncall/pull/4657; backport all engine changes
that keep backwards compatibility)
2024-07-31 16:12:56 +00:00

52 lines
2 KiB
Python

import logging
from django.conf import settings
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.grafana_plugin.helpers import GrafanaAPIClient
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
from common.api_helpers.mixins import GrafanaHeadersMixin
from common.api_helpers.utils import create_engine_url
logger = logging.getLogger(__name__)
class StatusV2View(GrafanaHeadersMixin, APIView):
authentication_classes = (
MobileAppAuthTokenAuthentication,
BasePluginAuthentication,
)
def get(self, request: Request) -> Response:
# Check if the plugin is currently undergoing maintenance, and return response without querying db
if settings.CURRENTLY_UNDERGOING_MAINTENANCE_MESSAGE:
return Response(
data={
"currently_undergoing_maintenance_message": settings.CURRENTLY_UNDERGOING_MAINTENANCE_MESSAGE,
}
)
organization = request.auth.organization
api_url = create_engine_url("")
# If /status is called frequently this can be skipped with a cache
grafana_api_client = GrafanaAPIClient(api_url=organization.grafana_url, api_token=organization.api_token)
_, call_status = grafana_api_client.check_token()
return Response(
data={
"connection_to_grafana": {
"url": call_status["url"],
"connected": call_status["connected"],
"status_code": call_status["status_code"],
"message": call_status["message"],
},
"license": settings.LICENSE,
"version": settings.VERSION,
"currently_undergoing_maintenance_message": settings.CURRENTLY_UNDERGOING_MAINTENANCE_MESSAGE,
"api_url": api_url,
}
)