def update_template(self, data: (dict, DocapiTemplate), request_user: User) -> str: """ Update new DocapiTemplat Object Args: data: init data request_user: current user, to detect who triggered event Returns: Public ID of the DocapiTemplate in database """ if isinstance(data, dict): update_object = DocapiTemplate(**data) elif isinstance(data, DocapiTemplate): update_object = data else: raise DocapiTemplateManagerUpdateError(f'Could not update template with ID: {data.get_public_id()}') update_object.last_execute_date = datetime.utcnow() ack = self._update( collection=DocapiTemplate.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database() ) if self._event_queue: event = Event("cmdb.docapi.updated", {"id": update_object.get_public_id(), "active": update_object.get_active(), "user_id": request_user.get_public_id()}) self._event_queue.put(event) return ack.acknowledged
def update_job(self, data: (dict, ExportdJob), request_user: User, event_start=True) -> str: """ Update new ExportdJob Object Args: data: init data request_user: current user, to detect who triggered event event_start: Controls whether an event should be started Returns: Public ID of the ExportdJob in database """ if isinstance(data, dict): update_object = ExportdJob(**data) elif isinstance(data, ExportdJob): update_object = data else: raise ExportdJobManagerUpdateError(f'Could not update job with ID: {data.get_public_id()}') update_object.last_execute_date = datetime.utcnow() ack = self._update( collection=ExportdJob.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database() ) if self._event_queue and event_start: event = Event("cmdb.exportd.updated", {"id": update_object.get_public_id(), "active": update_object.scheduling["event"]["active"] and update_object.get_active(), "user_id": request_user.get_public_id()}) self._event_queue.put(event) return ack.acknowledged
def update_file(request_user: User): try: import json from bson import json_util add_data_dump = json.dumps(request.json) new_file_data = json.loads(add_data_dump, object_hook=json_util.object_hook) # Check if file exists curr_file = media_file_manager.get_media_file_by_public_id( new_file_data['public_id'])._file curr_file['public_id'] = new_file_data['public_id'] curr_file['filename'] = new_file_data['filename'] curr_file['metadata'] = new_file_data['metadata'] curr_file['metadata']['author_id'] = new_file_data['metadata'][ 'author_id'] = request_user.get_public_id() ack = media_file_manager.updata_media_file(curr_file) except ObjectInsertError: return abort(500) resp = make_response(ack) return resp
def run_job_manual(self, public_id: int, request_user: User) -> bool: if self._event_queue: event = Event("cmdb.exportd.run_manual", { "id": public_id, "user_id": request_user.get_public_id() }) self._event_queue.put(event) return True
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 delete_template(self, public_id: int, request_user: User) -> bool: try: ack = self._delete(collection=DocapiTemplate.COLLECTION, public_id=public_id) if self._event_queue: event = Event("cmdb.docapi.deleted", {"id": public_id, "active": False, "user_id": request_user.get_public_id()}) self._event_queue.put(event) return ack except Exception: raise DocapiTemplateManagerDeleteError(f'Could not delete template with ID: {public_id}')
def delete_many_objects(self, filter_query: dict, public_ids, request_user: User): ack = self._delete_many(CmdbObject.COLLECTION, filter_query) if self._event_queue: event = Event("cmdb.core.objects.deleted", { "ids": public_ids, "user_id": request_user.get_public_id() }) self._event_queue.put(event) return ack
def delete_job(self, public_id: int, request_user: User) -> bool: try: ack = self._delete(collection=ExportdJob.COLLECTION, public_id=public_id) if self._event_queue: event = Event("cmdb.exportd.deleted", {"id": public_id, "active": False, "user_id": request_user.get_public_id()}) self._event_queue.put(event) return ack except Exception: raise ExportdJobManagerDeleteError(f'Could not delete job with ID: {public_id}')
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 delete_user(public_id: int, request_user: User): if public_id == request_user.get_public_id(): return abort(403, 'You cant delete yourself!') try: user_manager.get_user(public_id) except UserManagerGetError: return abort(404) try: ack = user_manager.delete_user(public_id=public_id) except UserManagerDeleteError: return abort(400) return make_response(ack)
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 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(self, public_id: int, request_user: User): try: if self._event_queue: event = Event( "cmdb.core.object.deleted", { "id": public_id, "type_id": self.get_object(public_id).get_type_id(), "user_id": request_user.get_public_id() }) self._event_queue.put(event) ack = self._delete(CmdbObject.COLLECTION, public_id) return ack except (CMDBError, Exception): raise ObjectDeleteError(msg=public_id)
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 update_object(self, data: (dict, CmdbObject), request_user: User) -> str: if isinstance(data, dict): update_object = CmdbObject(**data) elif isinstance(data, CmdbObject): update_object = data else: raise ObjectManagerUpdateError( 'Wrong CmdbObject init format - expecting CmdbObject or dict') update_object.last_edit_time = datetime.utcnow() ack = self._update(collection=CmdbObject.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database()) # create cmdb.core.object.updated event if self._event_queue and request_user: event = Event( "cmdb.core.object.updated", { "id": update_object.get_public_id(), "type_id": update_object.get_type_id(), "user_id": request_user.get_public_id() }) self._event_queue.put(event) return ack.acknowledged
def add_template(request_user: User): from bson import json_util add_data_dump = json.dumps(request.json) try: new_tpl_data = json.loads(add_data_dump, object_hook=json_util.object_hook) new_tpl_data['public_id'] = docapi_tpl_manager.get_new_id() new_tpl_data['author_id'] = request_user.get_public_id() except TypeError as err: LOGGER.warning(err) abort(400) try: template_instance = DocapiTemplate(**new_tpl_data) except CMDBError as err: LOGGER.debug(err) return abort(400) try: ack = docapi_tpl_manager.insert_template(template_instance) except DocapiTemplateManagerInsertError: return abort(500) 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)