def delete_many_objects(public_ids, request_user: User): try: ids = [] operator_in = {'$in': []} filter_public_ids = {'public_id': {}} for v in public_ids.split(","): try: ids.append(int(v)) except (ValueError, TypeError): return abort(400) operator_in.update({'$in': ids}) filter_public_ids.update({'public_id': operator_in}) ack = [] objects = object_manager.get_objects_by(**filter_public_ids) for current_object_instance in objects: try: current_type_instance = object_manager.get_type(current_object_instance.get_type_id()) current_object_render_result = CmdbRender(object_instance=current_object_instance, type_instance=current_type_instance, render_user=request_user, user_list=user_manager.get_users()).result() except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) except RenderError as err: LOGGER.error(err) return abort(500) try: ack.append(object_manager.delete_object(public_id=current_object_instance.get_public_id(), request_user=request_user)) except ObjectDeleteError: return abort(400) except CMDBError: return abort(500) try: # generate log log_data = { 'object_id': current_object_instance.get_public_id(), 'version': current_object_render_result.object_information['version'], 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'comment': 'Object was deleted', 'render_state': json.dumps(current_object_render_result, default=default).encode('UTF-8') } log_manager.insert_log(action=LogAction.DELETE, log_type=CmdbObjectLog.__name__, **log_data) except (CMDBError, LogManagerInsertError) as err: LOGGER.error(err) resp = make_response({'successfully': ack}) return resp except ObjectDeleteError as e: return jsonify(message='Delete Error', error=e.message) except CMDBError: return abort(500)
def insert_object(request_user: User): from bson import json_util from datetime import datetime add_data_dump = json.dumps(request.json) try: new_object_data = json.loads(add_data_dump, object_hook=json_util.object_hook) if not 'public_id' in new_object_data: new_object_data['public_id'] = object_manager.get_new_id(CmdbObject.COLLECTION) if not 'active' in new_object_data: new_object_data['active'] = True new_object_data['creation_time'] = datetime.utcnow() new_object_data['views'] = 0 new_object_data['version'] = '1.0.0' # default init version except TypeError as e: LOGGER.warning(e) abort(400) try: new_object_id = object_manager.insert_object(new_object_data) except ObjectInsertError as oie: LOGGER.error(oie) return abort(500) # get current object state try: current_type_instance = object_manager.get_type(new_object_data['type_id']) current_object = object_manager.get_object(new_object_id) current_object_render_result = CmdbRender(object_instance=current_object, type_instance=current_type_instance, render_user=request_user, user_list=user_manager.get_users()).result() except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) except RenderError as err: LOGGER.error(err) return abort(500) # Generate new insert log try: log_params = { 'object_id': new_object_id, 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'comment': 'Object was created', 'render_state': json.dumps(current_object_render_result, default=default).encode('UTF-8'), 'version': current_object.version } log_ack = log_manager.insert_log(action=LogAction.CREATE, log_type=CmdbObjectLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) resp = make_response(new_object_id) return resp
def update_object_state(public_id: int, request_user: User): if isinstance(request.json, bool): state = request.json else: return abort(400) try: founded_object = object_manager.get_object(public_id=public_id) except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) if founded_object.active == state: return make_response(False, 204) try: founded_object.active = state update_ack = object_manager.update_object(founded_object, request_user) except ObjectManagerUpdateError as err: LOGGER.error(err) return abort(500) # get current object state try: current_type_instance = object_manager.get_type(founded_object.get_type_id()) current_object_render_result = CmdbRender(object_instance=founded_object, type_instance=current_type_instance, render_user=request_user, user_list=user_manager.get_users()).result() except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) except RenderError as err: LOGGER.error(err) return abort(500) try: # generate log change = { 'old': not state, 'new': state } log_data = { 'object_id': public_id, 'version': founded_object.version, 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'render_state': json.dumps(current_object_render_result, default=default).encode('UTF-8'), 'comment': 'Active status has changed', 'changes': change, } log_manager.insert_log(action=LogAction.ACTIVE_CHANGE, log_type=CmdbObjectLog.__name__, **log_data) except (CMDBError, LogManagerInsertError) as err: LOGGER.error(err) return make_response(update_ack)
def worker(job: ExportdJob, request_user: User): from flask import make_response as make_res from cmdb.exportd.exporter_base import ExportdManagerBase from cmdb.event_management.event import Event try: event = Event("cmdb.exportd.pull") content = ExportdManagerBase(job).execute(event, request_user.public_id, request_user.get_name(), False) response = make_res(content.data, content.status) response.headers['Content-Type'] = '%s; charset=%s' % (content.mimetype, content.charset) return response except Exception as err: LOGGER.error(err) return abort(404)
def add_job(request_user: User): from bson import json_util add_data_dump = json.dumps(request.json) try: new_job_data = json.loads(add_data_dump, object_hook=json_util.object_hook) new_job_data['public_id'] = exportd_manager.get_new_id(ExportdJob.COLLECTION) new_job_data['last_execute_date'] = datetime.utcnow() new_job_data['author_id'] = request_user.get_public_id() new_job_data['author_name'] = request_user.get_name() new_job_data['state'] = ExecuteState.SUCCESSFUL.name except TypeError as e: LOGGER.warning(e) abort(400) try: job_instance = ExportdJob(**new_job_data) except CMDBError as e: LOGGER.debug(e) return abort(400) try: ack = exportd_manager.insert_job(job_instance) except ExportdJobManagerInsertError: return abort(500) # Generate new insert log try: log_params = { 'job_id': job_instance.get_public_id(), 'state': True, 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'event': LogAction.CREATE.name, 'message': '', } log_manager.insert_log(action=LogAction.CREATE, log_type=ExportdJobLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) resp = make_response(ack) return resp
def delete_object(public_id: int, request_user: User): try: current_object_instance = object_manager.get_object(public_id) current_type_instance = object_manager.get_type( current_object_instance.get_type_id()) current_object_render_result = CmdbRender( object_instance=current_object_instance, type_instance=current_type_instance, render_user=request_user, user_list=user_manager.get_users()).result() except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) except RenderError as err: LOGGER.error(err) return abort(500) try: ack = object_manager.delete_object(public_id=public_id, request_user=request_user) except ObjectDeleteError: return abort(400) except CMDBError: return abort(500) try: # generate log log_data = { 'object_id': public_id, 'version': current_object_render_result.object_information['version'], 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'comment': 'Object was deleted', 'render_state': json.dumps(current_object_render_result, default=default).encode('UTF-8') } log_manager.insert_log(action=LogAction.DELETE, log_type=CmdbObjectLog.__name__, **log_data) except (CMDBError, LogManagerInsertError) as err: LOGGER.error(err) resp = make_response(ack) return resp
def delete_job(public_id: int, request_user: User): try: try: job_instance = exportd_manager.get_job(public_id) log_params = { 'job_id': job_instance.get_public_id(), 'state': True, 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'event': LogAction.DELETE.name, 'message': '', } log_manager.insert_log(action=LogAction.DELETE, log_type=ExportdJobLog.__name__, **log_params) except (ExportdJobManagerGetError, LogManagerInsertError) as err: LOGGER.error(err) return abort(404) ack = exportd_manager.delete_job(public_id=public_id, request_user=request_user) except ExportdJobManagerDeleteError: return abort(400) except CMDBError: return abort(500) resp = make_response(ack) return resp
def update_job(request_user: User): from bson import json_util add_data_dump = json.dumps(request.json) new_job_data = None try: new_job_data = json.loads(add_data_dump, object_hook=json_util.object_hook) except TypeError as e: LOGGER.warning(e) abort(400) try: state = new_job_data["state"] update_job_instance = ExportdJob(**new_job_data) except CMDBError: return abort(400) try: exportd_manager.update_job(update_job_instance, request_user, False) except ExportdJobManagerUpdateError: return abort(500) # Generate new insert log if state not in ExecuteState.RUNNING.name: try: log_params = { 'job_id': update_job_instance.get_public_id(), 'state': True, 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'event': LogAction.EDIT.name, 'message': '', } log_manager.insert_log(action=LogAction.EDIT, log_type=ExportdJobLog.__name__, **log_params) except LogManagerInsertError as err: LOGGER.error(err) resp = make_response(update_job_instance) return resp
def update_object(public_id: int, request_user: User): object_ids = request.args.getlist('objectIDs') if len(object_ids) > 0: object_ids = list(map(int, object_ids)) else: object_ids = [public_id] update_ack = None for obj_id in object_ids: # get current object state try: current_object_instance = object_manager.get_object(obj_id) current_type_instance = object_manager.get_type( current_object_instance.get_type_id()) current_object_render_result = CmdbRender( object_instance=current_object_instance, type_instance=current_type_instance, render_user=request_user, user_list=user_manager.get_users()).result() except ObjectManagerGetError as err: LOGGER.error(err) return abort(404) except RenderError as err: LOGGER.error(err) return abort(500) update_comment = '' # load put data try: # get data as str add_data_dump = json.dumps(request.json) # convert into python dict put_data = json.loads(add_data_dump, object_hook=object_hook) # check for comment try: put_data['public_id'] = obj_id put_data[ 'creation_time'] = current_object_instance.creation_time put_data['author_id'] = current_object_instance.author_id old_fields = list( map( lambda x: {k: v for k, v in x.items() if k in ['name', 'value']}, current_type_instance.get_fields())) new_fields = put_data['fields'] for item in new_fields: for old in old_fields: if item['name'] == old['name']: old['value'] = item['value'] put_data['fields'] = old_fields if 'active' not in put_data: put_data['active'] = current_object_instance.active if 'version' not in put_data: put_data['version'] = current_object_instance.version update_comment = put_data['comment'] del put_data['comment'] except (KeyError, IndexError, ValueError): update_comment = '' except TypeError as e: LOGGER.warning(e) return abort(400) # update edit time put_data['last_edit_time'] = datetime.utcnow() try: update_object_instance = CmdbObject(**put_data) except ObjectManagerUpdateError as err: LOGGER.error(err) return abort(400) # calc version changes = current_object_instance / update_object_instance if len(changes['new']) == 1: update_object_instance.update_version( update_object_instance.VERSIONING_PATCH) elif len(changes['new']) == len(update_object_instance.fields): update_object_instance.update_version( update_object_instance.VERSIONING_MAJOR) elif len(changes['new']) > (len(update_object_instance.fields) / 2): update_object_instance.update_version( update_object_instance.VERSIONING_MINOR) else: update_object_instance.update_version( update_object_instance.VERSIONING_PATCH) # insert object try: update_ack = object_manager.update_object(update_object_instance, request_user) except CMDBError as e: LOGGER.warning(e) return abort(500) try: # generate log log_data = { 'object_id': obj_id, 'version': current_object_render_result.object_information['version'], 'user_id': request_user.get_public_id(), 'user_name': request_user.get_name(), 'comment': update_comment, 'changes': changes, 'render_state': json.dumps(current_object_render_result, default=default).encode('UTF-8') } log_manager.insert_log(action=LogAction.EDIT, log_type=CmdbObjectLog.__name__, **log_data) except (CMDBError, LogManagerInsertError) as err: LOGGER.error(err) return make_response(update_ack)