def delete(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() db_id = data.get('db_id', None) id_list = data.get('id_list', None) # 记录操作,不成功直接Pass try: with DBContext('w', None, True) as session: if db_id: data_info = session.query(DB).filter( DB.id == int(db_id)).all() for data in data_info: origion_data = model_to_dict(data) origion_data['create_time'] = str( origion_data['create_time']) origion_data['update_time'] = str( origion_data['update_time']) new_record = AssetOperationalAudit( username=nickname, request_object='数据库', request_host=origion_data.get('db_host'), request_method='删除', original_data=origion_data) session.add(new_record) elif id_list: for i in id_list: data_info = session.query(DB).filter(DB.id == i).all() for data in data_info: origion_data = model_to_dict(data) origion_data['create_time'] = str( origion_data['create_time']) origion_data['update_time'] = str( origion_data['update_time']) new_record = AssetOperationalAudit( username=nickname, request_object='数据库', request_host=origion_data.get('db_host'), request_method='删除', original_data=origion_data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) with DBContext('w', None, True) as session: if db_id: session.query(DB).filter(DB.id == int(db_id)).delete( synchronize_session=False) session.query(DBTag).filter(DBTag.db_id == int(db_id)).delete( synchronize_session=False) elif id_list: for i in id_list: session.query(DB).filter(DB.id == i).delete( synchronize_session=False) session.query(DBTag).filter(DBTag.db_id == i).delete( synchronize_session=False) else: return self.write(dict(code=1, msg='关键参数不能为空')) return self.write(dict(code=0, msg='删除成功'))
def delete(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) id = data.get('id') nickname = self.get_current_nickname() if not id: return self.write(dict(code=-2, msg='关键参数不能为空')) # 记录操作,不成功直接Pass try: with DBContext('w', None, True) as session: data_info = session.query(AssetIDC).filter( AssetIDC.id == id).all() for data in data_info: origion_data = model_to_dict(data) new_record = AssetOperationalAudit( username=nickname, request_object='IDC', request_host=origion_data.get('name'), request_method='删除', original_data=origion_data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) with DBContext('w', None, True) as session: session.query(AssetIDC).filter(AssetIDC.id == id).delete( synchronize_session=False) self.write(dict(code=0, msg='删除成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() # 列表包str格式,str空格切割 if not data: return self.write(dict(code=-1, msg='不能为空')) # 记录下操作,即使报错也不影响程序 try: with DBContext('w', None, True) as session: new_record = AssetOperationalAudit(username=nickname, request_object='主机', request_method='批量添加', modify_data=data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) # 判断下格式长度 with DBContext('w', None, True) as session: for i in data: if i: server_info = i.split(' ') ins_log.read_log( 'info', 'MultiServer:{server_info}'.format( server_info=server_info)) if len(server_info) != 4: return self.write(dict(code=-2, msg='格式错误')) hostname = server_info[0] ip = server_info[1] port = server_info[2] admin_user = server_info[3] if not check_ip(ip): return self.write(dict(code=-1, msg="IP格式不正确")) if not type(port) is int and int(port) >= 65535: return self.write(dict(code=-1, msg="端口格式不正确")) exist_id = session.query( Server.id).filter(Server.hostname == hostname).first() exist_ip = session.query( Server.id).filter(Server.ip == ip).first() if exist_id or exist_ip: return self.write( dict(code=-2, msg='IP:{ip}/Hostname:{hostname}已经存在,不要重复记录'. format(ip=ip, hostname=hostname))) new_server = Server(hostname=hostname, ip=ip, port=int(port), admin_user=admin_user) session.add(new_server) session.commit() return self.write(dict(code=0, msg='批量添加成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() db_id = data.get('id', None) db_code = data.get('db_code', None) db_host = data.get('db_host', None) db_port = data.get('db_port', 3306) db_user = data.get('db_user', None) db_pwd = data.get('db_pwd', None) db_env = data.get('db_env', None) proxy_host = data.get('proxy_host', None) db_type = data.get('db_type', 'mysql') db_version = data.get('db_version', None) db_mark = data.get('db_mark', '写') idc = data.get('idc', None) tag_list = data.get('tag_list', []) db_detail = data.get('db_detail', None) db_instance = data.get('db_instance', '') if not db_id or not db_code or not db_host or not db_port or not db_user: return self.write(dict(code=-1, msg='关键参数不能为空')) with DBContext('r') as session: old_password = session.query(DB.db_pwd).filter(DB.id == int(db_id)).first()[0] if old_password != db_pwd: # 加密密码 db_pwd = encrypt(db_pwd) with DBContext('w', None, True) as session: all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() session.query(DBTag).filter(DBTag.db_id == db_id).delete(synchronize_session=False) if all_tags: for tag_id in all_tags: session.add(DBTag(db_id=int(db_id), tag_id=tag_id[0])) session.query(DB).filter(DB.id == int(db_id)).update({DB.db_code: db_code, DB.db_host: db_host, DB.db_port: db_port, DB.db_user: db_user, DB.db_pwd: db_pwd, DB.db_env: db_env, DB.proxy_host: proxy_host, DB.db_type: db_type, DB.db_version: db_version, DB.idc: idc, DB.db_mark: db_mark, DB.db_detail: db_detail, DB.db_instance: db_instance }) # 记录操作,不成功直接Pass try: modify_data = data with DBContext('w', None, True) as session: data_info = session.query(DB).filter(DB.id == int(db_id)).all() for data in data_info: origion_data = model_to_dict(data) origion_data['create_time'] = str(origion_data['create_time']) origion_data['update_time'] = str(origion_data['update_time']) new_record = AssetOperationalAudit(username=nickname, request_object='数据库', request_host=db_host, request_method='更新', original_data=origion_data, modify_data=modify_data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) return self.write(dict(code=0, msg='编辑成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() server_id = data.get('id', None) hostname = data.get('hostname', None) ip = data.get('ip', None) public_ip = data.get('public_ip', None) port = data.get('port', None) idc = data.get('idc', None) admin_user = data.get('admin_user', None) region = data.get('region', None) tag_list = data.get('tag_list', []) detail = data.get('detail', None) if not hostname or not ip or not port: return self.write(dict(code=-1, msg='关键参数不能为空')) if not admin_user: return self.write(dict(code=-1, msg='管理用户不能为空')) if not check_ip(ip): return self.write(dict(code=-1, msg="IP格式不正确")) if not type(port) is int and int(port) >= 65535: return self.write(dict(code=-1, msg="端口格式不正确")) with DBContext('w', None, True) as session: exist_hostname = session.query(Server.hostname).filter(Server.id == server_id).first() if exist_hostname[0] != hostname: return self.write(dict(code=-2, msg='主机名不能修改')) session.query(ServerTag).filter(ServerTag.server_id == server_id).delete(synchronize_session=False) all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() if all_tags: for tag_id in all_tags: session.add(ServerTag(server_id=server_id, tag_id=tag_id[0])) session.query(Server).filter(Server.id == server_id).update({Server.hostname: hostname, Server.ip: ip, Server.port: int(port), Server.public_ip: public_ip, Server.idc: idc, Server.admin_user: admin_user, Server.region: region, Server.detail: detail }) # Server.state: 'new' # 记录操作,不成功直接Pass try: modify_data = data with DBContext('w', None, True) as session: data_info = session.query(Server).filter(Server.id == int(server_id)).all() for data in data_info: origion_data = model_to_dict(data) origion_data['create_time'] = str(origion_data['create_time']) origion_data['update_time'] = str(origion_data['update_time']) new_record = AssetOperationalAudit(username=nickname, request_object='主机', request_host=ip, request_method='更新', original_data=origion_data, modify_data=modify_data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) return self.write(dict(code=0, msg='编辑成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() id = data.get('id', None) name = data.get('name', None) contact = data.get('contact', None) email = data.get('email', None) phone = data.get('phone', None) address = data.get('address', None) network = data.get('network', None) bandwidth = data.get('bandwidth', None) ip_range = data.get('ip_range', None) remarks = data.get('remarks', None) if not id or not name: return self.write(dict(code=-2, msg='关键参数不能为空')) if email and not is_mail(email): return self.write(dict(code=-2, msg='Email格式不正确')) if phone and not is_tel(phone): return self.write(dict(code=-2, msg='Phone格式不正确')) with DBContext('r') as session: exist_name = session.query(AssetIDC.name).filter(AssetIDC.id == id).first() if name != exist_name[0]: return self.write(dict(code=-2, msg='名称不能修改')) # 记录操作,不成功直接Pass try: modify_data = data with DBContext('w', None, True) as session: data_info = session.query(AssetIDC).filter(AssetIDC.id == id).all() for data in data_info: origion_data = model_to_dict(data) new_record = AssetOperationalAudit(username=nickname, request_object='IDC', request_host=name, request_method='更新', original_data=origion_data, modify_data=modify_data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) update_info = { "contact": contact, "email": email, "phone": phone, "address": address, "network": network, "bandwidth": bandwidth, "ip_range": ip_range, "remarks": remarks, } with DBContext('w', None, True) as session: session.query(AssetIDC).filter(AssetIDC.name == name).update(update_info) self.write(dict(code=0, msg='更新成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() hostname = data.get('hostname', None) ip = data.get('ip', None) port = data.get('port', 22) public_ip = data.get('public_ip', None) idc = data.get('idc', None) admin_user = data.get('admin_user', None) region = data.get('region', None) state = data.get('state', 'new') tag_list = data.get('tag_list', []) detail = data.get('detail', None) if not hostname or not ip or not port: return self.write(dict(code=-1, msg='关键参数不能为空')) if not admin_user: return self.write(dict(code=-1, msg='管理用户不能为空')) if not check_ip(ip): return self.write(dict(code=-1, msg="IP格式不正确")) if not type(port) is int and int(port) >= 65535: return self.write(dict(code=-1, msg="端口格式不正确")) with DBContext('r') as session: exist_id = session.query(Server.id).filter(Server.hostname == hostname).first() exist_ip = session.query(Server.id).filter(Server.ip == ip).first() if exist_id or exist_ip: return self.write(dict(code=-2, msg='不要重复记录')) with DBContext('w', None, True) as session: new_server = Server(hostname=hostname, ip=ip, public_ip=public_ip, port=int(port), idc=idc, admin_user=admin_user, region=region, state=state, detail=detail) session.add(new_server) all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() # print('all_tags', all_tags) if all_tags: for tag_id in all_tags: session.add(ServerTag(server_id=new_server.id, tag_id=tag_id[0])) # 记录,记录错误也不要影响用户正常添加 try: with DBContext('w', None, True) as session: new_record = AssetOperationalAudit(username=nickname, request_object='主机', request_host=ip, request_method='新增', modify_data=data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) return self.write(dict(code=0, msg='添加成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() db_code = data.get('db_code', None) db_host = data.get('db_host', None) db_port = data.get('db_port', 3306) db_user = data.get('db_user', None) db_pwd = data.get('db_pwd', None) db_env = data.get('db_env', None) proxy_host = data.get('proxy_host', None) db_type = data.get('db_type', 'mysql') db_version = data.get('db_version', None) db_mark = data.get('db_mark', '写') tag_list = data.get('tag_list', []) db_detail = data.get('db_detail', None) db_instance = data.get('db_instance', '') if not db_code or not db_host or not db_port or not db_user: return self.write(dict(code=-1, msg='关键参数不能为空')) with DBContext('r') as session: exist_id = session.query(DB.id).filter(DB.db_code == db_code, DB.db_host == db_host, DB.db_port == db_port, DB.db_user == db_user, DB.db_env == db_env, DB.proxy_host == proxy_host, DB.db_type == db_type, db_version == db_version, DB.db_mark == db_mark).first() if exist_id: return self.write(dict(code=-2, msg='不要重复记录')) # 加密密码 db_pwd = encrypt(db_pwd) with DBContext('w', None, True) as session: new_db = DB(db_code=db_code, db_host=db_host, db_port=db_port, db_user=db_user, db_pwd=db_pwd, db_env=db_env, proxy_host=proxy_host, db_type=db_type, db_version=db_version, db_mark=db_mark, db_detail=db_detail, db_instance=db_instance) session.add(new_db) all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() if all_tags: for tag_id in all_tags: session.add(DBTag(db_id=new_db.id, tag_id=tag_id[0])) # 记录,记录错误也不要影响用户正常添加 try: with DBContext('w', None, True) as session: new_record = AssetOperationalAudit(username=nickname, request_object='数据库', request_host=db_host, request_method='新增', modify_data=data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) return self.write(dict(code=0, msg='添加成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() # 列表包str格式,str空格切割 if not data: return self.write(dict(code=-1, msg='不能为空')) # 记录下操作,即使报错也不影响程序 try: with DBContext('w', None, True) as session: new_record = AssetOperationalAudit(username=nickname, request_object='数据库', request_method='批量添加', modify_data=data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) # 判断下格式长度 with DBContext('w', None, True) as session: for i in data: if i: db_info = i.split(' ') ins_log.read_log('info', 'MultiDB:{db_info}'.format(db_info=db_info)) if len(db_info) != 6: return self.write(dict(code=-2, msg='格式错误')) db_type = db_info[0] db_name = db_info[1] db_host = db_info[2] db_port = db_info[3] db_user = db_info[4] db_passwd = db_info[5] if not type(db_port) is int and int(db_port) >= 65535: return self.write(dict(code=-1, msg="端口格式不正确")) exist_id = session.query(DB.id).filter(DB.db_code == db_name).first() exist_ip = session.query(DB.id).filter(DB.db_host == db_host).first() if exist_id or exist_ip: return self.write( dict(code=-2, msg='IP:{address}/Hostname:{hostname}已经存在,不要重复记录'.format(address=db_host, hostname=db_name))) new_db = DB(db_code=db_name, db_host=db_host, db_port=db_port, db_user=db_user, db_type=db_type, db_pwd=db_passwd, state='Handle') session.add(new_db) session.commit() return self.write(dict(code=0, msg='批量添加成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() name = data.get('name', None) contact = data.get('contact', None) email = data.get('email', None) phone = data.get('phone', None) address = data.get('address', None) network = data.get('network', None) bandwidth = data.get('bandwidth', None) ip_range = data.get('ip_range', None) remarks = data.get('remarks', None) if not name: return self.write(dict(code=-2, msg='关键参数不能为空')) if email and not is_mail(email): return self.write(dict(code=-2, msg='Email格式不正确')) if phone and not is_tel(phone): return self.write(dict(code=-2, msg='Phone格式不正确')) with DBContext('r') as session: exist_name = session.query(AssetIDC.id).filter(AssetIDC.name == name).first() count = session.query(AssetIDC).count() if exist_name: return self.write(dict(code=-2, msg='不要重复记录')) if count > 50: return self.write(dict(code=-2, msg='IDC最大只允许50个')) with DBContext('w', None, True) as session: new_idc = AssetIDC(name=name, contact=contact, email=email, phone=phone, address=address, network=network, bandwidth=bandwidth, ip_range=ip_range, remarks=remarks) session.add(new_idc) # 记录,记录错误也不要影响用户正常添加 try: with DBContext('w', None, True) as session: new_record = AssetOperationalAudit(username=nickname, request_object='IDC', request_host=name, request_method='新增', modify_data=data) session.add(new_record) except Exception as err: ins_log.read_log('error', 'operational_audit error:{err}'.format(err=err)) return self.write(dict(code=0, msg='添加成功'))