def update_object(self,
                      data: (dict, CmdbObject),
                      user: UserModel = None,
                      permission: AccessControlPermission = None) -> str:

        if isinstance(data, dict):
            update_object = CmdbObject(**data)
        elif isinstance(data, CmdbObject):
            update_object = data
        else:
            raise ObjectManagerUpdateError(
                'Wrong CmdbObject init format - expecting CmdbObject or dict')
        update_object.last_edit_time = datetime.utcnow()
        if user:
            update_object.editor_id = user.public_id

        type_ = self._type_manager.get(update_object.type_id)
        verify_access(type_, user, permission)

        ack = self._update(collection=CmdbObject.COLLECTION,
                           public_id=update_object.get_public_id(),
                           data=update_object.__dict__)
        # create cmdb.core.object.updated event
        if self._event_queue and user:
            event = Event(
                "cmdb.core.object.updated", {
                    "id": update_object.get_public_id(),
                    "type_id": update_object.get_type_id(),
                    "user_id": user.get_public_id()
                })
            self._event_queue.put(event)
        return ack.acknowledged
Beispiel #2
0
    def update_object(self,
                      data: (dict, CmdbObject),
                      user: UserModel = None,
                      permission: AccessControlPermission = None) -> str:

        if isinstance(data, dict):
            update_object = CmdbObject(**data)
        elif isinstance(data, CmdbObject):
            update_object = data
        else:
            raise ObjectManagerUpdateError(
                'Wrong CmdbObject init format - expecting CmdbObject or dict')
        update_object.last_edit_time = datetime.now(timezone.utc)
        if user:
            update_object.editor_id = user.public_id

        type_ = self._type_manager.get(update_object.type_id)
        if not type_.active:
            raise AccessDeniedError(
                f'Objects cannot be updated because type `{type_.name}` is deactivated.'
            )
        verify_access(type_, user, permission)

        ack = self._update(collection=CmdbObject.COLLECTION,
                           public_id=update_object.get_public_id(),
                           data=update_object.__dict__)

        if self._event_queue and user:
            event = Event(
                "cmdb.core.object.updated", {
                    "id": update_object.get_public_id(),
                    "type_id": update_object.get_type_id(),
                    "user_id": user.get_public_id(),
                    'event': 'update'
                })
            self._event_queue.put(event)
        return ack.acknowledged