Пример #1
0
 def to_objectchange(self, action):
     # Annotate the parent VirtualMachine
     return ObjectChange(changed_object=self,
                         object_repr=str(self),
                         action=action,
                         related_object=self.virtual_machine,
                         object_data=serialize_object(self))
Пример #2
0
 def snapshot(self):
     """
     Save a snapshot of the object's current state in preparation for modification.
     """
     logger = logging.getLogger('netbox')
     logger.debug(f"Taking a snapshot of {self}")
     self._prechange_snapshot = serialize_object(self)
Пример #3
0
 def to_objectchange(self, action):
     # Annotate the assigned object, if any
     return ObjectChange(changed_object=self,
                         object_repr=str(self),
                         action=action,
                         related_object=self.assigned_object,
                         object_data=serialize_object(self))
Пример #4
0
 def to_objectchange(self, action):
     # Remove MPTT-internal fields
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         object_data=serialize_object(self, exclude=['level', 'lft', 'rght', 'tree_id'])
     )
Пример #5
0
 def to_objectchange(self, action):
     """
     Return a new ObjectChange representing a change made to this object. This will typically be called automatically
     by extras.middleware.ChangeLoggingMiddleware.
     """
     return ObjectChange(changed_object=self,
                         object_repr=str(self),
                         action=action,
                         object_data=serialize_object(self))
Пример #6
0
 def log_change(self, user, request_id, action):
     """
     Create a new ObjectChange representing a change made to this object. This will typically be called automatically
     by extras.middleware.ChangeLoggingMiddleware.
     """
     ObjectChange(user=user,
                  request_id=request_id,
                  changed_object=self,
                  action=action,
                  object_data=serialize_object(self)).save()
Пример #7
0
 def log_change(self, user, request_id, action):
     """
     Reference the parent circuit when recording the change.
     """
     ObjectChange(user=user,
                  request_id=request_id,
                  changed_object=self,
                  related_object=self.circuit,
                  action=action,
                  object_data=serialize_object(self)).save()
Пример #8
0
 def log_change(self, user, request_id, action):
     """
     Reference the parent circuit when recording the change.
     """
     ObjectChange(
         user=user,
         request_id=request_id,
         changed_object=self,
         related_object=self.circuit,
         action=action,
         object_data=serialize_object(self)
     ).save()
Пример #9
0
def enqueue_webhooks(instance, user, request_id, action):
    """
    Find Webhook(s) assigned to this instance + action and enqueue them
    to be processed
    """
    # Determine whether this type of object supports webhooks
    app_label = instance._meta.app_label
    model_name = instance._meta.model_name
    if model_name not in registry['model_features']['webhooks'].get(
            app_label, []):
        return

    # Retrieve any applicable Webhooks
    content_type = ContentType.objects.get_for_model(instance)
    action_flag = {
        ObjectChangeActionChoices.ACTION_CREATE: 'type_create',
        ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
        ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
    }[action]
    webhooks = Webhook.objects.filter(content_types=content_type,
                                      enabled=True,
                                      **{action_flag: True})

    if webhooks.exists():

        # Get the Model's API serializer class and serialize the object
        serializer_class = get_serializer_for_model(instance.__class__)
        serializer_context = {
            'request': None,
        }
        serializer = serializer_class(instance, context=serializer_context)

        # Gather pre- and post-change snapshots
        snapshots = {
            'prechange':
            getattr(instance, '_prechange_snapshot', None),
            'postchange':
            serialize_object(instance)
            if action != ObjectChangeActionChoices.ACTION_DELETE else None,
        }

        # Enqueue the webhooks
        webhook_queue = get_queue('default')
        for webhook in webhooks:
            webhook_queue.enqueue("extras.webhooks_worker.process_webhook",
                                  webhook=webhook,
                                  model_name=instance._meta.model_name,
                                  event=action,
                                  data=serializer.data,
                                  snapshots=snapshots,
                                  timestamp=str(timezone.now()),
                                  username=user.username,
                                  request_id=request_id)
Пример #10
0
 def log_change(self, user, request_id, action):
     """
     Create a new ObjectChange representing a change made to this object. This will typically be called automatically
     by extras.middleware.ChangeLoggingMiddleware.
     """
     ObjectChange(
         user=user,
         request_id=request_id,
         changed_object=self,
         action=action,
         object_data=serialize_object(self)
     ).save()
Пример #11
0
    def to_objectchange(self, action):
        # Annotate the parent Device/VM
        try:
            parent_obj = self.device or self.virtual_machine
        except ObjectDoesNotExist:
            parent_obj = None

        return ObjectChange(changed_object=self,
                            object_repr=str(self),
                            action=action,
                            related_object=parent_obj,
                            object_data=serialize_object(self))
Пример #12
0
 def to_objectchange(self, action):
     # Annotate the parent Device
     try:
         device = self.device
     except ObjectDoesNotExist:
         # The parent Device has already been deleted
         device = None
     return ObjectChange(changed_object=self,
                         object_repr=str(self),
                         action=action,
                         related_object=device,
                         object_data=serialize_object(self))
Пример #13
0
    def to_objectchange(self, action):
        # Annotate the assigned Interface (if any)
        try:
            parent_obj = self.interface
        except ObjectDoesNotExist:
            parent_obj = None

        return ObjectChange(changed_object=self,
                            object_repr=str(self),
                            action=action,
                            related_object=parent_obj,
                            object_data=serialize_object(self))
Пример #14
0
    def to_objectchange(self, action):
        # Annotate the parent Circuit
        try:
            related_object = self.circuit
        except Circuit.DoesNotExist:
            # Parent circuit has been deleted
            related_object = None

        return ObjectChange(changed_object=self,
                            object_repr=str(self),
                            action=action,
                            related_object=related_object,
                            object_data=serialize_object(self))
Пример #15
0
    def to_objectchange(self, action):
        # Annotate the parent Device/VM
        try:
            parent = getattr(self, 'device', None) or getattr(
                self, 'virtual_machine', None)
        except ObjectDoesNotExist:
            # The parent device/VM has already been deleted
            parent = None

        return ObjectChange(changed_object=self,
                            object_repr=str(self),
                            action=action,
                            related_object=parent,
                            object_data=serialize_object(self))
Пример #16
0
 def log_change(self, user, request_id, action):
     """
     Reference the parent circuit when recording the change.
     """
     try:
         related_object = self.circuit
     except Circuit.DoesNotExist:
         # Parent circuit has been deleted
         related_object = None
     ObjectChange(user=user,
                  request_id=request_id,
                  changed_object=self,
                  related_object=related_object,
                  action=action,
                  object_data=serialize_object(self)).save()
Пример #17
0
    def to_objectchange(self, action, related_object=None):
        """
        Return a new ObjectChange representing a change made to this object. This will typically be called automatically
        by ChangeLoggingMiddleware.
        """
        from extras.models import ObjectChange
        objectchange = ObjectChange(changed_object=self,
                                    related_object=related_object,
                                    object_repr=str(self),
                                    action=action)
        if hasattr(self, '_prechange_snapshot'):
            objectchange.prechange_data = self._prechange_snapshot
        if action in (ObjectChangeActionChoices.ACTION_CREATE,
                      ObjectChangeActionChoices.ACTION_UPDATE):
            objectchange.postchange_data = serialize_object(self)

        return objectchange
Пример #18
0
    def log_change(self, user, request_id, action):
        """
        Include the connected Interface (if any).
        """

        # It's possible that an IPAddress can be deleted _after_ its parent Interface, in which case trying to resolve
        # the interface will raise DoesNotExist.
        try:
            parent_obj = self.interface
        except ObjectDoesNotExist:
            parent_obj = None

        ObjectChange(user=user,
                     request_id=request_id,
                     changed_object=self,
                     related_object=parent_obj,
                     action=action,
                     object_data=serialize_object(self)).save()
Пример #19
0
    def log_change(self, user, request_id, action):
        """
        Include the connected Interface (if any).
        """

        # It's possible that an IPAddress can be deleted _after_ its parent Interface, in which case trying to resolve
        # the interface will raise DoesNotExist.
        try:
            parent_obj = self.interface
        except ObjectDoesNotExist:
            parent_obj = None

        ObjectChange(
            user=user,
            request_id=request_id,
            changed_object=self,
            related_object=parent_obj,
            action=action,
            object_data=serialize_object(self)
        ).save()
Пример #20
0
def get_snapshots(instance, action):
    return {
        'prechange': getattr(instance, '_prechange_snapshot', None),
        'postchange': serialize_object(instance) if action != ObjectChangeActionChoices.ACTION_DELETE else None,
    }
Пример #21
0
 def to_objectchange(self, action):
     return ObjectChange(changed_object=self,
                         object_repr=str(self),
                         action=action,
                         related_object=self.device_type,
                         object_data=serialize_object(self))