2022-06-08 12:07:30 +04:00
|
|
|
from rest_framework import status
|
2022-06-06 16:02:09 +04:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import RBACPermission
|
2022-06-06 16:02:09 +04:00
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
2022-06-13 16:29:08 +04:00
|
|
|
from apps.base.models import LiveSetting
|
2022-06-06 16:02:09 +04:00
|
|
|
from apps.base.utils import live_settings
|
2022-06-13 16:29:08 +04:00
|
|
|
from apps.oss_installation.cloud_heartbeat import get_heartbeat_link
|
2022-06-06 16:02:09 +04:00
|
|
|
from apps.oss_installation.models import CloudConnector, CloudHeartbeat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CloudConnectionView(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
rbac_permissions = {
|
2023-01-23 11:17:57 +00:00
|
|
|
"get": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
2022-11-29 09:41:56 +01:00
|
|
|
"delete": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
|
|
|
}
|
2022-06-06 16:02:09 +04:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
connector = CloudConnector.objects.first()
|
|
|
|
|
heartbeat = CloudHeartbeat.objects.first()
|
|
|
|
|
response = {
|
|
|
|
|
"cloud_connection_status": connector is not None,
|
|
|
|
|
"cloud_notifications_enabled": live_settings.GRAFANA_CLOUD_NOTIFICATIONS_ENABLED,
|
|
|
|
|
"cloud_heartbeat_enabled": live_settings.GRAFANA_CLOUD_ONCALL_HEARTBEAT_ENABLED,
|
2024-10-09 08:55:10 -04:00
|
|
|
"cloud_heartbeat_link": get_heartbeat_link(self.request.auth.organization, connector, heartbeat),
|
2022-06-06 16:02:09 +04:00
|
|
|
"cloud_heartbeat_status": heartbeat is not None and heartbeat.success,
|
|
|
|
|
}
|
|
|
|
|
return Response(response)
|
|
|
|
|
|
2022-06-08 12:07:30 +04:00
|
|
|
def delete(self, request):
|
2022-06-13 16:29:08 +04:00
|
|
|
s = LiveSetting.objects.filter(name="GRAFANA_CLOUD_ONCALL_TOKEN").first()
|
|
|
|
|
if s is not None:
|
|
|
|
|
s.value = None
|
|
|
|
|
s.save()
|
2022-06-08 12:07:30 +04:00
|
|
|
connector = CloudConnector.objects.first()
|
|
|
|
|
if connector is None:
|
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
connector.remove_sync()
|
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|