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 update_object(self, data: (dict, CmdbObject), request_user: User) -> 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() ack = self._update(collection=CmdbObject.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database()) # create cmdb.core.object.updated event if self._event_queue and request_user: event = Event( "cmdb.core.object.updated", { "id": update_object.get_public_id(), "type_id": update_object.get_type_id(), "user_id": request_user.get_public_id() }) self._event_queue.put(event) return ack.acknowledged