def get_job_by_args(self, **requirements) -> ExportdJob: try: found_type_list = self._get_many(collection=ExportdJob.COLLECTION, limit=1, **requirements) if len(found_type_list) > 0: return ExportdJob(**found_type_list[0]) else: raise ObjectManagerGetError(err='More than 1 type matches this requirement') except (CMDBError, Exception) as e: raise ObjectManagerGetError(err=e)
def get_type_by(self, **requirements) -> CmdbType: try: found_type_list = self._get_many(collection=CmdbType.COLLECTION, limit=1, **requirements) if len(found_type_list) > 0: return CmdbType(**found_type_list[0]) else: raise ObjectManagerGetError( err='More than 1 type matches this requirement') except (CMDBError, Exception) as e: raise ObjectManagerGetError(err=e)
def get_category(self, public_id: int) -> CmdbCategory: """Get a category from the database""" try: raw_category: dict = self._get(collection=CmdbCategory.COLLECTION, public_id=public_id) except Exception as err: raise ObjectManagerGetError(err=err) if not raw_category: raise ObjectManagerGetError( err=f'No category with the id: {public_id} was found') try: return CmdbCategory.from_data(raw_category) except Exception as err: raise ObjectManagerInitError(err=err)
def aggregate(self, collection, pipeline: Pipeline, **kwargs): try: return self._aggregate(collection=collection, pipeline=pipeline, **kwargs) except Exception as err: raise ObjectManagerGetError(err)
def get_collection(self, public_id: int) -> CmdbCollection: try: return CmdbCollection(**self.dbm.find_one( collection=CmdbCollection.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as err: LOGGER.error(err) raise ObjectManagerGetError(err)
def get_type(self, public_id: int): try: return CmdbType(**self.dbm.find_one(collection=CmdbType.COLLECTION, public_id=public_id)) except RequiredInitKeyNotFoundError as err: raise ObjectManagerInitError(err=err.message) except Exception as err: raise ObjectManagerGetError(err=err)
def get_collection_template(self, public_id: int) -> CmdbCollectionTemplate: try: return CmdbCollectionTemplate( **self._get(collection=CmdbCollectionTemplate.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as err: raise ObjectManagerGetError(err)
def get_category(self, public_id: int) -> CategoryModel: """Get a category from the database""" try: raw_category: dict = self._get(collection=CategoryModel.COLLECTION, public_id=public_id) return CategoryModel.from_data(raw_category) except Exception as err: raise ObjectManagerGetError(err=err)
def get_category_tree(self) -> CategoryTree: """Get the complete category tree""" try: categories = self.get_categories() types = self.get_all_types() except Exception as err: raise ObjectManagerGetError(err=err) return CategoryTree(categories=categories, types=types)
def get_types_by(self, sort='public_id', **requirements): try: return [ TypeModel.from_data(data) for data in self._get_many( collection=TypeModel.COLLECTION, sort=sort, **requirements) ] except Exception as err: raise ObjectManagerGetError(err=err)
def get_all_types(self) -> List[TypeModel]: try: raw_types: List[dict] = self._get_many(collection=TypeModel.COLLECTION) except Exception as err: raise ObjectManagerGetError(err=err) try: return [TypeModel.from_data(type) for type in raw_types] except Exception as err: raise ObjectManagerInitError(err=err)
def get_categories_by(self, sort='public_id', **requirements: dict) -> List[CategoryModel]: """Get a list of categories by special requirements""" try: raw_categories = self._get_many(collection=CategoryModel.COLLECTION, sort=sort, **requirements) except Exception as err: raise ObjectManagerGetError(err) try: return [CategoryModel.from_data(category) for category in raw_categories] except Exception as err: raise ObjectManagerInitError(err)
def get_categories(self) -> List[CategoryModel]: """Get all categories as nested list""" try: raw_categories = self._get_many(collection=CategoryModel.COLLECTION, sort='public_id') except Exception as err: raise ObjectManagerGetError(err) try: return [CategoryModel.from_data(category) for category in raw_categories] except Exception as err: raise ObjectManagerInitError(err)
def get_all_types(self) -> List[CmdbType]: try: raw_types: List[dict] = self._get_many( collection=CmdbType.COLLECTION) except Exception as err: raise ObjectManagerGetError(err=err) try: return [CmdbType(**type) for type in raw_types] except Exception as err: raise ObjectManagerInitError(err=err)
def get_links_by_partner(self, public_id: int) -> List[CmdbLink]: query = {'$or': [{'primary': public_id}, {'secondary': public_id}]} link_list: List[CmdbLink] = [] try: find_list: List[dict] = self._get_many(CmdbLink.COLLECTION, **query) for link in find_list: link_list.append(CmdbLink(**link)) except CMDBError as err: raise ObjectManagerGetError(err) return link_list
def get_object(self, public_id: int, user: UserModel = None, permission: AccessControlPermission = None) -> CmdbObject: try: resource = CmdbObject(**self._get( collection=CmdbObject.COLLECTION, public_id=public_id)) except Exception as err: raise ObjectManagerGetError(str(err)) type_ = self._type_manager.get(resource.type_id) verify_access(type_, user, permission) return resource
def get_category_by(self, **requirements) -> CategoryModel: """Get a single category by requirements Notes: Even if multiple categories match the requirements only the first matched will be returned """ try: raw_category = self._get_by(collection=CategoryModel.COLLECTION, **requirements) except Exception as err: raise ObjectManagerGetError(err) try: return CategoryModel.from_data(raw_category) except Exception as err: raise ObjectManagerInitError(err)
def get_collections(self) -> list: collection_list = list() try: collection_resp = self._get_many( collection=CmdbCollection.COLLECTION) except (CMDBError, Exception) as err: raise ObjectManagerGetError(err) for collection in collection_resp: try: collection_list.append(CmdbCollection(**collection)) except (CMDBError, Exception) as err: LOGGER.error(err) continue return collection_list
def get_statuses(self) -> list: status_list = list() try: collection_resp = self.dbm.find_all( collection=CmdbStatus.COLLECTION) except (CMDBError, Exception) as err: LOGGER.error(err) raise ObjectManagerGetError(err) for collection in collection_resp: try: status_list.append(CmdbStatus(**collection)) except (CMDBError, Exception) as err: LOGGER.error(err) continue return status_list
def get_link(self, public_id: int): try: return CmdbLink(**self._get(collection=CmdbLink.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as err: raise ObjectManagerGetError(err)
def search(self, collection, query: Query, **kwargs): try: return self._search(collection=collection, query=query, **kwargs) except Exception as err: raise ObjectManagerGetError(err)
def get_type(self, public_id: int): try: return CmdbType(**self.dbm.find_one(collection=CmdbType.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as e: raise ObjectManagerGetError(err=e)
def get_category(self, public_id: int): try: return CmdbCategory(**self._get(collection=CmdbCategory.COLLECTION, public_id=public_id)) except (CMDBError, Exception) as e: raise ObjectManagerGetError(err=e)