def insert_object(self, data: (CmdbObject, dict)) -> int:
     """
     Insert new CMDB Object
     Args:
         data: init data
         request_user: current user, to detect who triggered event
     Returns:
         Public ID of the new object in database
     """
     if isinstance(data, dict):
         try:
             new_object = CmdbObject(**data)
         except CMDBError as e:
             LOGGER.debug(
                 f'Error while inserting object - error: {e.message}')
             raise ObjectManagerInsertError(e)
     elif isinstance(data, CmdbObject):
         new_object = data
     try:
         ack = self.dbm.insert(collection=CmdbObject.COLLECTION,
                               data=new_object.to_database())
         if self._event_queue:
             event = Event(
                 "cmdb.core.object.added", {
                     "id": new_object.get_public_id(),
                     "type_id": new_object.get_type_id(),
                     "user_id": new_object.author_id
                 })
             self._event_queue.put(event)
     except (CMDBError, PublicIDAlreadyExists) as e:
         raise ObjectInsertError(e)
     return ack
 def insert_collection_template(self, data: dict) -> int:
     # Insert data
     try:
         possible_id: int = self.dbm.get_highest_id(
             collection=CmdbCollectionTemplate.COLLECTION) + 1
         data.update({'public_id': possible_id})
         data.update({'creation_time': datetime.utcnow()})
         collection_template_id = self._insert(
             CmdbCollectionTemplate.COLLECTION, data)
     except (CMDBError, Exception) as err:
         raise ObjectManagerInsertError(err)
     # Check if instance is valid
     try:
         self.get_collection_template(public_id=collection_template_id)
     except ObjectManagerGetError as err:
         # Invalid instance -> delete
         try:
             self.delete_collection_template(collection_template_id)
         except ObjectManagerDeleteError as err_delete:
             raise ObjectInsertError(
                 f'Instance is invalid, but could not delete template: {err_delete.message}'
             )
         raise ObjectManagerInsertError(
             f'Error in instance of new object: {err.message}')
     return collection_template_id
Example #3
0
    def insert_object(self,
                      data: (CmdbObject, dict),
                      user: UserModel = None,
                      permission: AccessControlPermission = None) -> int:
        """
        Insert new CMDB Object
        Args:
            data: init data
            user: current user, to detect who triggered event
            permission: extended user acl rights
        Returns:
            Public ID of the new object in database
        """
        new_object = None
        if isinstance(data, dict):
            try:
                new_object = CmdbObject(**data)
            except CMDBError as e:
                LOGGER.debug(
                    f'Error while inserting object - error: {e.message}')
                raise ObjectManagerInsertError(e)
        elif isinstance(data, CmdbObject):
            new_object = data

        type_ = self._type_manager.get(new_object.type_id)
        if not type_.active:
            raise AccessDeniedError(
                f'Objects cannot be created because type `{type_.name}` is deactivated.'
            )

        verify_access(type_, user, permission)

        try:
            ack = self.dbm.insert(collection=CmdbObject.COLLECTION,
                                  data=new_object.__dict__)
            if self._event_queue:
                event = Event(
                    "cmdb.core.object.added", {
                        "id": new_object.get_public_id(),
                        "type_id": new_object.get_type_id(),
                        "user_id": new_object.author_id,
                        "event": 'insert'
                    })
                self._event_queue.put(event)
        except (CMDBError, PublicIDAlreadyExists) as e:
            raise ObjectInsertError(e)
        return ack