def __init__(self, database_manager: DatabaseManagerMongo): self.query_builder = ObjectQueryBuilder() self.object_manager = CmdbObjectManager( database_manager) # TODO: Replace when object api is updated super(ObjectLinkManager, self).__init__(ObjectLinkModel.COLLECTION, database_manager=database_manager)
def get_unstructured_objects(public_id: int, request_user: UserModel): """ HTTP `GET`/`HEAD` route for a multi resources which are not formatted according the type structure. Args: public_id (int): Public ID of the type. Raises: ManagerGetError: When the selected type does not exists or the objects could not be loaded. Returns: GetListResponse: Which includes the json data of multiple objects. """ type_manager = TypeManager(database_manager=current_app.database_manager) object_manager = CmdbObjectManager( database_manager=current_app.database_manager) try: type: TypeModel = type_manager.get(public_id=public_id) objects: List[CmdbObject] = object_manager.get_objects_by( type_id=public_id) except ManagerGetError as err: return abort(400, err.message) type_fields = sorted([field.get('name') for field in type.fields]) unstructured_objects: List[CmdbObject] = [] for object_ in objects: object_fields = [field.get('name') for field in object_.fields] if sorted(object_fields) != type_fields: unstructured_objects.append(object_) api_response = GetListResponse( [un_object.__dict__ for un_object in unstructured_objects], url=request.url, model='Object', body=request.method == 'HEAD') return api_response.make_response()
def __init__(self, job: ExportdJob): self.job = job self.exportvars = self.__get_exportvars() self.destinations = self.__get__destinations() scr = SystemConfigReader() database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.__object_manager = CmdbObjectManager( database_manager=database_manager) self.log_manager = ExportdLogManager(database_manager=database_manager) self.sources = self.__get_sources() super(ExportdManagerBase, self).__init__(database_manager)
def from_database(self, database_manager, user: UserModel, permission: AccessControlPermission): """Get all objects from the collection""" manager = ObjectManager(database_manager=database_manager) dep_object_manager = CmdbObjectManager( database_manager=database_manager) try: _params = self.export_config.parameters _result: List[CmdbObject] = manager.iterate( filter=_params.filter, sort=_params.sort, order=_params.order, limit=0, skip=0, user=user, permission=permission).results self.data = RenderList( object_list=_result, request_user=user, database_manager=database_manager, object_manager=dep_object_manager, ref_render=True).render_result_list(raw=False) except CMDBError as e: return abort(400, e)
def __init__(self, file, file_type, config: ObjectImporterConfig = None, parser: BaseObjectParser = None, object_manager: CmdbObjectManager = None, request_user: UserModel = None): """ Basic importer super class for object imports Normally should be started by start_import Args: file: File instance, name, content or loaded path to file file_type: file type - used with content-type config: importer configuration parser: the parser instance based on content-type object_manager: a instance of the object managers request_user: the instance of the started user """ self.parser = parser if object_manager: self.object_manager = object_manager else: from cmdb.utils.system_config import SystemConfigReader from cmdb.database.managers import DatabaseManagerMongo object_manager = CmdbObjectManager( database_manager=DatabaseManagerMongo(**SystemConfigReader( ).get_all_values_from_section('Database'))) self.object_manager = object_manager self.request_user = request_user super(ObjectImporter, self).__init__(file=file, file_type=file_type, config=config)
def object_manager(mongodb): from cmdb.data_storage.database_manager import DatabaseManagerMongo from cmdb.framework.cmdb_object_manager import CmdbObjectManager return CmdbObjectManager( database_manager=DatabaseManagerMongo(connector=mongodb) )
def __init__(self): scr = SystemConfigReader() self.database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.object_manager = CmdbObjectManager( database_manager=self.database_manager) self.log_manager = ExportdLogManager( database_manager=self.database_manager) super().__init__(self.database_manager)
def tree(self) -> CategoryTree: # Currently only a work around until the other managers were converted to the new format - MH types = CmdbObjectManager( database_manager=self._database_manager).get_all_types() categories = [ CategoryModel.from_data(category) for category in super(CategoryManager, self).get_many({}) ] return CategoryTree(categories=categories, types=types)
def __init__(self, object_list: List[CmdbObject], request_user: UserModel, dt_render=False, ref_render=False, object_manager: CmdbObjectManager = None): self.object_list: List[CmdbObject] = object_list self.request_user = request_user self.dt_render = dt_render self.ref_render = ref_render from cmdb.utils.system_config import SystemConfigReader database_manager = DatabaseManagerMongo( **SystemConfigReader().get_all_values_from_section('Database') ) self.object_manager = object_manager or CmdbObjectManager(database_manager=database_manager) self.user_manager = UserManager(database_manager=database_manager)
def delete_type(public_id: int): """ HTTP `DELETE` route for delete a single type resource. Args: public_id (int): Public ID of the deletable type Raises: ManagerGetError: When the type with the `public_id` was not found. ManagerDeleteError: When something went wrong during the deletion. Notes: Deleting the type will also delete all objects in this type! Returns: DeleteSingleResponse: Delete result with the deleted type as data. """ type_manager = TypeManager(database_manager=current_app.database_manager) from cmdb.framework.cmdb_object_manager import CmdbObjectManager deprecated_object_manager = CmdbObjectManager( database_manager=current_app.database_manager) try: objects_ids = [ object_.get_public_id() for object_ in deprecated_object_manager.get_objects_by_type(public_id) ] deprecated_object_manager.delete_many_objects({'type_id': public_id}, objects_ids, None) deleted_type = type_manager.delete(public_id=PublicID(public_id)) api_response = DeleteSingleResponse( raw=TypeModel.to_json(deleted_type), model=TypeModel.MODEL) except ManagerGetError as err: return abort(404, err.message) except ManagerDeleteError as err: return abort(400, err.message) except Exception as err: return abort(400, str(err)) return api_response.make_response()
def build(self, params: List[SearchParam], obj_manager: CmdbObjectManager = None, active_flag: bool = False) -> Pipeline: """Build a pipeline query out of frontend params""" # clear pipeline self.clear() # fetch only active objects if active_flag: self.add_pipe(self.match_({'active': {"$eq": True}})) # text builds text_params = [ _ for _ in params if _.search_form == 'text' or _.search_form == 'regex' ] for param in text_params: regex = self.regex_('fields.value', param.search_text, 'ims') self.add_pipe(self.match_(regex)) # type builds disjunction_query = [] type_params = [_ for _ in params if _.search_form == 'type'] for param in type_params: if param.settings and len(param.settings.get('types', [])) > 0: type_id_in = self.in_('type_id', param.settings['types']) if param.disjunction: disjunction_query.append(type_id_in) else: self.add_pipe(self.match_(type_id_in)) if len(disjunction_query) > 0: self.add_pipe(self.match_(self.or_(disjunction_query))) # category builds category_params = [_ for _ in params if _.search_form == 'category'] for param in category_params: if param.settings and len(param.settings.get('categories', [])) > 0: categories = obj_manager.get_categories_by( **self.regex_('label', param.search_text)) for curr_category in categories: type_id_in = self.in_('type_id', curr_category.types) self.add_pipe(self.match_(type_id_in)) # public builds id_params = [_ for _ in params if _.search_form == 'publicID'] for param in id_params: self.add_pipe(self.match_({'public_id': int(param.search_text)})) return self.pipeline
def tree(self) -> CategoryTree: """ Get the complete category list as nested tree. Returns: CategoryTree: Categories as tree structure. """ types = CmdbObjectManager( database_manager=self._database_manager).get_all_types() categories = self.iterate(filter={}, limit=0, skip=0, sort='public_id', order=1).results return CategoryTree(categories=categories, types=types)
def insert_data(self) -> list: """insert data into database""" if self._database_manager is None: raise NoDatabaseManagerError() from cmdb.framework.cmdb_object_manager import CmdbObjectManager from cmdb.user_management.user_manager import UserManager obm = CmdbObjectManager(database_manager=self._database_manager) usm = UserManager(database_manager=self._database_manager) error = [] for group_element in self.groups: try: usm.insert_group(group_element) except CMDBError as e: error.append(e.message) continue for user_element in self.users: try: usm.insert_user(user_element) except CMDBError as e: error.append(e.message) continue for type_element in self.types: try: obm.insert_type(type_element) except CMDBError as e: error.append(e.message) continue for category_element in self.categories: try: obm.insert_category(category_element) except CMDBError as e: error.append(e.message) continue for object_element in self.objects: try: obm.insert_object(object_element) except CMDBError as e: error.append(e.message) continue return error
def build(self, params: List[SearchParam], obj_manager: CmdbObjectManager = None, active_flag: bool = False) -> Pipeline: """Build a pipeline query out of frontend params""" # clear pipeline self.clear() # fetch only active objects if active_flag: self.add_pipe(self.match_({'active': {"$eq": True}})) # text builds text_params = [_ for _ in params if _.search_form == 'text'] for param in text_params: regex = self.regex_('fields.value', param.search_text, 'ims') self.add_pipe(self.match_(regex)) # type builds type_params = [_ for _ in params if _.search_form == 'type'] for param in type_params: if param.settings and len(param.settings.get('types', [])) > 0: type_id_in = self.in_('type_id', param.settings['types']) self.add_pipe(self.match_(type_id_in)) # category builds category_params = [_ for _ in params if _.search_form == 'category'] for param in category_params: if param.settings and len(param.settings.get('categories', [])) > 0: type_list = obj_manager.get_types_by(**self.in_('category_id', param.settings['categories'])) type_ids = [] for curr_type in type_list: type_ids.append(curr_type.get_public_id()) type_id_in = self.in_('type_id', type_ids) self.add_pipe(self.match_(type_id_in)) # public builds id_params = [_ for _ in params if _.search_form == 'publicID'] for param in id_params: self.add_pipe(self.match_({'public_id': int(param.search_text)})) return self.pipeline
def __init__(self, event: Event, state: bool = False): scr = SystemConfigReader() database_options = scr.get_all_values_from_section('Database') database = DatabaseManagerMongo(**database_options) super(ExportdThread, self).__init__() self.job = None self.job_id = int(event.get_param("id")) self.type_id = int( event.get_param("type_id")) if event.get_param("type_id") else None self.user_id = int(event.get_param("user_id")) self.event = event self.is_active = state self.exception_handling = None self.object_manager = CmdbObjectManager(database_manager=database) self.log_manager = ExportdLogManager(database_manager=database) self.exportd_job_manager = ExportdJobManagement( database_manager=database) self.user_manager = UserManager(database_manager=database)
ExportdLogManager from cmdb.exportd.exportd_job.exportd_job import ExportdJob, ExecuteState from cmdb.exportd.managers.exportd_job_manager import ExportDJobManager from cmdb.framework.results import IterationResult from cmdb.interface.api_parameters import CollectionParameters from cmdb.interface.response import GetMultiResponse from cmdb.interface.rest_api.exportd_routes import exportd_blueprint from cmdb.interface.route_utils import make_response, login_required, insert_request_user, right_required from cmdb.interface.blueprint import RootBlueprint from cmdb.framework.cmdb_errors import ObjectManagerGetError from cmdb.manager import ManagerIterationError, ManagerGetError from cmdb.user_management import UserModel from cmdb.utils.error import CMDBError with current_app.app_context(): object_manager = CmdbObjectManager(current_app.database_manager) exportd_manager = ExportdJobManagement(current_app.database_manager, current_app.event_queue) log_manager = ExportdLogManager(current_app.database_manager) LOGGER = logging.getLogger(__name__) exportd_job_blueprint = RootBlueprint('exportd_job_blueprint', __name__, url_prefix='/exportdjob') # DEFAULT ROUTES @exportd_blueprint.route('/jobs', methods=['GET', 'HEAD']) @exportd_blueprint.protect(auth=True, right='base.exportd.job.view') @exportd_blueprint.parse_collection_parameters() def get_exportd_jobs(params: CollectionParameters):
import json import datetime import time from flask import abort, jsonify, current_app, Response from cmdb.framework import TypeModel from cmdb.framework.cmdb_object_manager import CmdbObjectManager from cmdb.interface.route_utils import login_required from cmdb.interface.blueprint import RootBlueprint from cmdb.framework.cmdb_errors import TypeNotFoundError from cmdb.utils import json_encoding from cmdb.utils.error import CMDBError with current_app.app_context(): object_manager = CmdbObjectManager(current_app.database_manager, current_app.event_queue) type_export_blueprint = RootBlueprint('type_export_rest', __name__, url_prefix='/export/type') @type_export_blueprint.route('/', methods=['POST']) @login_required def export_type(): try: type_list = [ TypeModel.to_json(type) for type in object_manager.get_all_types() ] resp = json.dumps(type_list, default=json_encoding.default, indent=2) except TypeNotFoundError as e:
def update_unstructured_objects(public_id: int, request_user: UserModel): """ HTTP `PUT`/`PATCH` route for a multi resources which will be formatted based on the TypeModel Args: public_id (int): Public ID of the type. Raises: ManagerGetError: When the type with the `public_id` was not found. ManagerUpdateError: When something went wrong during the update. Returns: UpdateMultiResponse: Which includes the json data of multiple updated objects. """ type_manager = TypeManager(database_manager=current_app.database_manager) object_manager = CmdbObjectManager( database_manager=current_app.database_manager) try: update_type_instance = type_manager.get(public_id) type_fields = update_type_instance.fields objects_by_type = object_manager.get_objects_by_type(type_id=public_id) for obj in objects_by_type: incorrect = [] correct = [] obj_fields = obj.get_all_fields() for t_field in type_fields: name = t_field["name"] for field in obj_fields: if name == field["name"]: correct.append(field["name"]) else: incorrect.append(field["name"]) removed_type_fields = [ item for item in incorrect if not item in correct ] for field in removed_type_fields: object_manager.remove_object_fields( filter_query={'public_id': obj.public_id}, update={'$pull': { 'fields': { "name": field } }}) objects_by_type = object_manager.get_objects_by_type(type_id=public_id) for obj in objects_by_type: for t_field in type_fields: name = t_field["name"] value = None if [ item for item in obj.get_all_fields() if item["name"] == name ]: continue if "value" in t_field: value = t_field["value"] object_manager.update_object_fields( filter={'public_id': obj.public_id}, update={ '$addToSet': { 'fields': { "name": name, "value": value } } }) except ManagerUpdateError as err: return abort(400, err.message) api_response = UpdateMultiResponse([], url=request.url, model='Object') return api_response.make_response()
def create_rest_api(event_queue): from cmdb.interface.config import app_config from cmdb.utils.system_config import SystemConfigReader system_config_reader = SystemConfigReader() try: cache_config = { 'DEBUG': True, 'CACHE_TYPE': system_config_reader.get_value('name', 'Cache'), 'CACHE_REDIS_HOST': system_config_reader.get_value('host', 'Cache'), 'CACHE_REDIS_PORT': system_config_reader.get_value('port', 'Cache'), 'CACHE_REDIS_PASSWORD': system_config_reader.get_value('password', 'Cache'), } except (ImportError, CMDBError) as e: LOGGER.debug(e.message) cache_config = {'CACHE_TYPE': 'simple'} from flask_caching import Cache cache = Cache(config=cache_config) # Create manager from cmdb.data_storage.database_manager import DatabaseManagerMongo app_database = DatabaseManagerMongo( **system_config_reader.get_all_values_from_section('Database')) object_manager = CmdbObjectManager(database_manager=app_database, event_queue=event_queue) log_manager = CmdbLogManager(database_manager=app_database) security_manager = SecurityManager(database_manager=app_database) user_manager = UserManager(database_manager=app_database) exportd_job_manager = ExportdJobManagement(database_manager=app_database, event_queue=event_queue) exportd_log_manager = ExportdLogManager(database_manager=app_database) media_file_manager = MediaFileManagement(database_manager=app_database) docapi_tpl_manager = DocapiTemplateManager(database_manager=app_database) # Create APP from cmdb.interface.cmdb_app import BaseCmdbApp app = BaseCmdbApp(__name__, database_manager=app_database, docapi_tpl_manager=docapi_tpl_manager, media_file_manager=media_file_manager, exportd_manager=exportd_job_manager, exportd_log_manager=exportd_log_manager, object_manager=object_manager, log_manager=log_manager, user_manager=user_manager, security_manager=security_manager) # Import App Extensions from flask_cors import CORS CORS(app) import cmdb cache.init_app(app) cache.clear() app.cache = cache if cmdb.__MODE__ == 'DEBUG': app.config.from_object(app_config['rest_development']) LOGGER.info('RestAPI starting with config mode {}'.format( app.config.get("ENV"))) elif cmdb.__MODE__ == 'TESTING': app.config.from_object(app_config['testing']) else: app.config.from_object(app_config['rest']) LOGGER.info('RestAPI starting with config mode {}'.format( app.config.get("ENV"))) with app.app_context(): register_converters(app) register_error_pages(app) register_blueprints(app) return app
def build(self, params: List[SearchParam], obj_manager: CmdbObjectManager = None, user: UserModel = None, permission: AccessControlPermission = None, active_flag: bool = False, *args, **kwargs) -> Pipeline: """Build a pipeline query out of frontend params""" # clear pipeline self.clear() # load reference fields in runtime. self.pipeline = SearchReferencesPipelineBuilder().build() # fetch only active objects if active_flag: self.add_pipe(self.match_({'active': {"$eq": True}})) # text builds text_params = [ _ for _ in params if _.search_form == 'text' or _.search_form == 'regex' ] for param in text_params: regex = self.regex_('fields.value', param.search_text, 'ims') self.add_pipe(self.match_(regex)) # type builds disjunction_query = [] type_params = [_ for _ in params if _.search_form == 'type'] for param in type_params: if param.settings and len(param.settings.get('types', [])) > 0: type_id_in = self.in_('type_id', param.settings['types']) if param.disjunction: disjunction_query.append(type_id_in) else: self.add_pipe(self.match_(type_id_in)) if len(disjunction_query) > 0: self.add_pipe(self.match_(self.or_(disjunction_query))) # category builds category_params = [_ for _ in params if _.search_form == 'category'] for param in category_params: if param.settings and len(param.settings.get('categories', [])) > 0: categories = obj_manager.get_categories_by( **self.regex_('label', param.search_text)) for curr_category in categories: type_id_in = self.in_('type_id', curr_category.types) self.add_pipe(self.match_(type_id_in)) # public builds id_params = [_ for _ in params if _.search_form == 'publicID'] for param in id_params: self.add_pipe(self.match_({'public_id': int(param.search_text)})) # permission builds if user and permission: self.pipeline = [ *self.pipeline, *(AccessControlQueryBuilder().build( group_id=PublicID(user.group_id), permission=permission)) ] return self.pipeline
def create_rest_api(event_queue): from cmdb.interface.config import app_config from cmdb.utils.system_config import SystemConfigReader system_config_reader = SystemConfigReader() # Create managers from cmdb.data_storage.database_manager import DatabaseManagerMongo app_database = DatabaseManagerMongo( **system_config_reader.get_all_values_from_section('Database')) object_manager = CmdbObjectManager(database_manager=app_database, event_queue=event_queue) log_manager = CmdbLogManager(database_manager=app_database) security_manager = SecurityManager(database_manager=app_database) user_manager = UserManager(database_manager=app_database) exportd_job_manager = ExportdJobManagement(database_manager=app_database, event_queue=event_queue) exportd_log_manager = ExportdLogManager(database_manager=app_database) media_file_manager = MediaFileManagement(database_manager=app_database) docapi_tpl_manager = DocapiTemplateManager(database_manager=app_database) # Create APP from cmdb.interface.cmdb_app import BaseCmdbApp app = BaseCmdbApp(__name__, database_manager=app_database, docapi_tpl_manager=docapi_tpl_manager, media_file_manager=media_file_manager, exportd_manager=exportd_job_manager, exportd_log_manager=exportd_log_manager, object_manager=object_manager, log_manager=log_manager, user_manager=user_manager, security_manager=security_manager) app.url_map.strict_slashes = True # Import App Extensions from flask_cors import CORS CORS(app, expose_headers=['X-API-Version', 'X-Total-Count']) import cmdb if cmdb.__MODE__ == 'DEBUG': config = app_config['development'] config.APPLICATION_ROOT = '/rest/' app.config.from_object(config) else: config = app_config['production'] config.APPLICATION_ROOT = '/rest/' app.config.from_object(config) with app.app_context(): register_converters(app) register_error_pages(app) register_blueprints(app) return app
class ObjectLinkManager(ManagerBase): def __init__(self, database_manager: DatabaseManagerMongo): self.query_builder = ObjectQueryBuilder() self.object_manager = CmdbObjectManager( database_manager) # TODO: Replace when object api is updated super(ObjectLinkManager, self).__init__(ObjectLinkModel.COLLECTION, database_manager=database_manager) 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 iterate(self, filter: Union[List[dict], dict], limit: int, skip: int, sort: str, order: int, user: UserModel = None, permission: AccessControlPermission = None, *args, **kwargs) \ -> IterationResult[ObjectLinkModel]: """ Iterate over a collection where the public id exists. """ try: query: Pipeline = self.query_builder.build(filter=filter, limit=limit, skip=skip, sort=sort, order=order) count_query: Pipeline = self.query_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) iteration_result: IterationResult[ObjectLinkModel] = IterationResult( aggregation_result, total) iteration_result.convert_to(ObjectLinkModel) return iteration_result def insert( self, link: Union[dict, ObjectLinkModel], user: UserModel = None, permission: AccessControlPermission = AccessControlPermission.CREATE ) -> PublicID: """ Insert a single link into the system. Args: link (ObjectLinkModel): Raw data of the link. user: request user permission: acl permission Notes: If no public id was given, the database manager will auto insert the next available ID. Returns: int: The Public ID of the new inserted link """ if isinstance(link, ObjectLinkModel): link = ObjectLinkModel.to_json(link) if 'creation_time' not in link: link['creation_time'] = datetime.now(timezone.utc) 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 self._insert(self.collection, resource=link) def update(self, public_id: PublicID, link: Union[ObjectLinkModel, dict]): raise NotImplementedError def delete( self, public_id: PublicID, user: UserModel = None, permission: AccessControlPermission = AccessControlPermission.DELETE ) -> ObjectLinkModel: """ Delete a existing link by its PublicID. Args: public_id (int): PublicID of the link in the system. user: request user permission: acl permission Returns: ObjectLinkModel: The deleted link as its model. """ link: ObjectLinkModel = self.get(public_id=public_id) 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) delete_result = self._delete(self.collection, filter={'public_id': public_id}) if delete_result.deleted_count == 0: raise ManagerDeleteError(err='No link matched this public id') return link
def object_manager(database_manager) -> CmdbObjectManager: return CmdbObjectManager(database_manager=database_manager)
from flask import abort, Response from cmdb.data_storage.database_manager import DatabaseManagerMongo from cmdb.framework.cmdb_errors import ObjectNotFoundError, TypeNotFoundError from cmdb.file_export.export_types import ExportType from cmdb.framework.cmdb_object_manager import CmdbObjectManager try: from cmdb.utils.error import CMDBError except ImportError: CMDBError = Exception from cmdb.utils.system_config import SystemConfigReader object_manager = CmdbObjectManager(database_manager=DatabaseManagerMongo( **SystemConfigReader().get_all_values_from_section('Database'))) class FileExporter: @staticmethod def get_filetypes(): filetypes = [ "CsvExportType", "JsonExportType", "XlsxExportType", "XmlExportType" ] return filetypes def __init__(self, object_type, export_type: ExportType, public_id: str, **kwargs): """init of FileExporter
class ExportdManagerBase(ExportdJobManagement): def __init__(self, job: ExportdJob): self.job = job self.exportvars = self.__get_exportvars() self.destinations = self.__get__destinations() scr = SystemConfigReader() database_manager = DatabaseManagerMongo( **scr.get_all_values_from_section('Database')) self.__object_manager = CmdbObjectManager( database_manager=database_manager) self.log_manager = ExportdLogManager(database_manager=database_manager) self.sources = self.__get_sources() super(ExportdManagerBase, self).__init__(database_manager) def __get_exportvars(self): exportvars = {} for variable in self.job.get_variables(): exportvars.update({ variable["name"]: ExportVariable(variable["name"], variable["default"], variable["templates"]) }) return exportvars def __get_sources(self): sources = [] sources.append( ExportSource(self.job, object_manager=self.__object_manager)) return sources def __get__destinations(self): destinations = [] destination_params = {} for destination in self.job.get_destinations(): for param in destination["parameter"]: destination_params.update({param["name"]: param["value"]}) export_destination = ExportDestination( "cmdb.exportd.externals.external_systems.{}".format( destination["className"]), destination_params, self.exportvars) destinations.append(export_destination) return destinations def execute(self, event, user_id: int, user_name: str, log_flag: bool = True) -> ExportdHeader: # get cmdb objects from all sources cmdb_objects = set() exportd_header = ExportdHeader() for source in self.sources: cmdb_objects.update(source.get_objects()) # for every destination: do export for destination in self.destinations: external_system = destination.get_external_system() external_system.prepare_export() for cmdb_object in cmdb_objects: # setup objectdata for use in ExportVariable templates template_data = self.__get_objectdata(cmdb_object, 3) external_system.add_object(cmdb_object, template_data) exportd_header = external_system.finish_export() if log_flag: try: log_params = { 'job_id': self.job.get_public_id(), 'state': True, 'user_id': user_id, 'user_name': user_name, 'event': event.get_type(), 'message': external_system.msg_string, } self.log_manager.insert_log( action=LogAction.EXECUTE, log_type=ExportdJobLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) return exportd_header def __get_objectdata(self, cmdb_object, iteration): data = {} data["id"] = cmdb_object.object_information['object_id'] data["fields"] = {} for field in cmdb_object.fields: try: field_name = field["name"] if field["type"] == "ref" and field["value"] and iteration > 0: # resolve type iteration = iteration - 1 current_object = self.__object_manager.get_object( field["value"]) type_instance = self.__object_manager.get_type( current_object.get_type_id()) cmdb_render_object = CmdbRender( object_instance=current_object, type_instance=type_instance, render_user=None) data["fields"][field_name] = self.__get_objectdata( cmdb_render_object.result(), iteration) else: data["fields"][field_name] = field["value"] except Exception as err: LOGGER.error(err) return data
from typing import List from cmdb.utils import json_encoding from cmdb.utils.helpers import load_class from cmdb.utils.system_config import SystemConfigReader from cmdb.database.managers import DatabaseManagerMongo from cmdb.exporter.exporter_utils import ExperterUtils from cmdb.exporter.format.format_base import BaseExporterFormat from cmdb.exporter.config.config_type import ExporterConfigType from cmdb.framework.managers.type_manager import TypeManager from cmdb.framework.cmdb_object_manager import CmdbObjectManager from cmdb.framework.cmdb_render import RenderResult database_manager = DatabaseManagerMongo( **SystemConfigReader().get_all_values_from_section('Database')) object_manager = CmdbObjectManager(database_manager=database_manager) type_manager = TypeManager(database_manager=database_manager) class ZipExportType(BaseExporterFormat): FILE_EXTENSION = "zip" LABEL = "ZIP" MULTITYPE_SUPPORT = True ICON = "file-archive" DESCRIPTION = "Export Zipped Files" ACTIVE = True def export(self, data: List[RenderResult], *args): """ Export a zip file, containing the object list sorted by type in several files.