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 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, 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 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): 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 __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 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 __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)
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 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
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 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
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):
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.
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