def get_exportd_jobs(params: CollectionParameters): """Iterate route for exportd jobs""" job_manager = ExportDJobManager(current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[ExportdJob] = job_manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) types = [ExportdJob.to_json(type) for type in iteration_result.results] api_response = GetMultiResponse(types, total=iteration_result.total, params=params, url=request.url, model=ExportdJob.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_file_list(params: CollectionParameters): """ Get all objects in database Args: params (CollectionParameters): Passed parameters over the http query string + optional `view` parameter. Raises: MediaFileManagerGetError: If the files could not be found. Returns: list of files """ try: metadata = generate_collection_parameters(params=params) response_query = { 'limit': params.limit, 'skip': params.skip, 'sort': [(params.sort, params.order)] } output = media_file_manager.get_many(metadata, **response_query) api_response = GetMultiResponse(output.result, total=output.total, params=params, url=request.url) except MediaFileManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_links(params: CollectionParameters, request_user: UserModel): link_manager = ObjectLinkManager( database_manager=current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[ ObjectLinkModel] = link_manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order, user=request_user, permission=AccessControlPermission.READ) types = [ ObjectLinkModel.to_json(type) for type in iteration_result.results ] api_response = GetMultiResponse(types, total=iteration_result.total, params=params, url=request.url, model=ObjectLinkModel.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_groups(params: CollectionParameters): """ HTTP `GET`/`HEAD` route for getting a iterable collection of resources. Args: params (CollectionParameters): Passed parameters over the http query string Returns: GetMultiResponse: Which includes a IterationResult of the UserGroupModel. Notes: Calling the route over HTTP HEAD method will result in an empty body. Raises: ManagerIterationError: If the collection could not be iterated. ManagerGetError: If the collection/resources could not be found. """ group_manager: GroupManager = GroupManager(database_manager=current_app.database_manager, right_manager=RightManager(rights)) try: iteration_result: IterationResult[UserGroupModel] = group_manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) groups = [UserGroupModel.to_dict(group) for group in iteration_result.results] api_response = GetMultiResponse(groups, total=iteration_result.total, params=params, url=request.url, model=UserGroupModel.MODEL, body=request.method == 'HEAD') except FrameworkIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_logs_by_object(object_id: int, params: CollectionParameters, request_user: UserModel): manager = CmdbLogManager(database_manager=database_manager) try: ObjectManager(database_manager=database_manager).get( object_id, user=request_user, permission=AccessControlPermission.READ) body = request.method == 'HEAD' iteration_result = manager.iterate(public_id=object_id, filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) logs = [CmdbObjectLog.to_json(_) for _ in iteration_result.results] api_response = GetMultiResponse(logs, total=iteration_result.total, params=params, url=request.url, model=CmdbMetaLog.MODEL, body=body) except ManagerGetError as err: return abort(404, err.message) except AccessDeniedError as err: return abort(403, err.message) except ManagerIterationError as err: return abort(400, err.message) return api_response.make_response()
def get_object_delete_logs(params: CollectionParameters): manager = CmdbLogManager(database_manager=database_manager) try: query = { 'log_type': CmdbObjectLog.__name__, 'action': LogAction.DELETE.value } body = request.method == 'HEAD' object_logs = manager.iterate(filter=query, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) logs = [CmdbObjectLog.to_json(_) for _ in object_logs.results] api_response = GetMultiResponse(logs, total=object_logs.total, params=params, url=request.url, model=CmdbMetaLog.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ObjectManagerGetError as err: LOGGER.error(f'Error in get_object_delete_logs: {err}') return abort(404) return api_response.make_response()
def get_template_list(params: CollectionParameters): template_manager = DocapiTemplateManager( database_manager=current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[ DocapiTemplate] = template_manager.get_templates( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) types = [ DocapiTemplate.to_json(type) for type in iteration_result.results ] api_response = GetMultiResponse(types, total=iteration_result.total, params=params, url=request.url, model=DocapiTemplate.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_objects(params: CollectionParameters, request_user: UserModel): manager = ObjectManager(database_manager=current_app.database_manager) view = params.optional.get('view', 'native') if _fetch_only_active_objs(): if isinstance(params.filter, dict): filter_ = params.filter params.filter = [{'$match': filter_}] params.filter.append({'$match': {'active': {"$eq": True}}}) elif isinstance(params.filter, list): params.filter.append({'$match': {'active': {"$eq": True}}}) try: iteration_result: IterationResult[CmdbObject] = manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order, user=request_user, permission=AccessControlPermission.READ) if view == 'native': object_list: List[dict] = [ object_.__dict__ for object_ in iteration_result.results ] api_response = GetMultiResponse(object_list, total=iteration_result.total, params=params, url=request.url, model=CmdbObject.MODEL, body=request.method == 'HEAD') elif view == 'render': rendered_list = RenderList( object_list=iteration_result.results, request_user=request_user, database_manager=current_app.database_manager, object_manager=object_manager, ref_render=True).render_result_list(raw=True) api_response = GetMultiResponse(rendered_list, total=iteration_result.total, params=params, url=request.url, model=Model('RenderResult'), body=request.method == 'HEAD') else: return abort(401, 'No possible view parameter') except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_types(params: CollectionParameters): """ HTTP `GET`/`HEAD` route for getting a iterable collection of resources. Args: params (CollectionParameters): Passed parameters over the http query string Returns: GetMultiResponse: Which includes a IterationResult of the TypeModel. Example: You can pass any parameter based on the CollectionParameters. Optional parameters are passed over the function declaration. Raises: FrameworkIterationError: If the collection could not be iterated. ManagerGetError: If the collection could not be found. """ type_manager = TypeManager(database_manager=current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[TypeModel] = type_manager.iterate( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) types = [TypeModel.to_json(type) for type in iteration_result.results] api_response = GetMultiResponse(types, total=iteration_result.total, params=params, url=request.url, model=TypeModel.MODEL, body=body) except FrameworkIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_categories(params: CollectionParameters): """ HTTP `GET`/`HEAD` route for getting a iterable collection of resources. Args: params (CollectionParameters): Passed parameters over the http query string + optional `view` parameter. Returns: GetMultiResponse: Which includes a IterationResult of the CategoryModel. If the view parameter with tree was set the route returns a GetMultiResponse<CategoryTree>. Example: You can pass any parameter based on the CollectionParameters. Optional parameters are passed over the function declaration. The `view` parameter is optional and default `list`, but can be `tree` for the category tree view. Raises: FrameworkIterationError: If the collection could not be iterated. ManagerGetError: If the collection could not be found. """ category_manager: CategoryManager = CategoryManager( database_manager=current_app.database_manager) body = True if not request.method != 'HEAD' else False try: if params.optional['view'] == 'tree': tree: CategoryTree = category_manager.tree api_response = GetMultiResponse(CategoryTree.to_json(tree), total=len(tree), params=params, url=request.url, model=CategoryTree.MODEL, body=body) return api_response.make_response(pagination=False) else: iteration_result: IterationResult[ CategoryModel] = category_manager.iterate(filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) category_list = [ CategoryModel.to_json(category) for category in iteration_result.results ] api_response = GetMultiResponse(category_list, total=iteration_result.total, params=params, url=request.url, model=CategoryModel.MODEL, body=body) except FrameworkIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_rights(params: CollectionParameters): """ HTTP `GET`/`HEAD` route for getting a iterable collection of resources. Args: params (CollectionParameters): Passed parameters over the http query string Returns: GetMultiResponse: Which includes a IterationResult of the BaseRight. Notes: Calling the route over HTTP HEAD method will result in an empty body. Raises: ManagerIterationError: If the collection could not be iterated. ManagerGetError: If the collection/resources could not be found. """ right_manager = RightManager(right_tree) body = request.method == 'HEAD' try: if params.optional['view'] == 'tree': api_response = GetMultiResponse( right_manager.tree_to_json(right_tree), total=len(right_tree), params=params, url=request.url, model='Right-Tree', body=body) return api_response.make_response(pagination=False) else: iteration_result: IterationResult[ BaseRight] = right_manager.iterate(filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) rights = [ BaseRight.to_dict(type) for type in iteration_result.results ] api_response = GetMultiResponse(rights, total=iteration_result.total, params=params, url=request.url, model=Model('Right'), body=request.method == 'HEAD') return api_response.make_response() except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message)
def get_logs_with_deleted_objects(params: CollectionParameters): manager = CmdbLogManager(database_manager=database_manager) try: query = [] if isinstance(params.filter, dict): query.append({'$match': params.filter}) elif isinstance(params.filter, list): for pipe in params.filter: query.append(pipe) query.append({ '$match': { 'log_type': CmdbObjectLog.__name__, 'action': { '$ne': LogAction.DELETE.value } } }) query.append({ "$lookup": { "from": "framework.objects", "let": { "ref_id": "$object_id" }, "pipeline": [{ '$match': { '$expr': { '$eq': ["$public_id", '$$ref_id'] } } }], "as": "object" } }) query.append({ '$unwind': { 'path': '$object', 'preserveNullAndEmptyArrays': True } }) query.append({'$match': {'object': {'$exists': False}}}) body = request.method == 'HEAD' object_logs = manager.iterate(filter=query, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) logs = [CmdbObjectLog.to_json(_) for _ in object_logs.results] api_response = GetMultiResponse(logs, total=object_logs.total, params=params, url=request.url, model=CmdbMetaLog.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ObjectManagerGetError as err: LOGGER.error(f'Error in get_logs_with_deleted_objects: {err}') return abort(404) return api_response.make_response()