Пример #1
0
 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
Пример #2
0
 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_category(self, category: CategoryModel):
     """Add a new category into the database or add the children list an existing category"""
     try:
         return self._insert(collection=CategoryModel.COLLECTION,
                             data=CategoryModel.to_json(category))
     except Exception as err:
         raise ObjectManagerInsertError(err=err)
Пример #4
0
 def insert_link(self, data: dict):
     try:
         new_link = CmdbLink(
             public_id=self.get_new_id(collection=CmdbLink.COLLECTION),
             **data)
         return self._insert(CmdbLink.COLLECTION, new_link.to_database())
     except (CMDBError, Exception) as err:
         raise ObjectManagerInsertError(err)
Пример #5
0
 def insert_collection(self, data) -> int:
     try:
         new_collection = CmdbCollection(**data)
         ack = self.dbm.insert(CmdbCollection.COLLECTION,
                               new_collection.to_database())
     except (CMDBError, Exception) as err:
         LOGGER.error(err)
         raise ObjectManagerInsertError(err)
     return ack
Пример #6
0
 def insert_status(self, data) -> int:
     try:
         new_status = CmdbStatus(**data)
         ack = self.dbm.insert(CmdbStatus.COLLECTION,
                               new_status.to_database())
     except (CMDBError, Exception) as err:
         LOGGER.error(err)
         raise ObjectManagerInsertError(err)
     return ack
Пример #7
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