def get(self, public_id: Union[PublicID, int], user: UserModel = None, permission: AccessControlPermission = None) -> ObjectLinkModel: """ Get a single link by its id. Args: public_id (int): ID of the link. user: request user permission: acl permission Returns: ObjectLinkModel: Instance of CmdbLink with data. """ cursor_result = self._get(self.collection, filter={'public_id': public_id}, limit=1) for resource_result in cursor_result.limit(-1): link = ObjectLinkModel.from_data(resource_result) if user and permission: self.object_manager.get_object(public_id=link.primary, user=user, permission=permission) self.object_manager.get_object(public_id=link.secondary, user=user, permission=permission) return link raise ManagerGetError( f'ObjectLinkModel with ID: {public_id} not found!')
def get(self, public_id: Union[PublicID, int], user: UserModel = None, permission: AccessControlPermission = None) -> CmdbObject: """ Get a single object by its id. Args: public_id (int): ID of the object. user: Request user permission: ACL permission Returns: CmdbObject: Instance of CmdbObject with data. """ cursor_result = self._get(self.collection, filter={'public_id': public_id}, limit=1) for resource_result in cursor_result.limit(-1): resource = CmdbObject.from_data(resource_result) type_ = TypeManager(database_manager=self._database_manager).get( resource.type_id) verify_access(type_, user, permission) return resource else: raise ManagerGetError(f'Object with ID: {public_id} not found!')
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(self, public_id: Union[PublicID, int]) -> CategoryModel: """ Get a single type by its id. Args: public_id (int): ID of the type. Returns: CategoryModel: Instance of CategoryModel with data. """ cursor_result = self._get(self.collection, filter={'public_id': public_id}, limit=1) for resource_result in cursor_result.limit(-1): return CategoryModel.from_data(resource_result) raise ManagerGetError(f'Category with ID: {public_id} not found!')
def iterate(self, filter: dict, limit: int, skip: int, sort: str, order: int, user: UserModel = None, permission: AccessControlPermission = None, *args, **kwargs): """ Iterate over a collection of object logs resources. Args: filter: match requirements of field values limit: max number of elements to return skip: number of elements to skip first sort: sort field order: sort order Returns: IterationResult: Instance of IterationResult with generic CmdbObjectLog. """ try: query: Query = self.log_builder.build(filter=filter, limit=limit, skip=skip, sort=sort, order=order, user=user, permission=permission) count_query: Pipeline = self.log_builder.count( filter=filter, user=user, permission=permission) aggregation_result = list(self._aggregate(self.collection, query)) total_cursor = self._aggregate(self.collection, count_query) total = 0 while total_cursor.alive: total = next(total_cursor)['total'] except ManagerGetError as err: raise ManagerIterationError(err=err) try: iteration_result: IterationResult[CmdbMetaLog] = IterationResult( aggregation_result, total) iteration_result.convert_to(CmdbObjectLog) except ManagerGetError as err: raise ManagerGetError(err) return iteration_result
def get_user_setting(self, user_id: int, resource: str) -> UserSettingModel: """ Get a single setting from a user by the identifier. Args: user_id (int): PublicID of the user. resource (str): Name of the setting. Returns: UserSettingModel: Instance of UserSettingModel with data. """ result = self._get(self.collection, filter={ 'user_id': user_id, 'resource': resource }, limit=1) for resource_result in result.limit(-1): return UserSettingModel.from_data(resource_result) raise ManagerGetError( f'No setting with the name: {resource} was found!')