def get_log(self, public_id: int): try: return CmdbLog(**self._get(collection=CmdbMetaLog.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as err: LOGGER.error(err) raise LogManagerGetError(err)
def get(self, public_id: Union[PublicID, int]) -> Union[CmdbMetaLog, CmdbObjectLog]: cursor_result = self._get(self.collection, filter={'public_id': public_id}, limit=1) for resource_result in cursor_result.limit(-1): return CmdbLog.from_data(resource_result) raise ManagerGetError(f'Log with ID: {public_id} not found!')
def get_logs_by(self, sort='public_id', **requirements): ack = [] try: logs = self._get_many(collection=CmdbMetaLog.COLLECTION, sort=sort, **requirements) for log in logs: ack.append(CmdbLog(**log)) except (CMDBError, Exception) as err: LOGGER.error(err) raise LogManagerGetError(err) return ack
def insert_log(self, action: LogAction, log_type: str, **kwargs) -> int: # Get possible public id log_init = {} available_id = self.dbm.get_next_public_id( collection=CmdbMetaLog.COLLECTION) log_init['public_id'] = available_id # set static values log_init['action'] = action.value log_init['action_name'] = action.name log_init['log_type'] = log_type log_init['log_time'] = datetime.utcnow() log_data = {**log_init, **kwargs} try: new_log = CmdbLog(**log_data) ack = self._insert(CmdbMetaLog.COLLECTION, new_log.to_database()) except (CMDBError, Exception) as err: LOGGER.error(err) raise LogManagerInsertError(err) return ack
def get_object_logs(self, public_id: int) -> list: """ Get corresponding logs to object. Args: public_id: Public id for logs Returns: List of object-logs """ object_list: list = [] try: query = { 'filter': { 'log_type': str(CmdbObjectLog.__name__), 'object_id': public_id } } founded_logs = self.dbm.find_all(CmdbMetaLog.COLLECTION, **query) for _ in founded_logs: object_list.append(CmdbLog(**_)) except (CMDBError, Exception) as err: LOGGER.error(f'Error in get_object_logs: {err}') raise LogManagerGetError(err) return object_list