Esempio n. 1
0
    def post(self, request, project):
        """
        Register a new Service Hook
        ```````````````````````````

        Register a new service hook on a project.

        Events include:

        - event.alert: An alert is generated for an event (via rules).
        - event.created: A new event has been processed.

        This endpoint requires the 'servicehooks' feature to
        be enabled for your project.

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        :auth: required
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        if not self.has_feature(request, project):
            return self.respond(
                {
                    "error_type": "unavailable_feature",
                    "detail": ["You do not have that feature enabled"],
                },
                status=403,
            )

        validator = ServiceHookValidator(data=request.data)
        if not validator.is_valid():
            return self.respond(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        result = validator.validated_data

        with transaction.atomic():
            hook = service_hooks.Creator.run(
                projects=[project],
                organization=project.organization,
                url=result["url"],
                actor=request.user,
                events=result.get("events"),
                application=getattr(request.auth, "application", None) if request.auth else None,
            )

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_ADD,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user), status=201)
    def put(self, request, project, hook_id):
        """
        Update a Service Hook
        `````````````````````

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :pparam string hook_id: the guid of the service hook.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        :auth: required
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        try:
            hook = ServiceHook.objects.get(
                project_id=project.id,
                guid=hook_id,
            )
        except ServiceHook.DoesNotExist:
            raise ResourceDoesNotExist

        validator = ServiceHookValidator(data=request.data, partial=True)
        if not validator.is_valid():
            return self.respond(validator.errors,
                                status=status.HTTP_400_BAD_REQUEST)

        result = validator.validated_data

        updates = {}
        if result.get('events') is not None:
            updates['events'] = result['events']
        if result.get('url'):
            updates['url'] = result['url']
        if result.get('version') is not None:
            updates['version'] = result['version']
        if result.get('isActive') is True:
            updates['status'] = ObjectStatus.ACTIVE
        elif result.get('isActive') is False:
            updates['status'] = ObjectStatus.DISABLED

        with transaction.atomic():
            hook.update(**updates)

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_EDIT,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user))
Esempio n. 3
0
    def post(self, request, project):
        """
        Register a new Service Hook
        ```````````````````````````

        Register a new service hook on a project.

        Events include:

        - event.alert: An alert is generated for an event (via rules).
        - event.created: A new event has been processed.

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        if not self.has_feature(request, project):
            return self.respond(
                {
                    'error_type': 'unavailable_feature',
                    'detail': ['You do not have that feature enabled']
                },
                status=403)

        validator = ServiceHookValidator(data=request.DATA)
        if not validator.is_valid():
            return self.respond(validator.errors,
                                status=status.HTTP_400_BAD_REQUEST)

        result = validator.object

        with transaction.atomic():
            hook = ServiceHook.objects.create(
                project_id=project.id,
                url=result['url'],
                actor_id=request.user.id,
                events=result.get('events'),
                application=getattr(request.auth, 'application', None)
                if request.auth else None,
            )

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_ADD,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user), status=201)
    def put(self, request, project, hook_id):
        """
        Update a Service Hook
        `````````````````````

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :pparam string hook_id: the guid of the service hook.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        :auth: required
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        try:
            hook = ServiceHook.objects.get(
                project_id=project.id,
                guid=hook_id,
            )
        except ServiceHook.DoesNotExist:
            raise ResourceDoesNotExist

        validator = ServiceHookValidator(data=request.DATA, partial=True)
        if not validator.is_valid():
            return self.respond(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        result = validator.object

        updates = {}
        if result.get('events') is not None:
            updates['events'] = result['events']
        if result.get('url'):
            updates['url'] = result['url']
        if result.get('version') is not None:
            updates['version'] = result['version']
        if result.get('isActive') is True:
            updates['status'] = ObjectStatus.ACTIVE
        elif result.get('isActive') is False:
            updates['status'] = ObjectStatus.DISABLED

        with transaction.atomic():
            hook.update(**updates)

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_EDIT,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user))
Esempio n. 5
0
    def post(self, request, project):
        """
        Register a new Service Hook
        ```````````````````````````

        Register a new service hook on a project.

        Events include:

        - event.alert: An alert is generated for an event (via rules).
        - event.created: A new event has been processed.

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        if not self.has_feature(request, project):
            return self.respond({
                'error_type': 'unavailable_feature',
                'detail': ['You do not have that feature enabled']
            }, status=403)

        validator = ServiceHookValidator(data=request.DATA)
        if not validator.is_valid():
            return self.respond(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        result = validator.object

        with transaction.atomic():
            hook = ServiceHook.objects.create(
                project_id=project.id,
                url=result['url'],
                actor_id=request.user.id,
                events=result.get('events'),
                application=getattr(request.auth, 'application', None) if request.auth else None,
            )

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_ADD,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user), status=201)
Esempio n. 6
0
    def post(self, request, project):
        """
        Register a new Service Hook
        ```````````````````````````

        Create a new client key bound to a project.  The key's secret and
        public key are generated by the server.

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        validator = ServiceHookValidator(data=request.DATA)
        if not validator.is_valid():
            return self.respond(validator.errors,
                                status=status.HTTP_400_BAD_REQUEST)

        result = validator.object

        with transaction.atomic():
            hook = ServiceHook.objects.create(
                project_id=project.id,
                url=result['url'],
                actor_id=request.user.id,
                events=result.get('events'),
                application=getattr(request.auth, 'application', None)
                if request.auth else None,
            )

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_ADD,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user), status=201)
Esempio n. 7
0
    def post(self, request, project):
        """
        Register a new Service Hook
        ```````````````````````````

        Create a new client key bound to a project.  The key's secret and
        public key are generated by the server.

        :pparam string organization_slug: the slug of the organization the
                                          client keys belong to.
        :pparam string project_slug: the slug of the project the client keys
                                     belong to.
        :param string url: the url for the webhook
        :param array[string] events: the events to subscribe to
        """
        if not request.user.is_authenticated():
            return self.respond(status=401)

        validator = ServiceHookValidator(data=request.DATA)
        if not validator.is_valid():
            return self.respond(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        result = validator.object

        with transaction.atomic():
            hook = ServiceHook.objects.create(
                project_id=project.id,
                url=result['url'],
                actor_id=request.user.id,
                events=result.get('events'),
                application=getattr(request.auth, 'application', None) if request.auth else None,
            )

            self.create_audit_entry(
                request=request,
                organization=project.organization,
                target_object=hook.id,
                event=AuditLogEntryEvent.SERVICEHOOK_ADD,
                data=hook.get_audit_log_data(),
            )

        return self.respond(serialize(hook, request.user), status=201)