oncall-engine/engine/apps/grafana_plugin/views/install_v2.py
Michael Derynck 5cf382a2e3
Fix error encoding on install_v2 endpoint (#5133)
# What this PR does
Fixes incorrect encoding when SyncException contains one of the
predefined dataclass errors in install_v2 endpoint.

## Which issue(s) this PR closes

Related to #5124 

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-10-07 20:08:53 +00:00

34 lines
1.1 KiB
Python

import logging
from dataclasses import asdict, is_dataclass
from django.conf import settings
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from apps.grafana_plugin.views.sync_v2 import SyncException, SyncV2View
from common.api_helpers.errors import SELF_HOSTED_ONLY_FEATURE_ERROR
logger = logging.getLogger(__name__)
class InstallV2View(SyncV2View):
authentication_classes = ()
permission_classes = ()
def post(self, request: Request) -> Response:
if settings.LICENSE != settings.OPEN_SOURCE_LICENSE_NAME:
return Response(data=asdict(SELF_HOSTED_ONLY_FEATURE_ERROR), status=status.HTTP_403_FORBIDDEN)
try:
organization = self.do_sync(request)
except SyncException as e:
return Response(
data=asdict(e.error_data) if is_dataclass(e.error_data) else e.error_data,
status=status.HTTP_400_BAD_REQUEST,
)
organization.revoke_plugin()
provisioned_data = organization.provision_plugin()
return Response(data=provisioned_data, status=status.HTTP_200_OK)