|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from django.http import HttpResponseRedirect |
| 6 | +from rest_framework.request import Request |
| 7 | +from rest_framework.response import Response |
| 8 | + |
| 9 | +from sentry import features |
| 10 | +from sentry.api.api_owners import ApiOwner |
| 11 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 12 | +from sentry.api.base import control_silo_endpoint |
| 13 | +from sentry.api.bases.organization import ControlSiloOrganizationEndpoint |
| 14 | +from sentry.api.endpoints.organization_monitoring_provider_index import ( |
| 15 | + MONITORING_PROVIDERS, |
| 16 | + MonitoringProviderPermission, |
| 17 | +) |
| 18 | +from sentry.identity import default_manager as identity_manager |
| 19 | +from sentry.identity.pipeline import IdentityPipeline |
| 20 | +from sentry.organizations.services.organization.model import RpcOrganization |
| 21 | +from sentry.users.models.identity import Identity, IdentityProvider |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +@control_silo_endpoint |
| 27 | +class OrganizationMonitoringProviderDetailsEndpoint(ControlSiloOrganizationEndpoint): |
| 28 | + owner = ApiOwner.CODING_WORKFLOWS |
| 29 | + publish_status = { |
| 30 | + "POST": ApiPublishStatus.PRIVATE, |
| 31 | + "DELETE": ApiPublishStatus.PRIVATE, |
| 32 | + } |
| 33 | + permission_classes = (MonitoringProviderPermission,) |
| 34 | + |
| 35 | + def post( |
| 36 | + self, request: Request, organization: RpcOrganization, provider_key: str, **kwargs: object |
| 37 | + ) -> Response: |
| 38 | + if not features.has("organizations:seer-infra-telemetry", organization, actor=request.user): |
| 39 | + return Response(status=404) |
| 40 | + |
| 41 | + if provider_key not in MONITORING_PROVIDERS: |
| 42 | + return Response({"detail": "Unknown monitoring provider."}, status=400) |
| 43 | + |
| 44 | + provider_type = identity_manager.get(provider_key) |
| 45 | + try: |
| 46 | + config = provider_type.get_pipeline_config(request.data) |
| 47 | + except ValueError as e: |
| 48 | + return Response({"detail": str(e)}, status=400) |
| 49 | + |
| 50 | + idp: IdentityProvider | None = None |
| 51 | + if not provider_type.auto_create_provider_model: |
| 52 | + idp, _ = IdentityProvider.objects.get_or_create(type=provider_key, external_id="") |
| 53 | + |
| 54 | + pipeline = IdentityPipeline( |
| 55 | + request=request._request, |
| 56 | + provider_key=provider_key, |
| 57 | + organization=organization, |
| 58 | + provider_model=idp, |
| 59 | + config=config, |
| 60 | + ) |
| 61 | + pipeline.initialize() |
| 62 | + |
| 63 | + response = pipeline.current_step() |
| 64 | + |
| 65 | + if isinstance(response, HttpResponseRedirect): |
| 66 | + return Response({"redirectUrl": response.url}) |
| 67 | + |
| 68 | + logger.error( |
| 69 | + "monitoring_provider.connect.unexpected_response", |
| 70 | + extra={"provider": provider_key, "response_type": type(response).__name__}, |
| 71 | + ) |
| 72 | + return Response({"detail": "Failed to start OAuth flow."}, status=500) |
| 73 | + |
| 74 | + def delete( |
| 75 | + self, request: Request, organization: RpcOrganization, provider_key: str, **kwargs: object |
| 76 | + ) -> Response: |
| 77 | + if not features.has("organizations:seer-infra-telemetry", organization, actor=request.user): |
| 78 | + return Response(status=404) |
| 79 | + |
| 80 | + if provider_key not in MONITORING_PROVIDERS: |
| 81 | + return Response({"detail": "Unknown monitoring provider."}, status=400) |
| 82 | + |
| 83 | + identities = list( |
| 84 | + Identity.objects.filter( |
| 85 | + idp__type=provider_key, |
| 86 | + user_id=request.user.id, # type: ignore[misc] |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + if not identities: |
| 91 | + return Response({"detail": "Not connected to this provider."}, status=404) |
| 92 | + |
| 93 | + for identity in identities: |
| 94 | + identity.delete() |
| 95 | + |
| 96 | + return Response(status=204) |
0 commit comments