|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from django.db import transaction |
| 4 | +from django.utils import timezone |
| 5 | +from rest_framework import serializers |
| 6 | + |
| 7 | +from sentry import features |
| 8 | +from sentry.api.authentication import DSNAuthentication |
| 9 | +from sentry.api.base import Endpoint |
| 10 | +from sentry.api.exceptions import ResourceDoesNotExist |
| 11 | +from sentry.api.bases.project import ProjectPermission |
| 12 | +from sentry.api.serializers import serialize |
| 13 | +from sentry.models import Monitor, MonitorCheckIn, CheckInStatus, MonitorStatus, Project, ProjectKey, ProjectStatus |
| 14 | +from sentry.utils.sdk import configure_scope |
| 15 | + |
| 16 | + |
| 17 | +class CheckInSerializer(serializers.Serializer): |
| 18 | + status = serializers.ChoiceField( |
| 19 | + choices=( |
| 20 | + ('ok', CheckInStatus.OK), |
| 21 | + ('error', CheckInStatus.ERROR), |
| 22 | + ('in_progress', CheckInStatus.IN_PROGRESS), |
| 23 | + ), |
| 24 | + ) |
| 25 | + duration = serializers.IntegerField(required=False) |
| 26 | + |
| 27 | + |
| 28 | +class MonitorCheckInDetailsEndpoint(Endpoint): |
| 29 | + authentication_classes = Endpoint.authentication_classes + (DSNAuthentication,) |
| 30 | + permission_classes = (ProjectPermission,) |
| 31 | + |
| 32 | + # TODO(dcramer): this code needs shared with other endpoints as its security focused |
| 33 | + # TODO(dcramer): this doesnt handle is_global roles |
| 34 | + def convert_args(self, request, monitor_id, checkin_id, *args, **kwargs): |
| 35 | + try: |
| 36 | + monitor = Monitor.objects.get( |
| 37 | + guid=monitor_id, |
| 38 | + ) |
| 39 | + except Monitor.DoesNotExist: |
| 40 | + raise ResourceDoesNotExist |
| 41 | + |
| 42 | + project = Project.objects.get_from_cache(id=monitor.project_id) |
| 43 | + if project.status != ProjectStatus.VISIBLE: |
| 44 | + raise ResourceDoesNotExist |
| 45 | + |
| 46 | + if hasattr(request.auth, 'project_id') and project.id != request.auth.project_id: |
| 47 | + return self.respond(status=400) |
| 48 | + |
| 49 | + if not features.has('organizations:monitors', |
| 50 | + project.organization, actor=request.user): |
| 51 | + raise ResourceDoesNotExist |
| 52 | + |
| 53 | + self.check_object_permissions(request, project) |
| 54 | + |
| 55 | + with configure_scope() as scope: |
| 56 | + scope.set_tag("organization", project.organization_id) |
| 57 | + scope.set_tag("project", project.id) |
| 58 | + |
| 59 | + try: |
| 60 | + checkin = MonitorCheckIn.objects.get( |
| 61 | + monitor=monitor, |
| 62 | + guid=checkin_id, |
| 63 | + ) |
| 64 | + except MonitorCheckIn.DoesNotExist: |
| 65 | + raise ResourceDoesNotExist |
| 66 | + |
| 67 | + request._request.organization = project.organization |
| 68 | + |
| 69 | + kwargs.update({ |
| 70 | + 'checkin': checkin, |
| 71 | + 'monitor': monitor, |
| 72 | + 'project': project, |
| 73 | + }) |
| 74 | + return (args, kwargs) |
| 75 | + |
| 76 | + def get(self, request, project, monitor, checkin): |
| 77 | + """ |
| 78 | + Retrieve a check-in |
| 79 | + `````````````````` |
| 80 | +
|
| 81 | + :pparam string monitor_id: the id of the monitor. |
| 82 | + :pparam string checkin_id: the id of the check-in. |
| 83 | + :auth: required |
| 84 | + """ |
| 85 | + # we dont allow read permission with DSNs |
| 86 | + if isinstance(request.auth, ProjectKey): |
| 87 | + return self.respond(status=401) |
| 88 | + |
| 89 | + return self.respond(serialize(checkin, request.user)) |
| 90 | + |
| 91 | + def put(self, request, project, monitor, checkin): |
| 92 | + """ |
| 93 | + Update a check-in |
| 94 | + ````````````````` |
| 95 | +
|
| 96 | + :pparam string monitor_id: the id of the monitor. |
| 97 | + :pparam string checkin_id: the id of the check-in. |
| 98 | + :auth: required |
| 99 | + """ |
| 100 | + if checkin.status in CheckInStatus.FINISHED_VALUES: |
| 101 | + return self.respond(status=400) |
| 102 | + |
| 103 | + serializer = CheckInSerializer( |
| 104 | + data=request.DATA, |
| 105 | + partial=True, |
| 106 | + context={ |
| 107 | + 'project': project, |
| 108 | + 'request': request, |
| 109 | + }, |
| 110 | + ) |
| 111 | + if not serializer.is_valid(): |
| 112 | + return self.respond(serializer.errors, status=400) |
| 113 | + |
| 114 | + result = serializer.object |
| 115 | + |
| 116 | + current_datetime = timezone.now() |
| 117 | + params = { |
| 118 | + 'date_updated': current_datetime, |
| 119 | + } |
| 120 | + if 'duration' in result: |
| 121 | + params['duration'] = result['duration'] |
| 122 | + if 'status' in result: |
| 123 | + params['status'] = getattr(CheckInStatus, result['status'].upper()) |
| 124 | + |
| 125 | + with transaction.atomic(): |
| 126 | + checkin.update(**params) |
| 127 | + if checkin.status == CheckInStatus.ERROR: |
| 128 | + monitor.mark_failed(current_datetime) |
| 129 | + else: |
| 130 | + monitor_params = { |
| 131 | + 'last_checkin': current_datetime, |
| 132 | + 'next_checkin': monitor.get_next_scheduled_checkin(current_datetime), |
| 133 | + } |
| 134 | + if checkin.status == CheckInStatus.OK: |
| 135 | + monitor_params['status'] = MonitorStatus.OK |
| 136 | + Monitor.objects.filter( |
| 137 | + id=monitor.id, |
| 138 | + ).exclude( |
| 139 | + last_checkin__gt=current_datetime, |
| 140 | + ).update(**monitor_params) |
| 141 | + |
| 142 | + return self.respond(serialize(checkin, request.user)) |
0 commit comments