def get_system_user(self): """ 获取系统用户,若查询到2个,就只return一个优先级高的 :return: """ system_user_list = [] with DBContext('r') as session: system_user_cont = session.query(SystemUser).count() if system_user_cont == 1: #只有一个用户,直接返回,用这个作为跳板的用户 system_user_data = session.query(SystemUser).all() for data in system_user_data: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) if data_dict['platform_users']: data_dict['platform_users'] = data_dict.get('platform_users', '').split(',') system_user_list.append(data_dict) else: priority_max_info = session.query(func.max(SystemUser.priority)).all() # return is list[tuple] priority_max_num = priority_max_info[0][0] system_user_data = session.query(SystemUser).filter(SystemUser.priority == priority_max_num).first() data_dict = model_to_dict(system_user_data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) if data_dict['platform_users']: data_dict['platform_users'] = data_dict.get('platform_users', '').split(',') system_user_list.append(data_dict) #print(system_user_list) return system_user_list
def get(self, *args, **kwargs): # nickname = self.get_current_nickname() id = self.get_argument('id', default=None, strip=True) if not id: return self.write(dict(code=-1, msg='关键参数不能为空')) server_list = [] admin_user_list = [] connect_server_info_list = [] with DBContext('r') as session: server_info = session.query(Server).filter( Server.id == id).filter() for msg in server_info: data_dict = model_to_dict(msg) server_list.append(data_dict) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) if not server_list: return self.write(dict(code=-1, msg='没有发现IP')) admin_user = server_list[0]['admin_user'] if not admin_user: return self.write(dict(code=-1, msg='此机器没有配置管理用户,请先配置管理用户')) admin_user_info = session.query(AdminUser).filter( AdminUser.admin_user == admin_user).filter() for admin_user_msg in admin_user_info: data_dict = model_to_dict(admin_user_msg) data_dict['update_time'] = str(data_dict['update_time']) admin_user_list.append(data_dict) try: connect_server_info = dict() connect_server_info['ip'] = server_list[0].get('ip') connect_server_info['public_ip'] = server_list[0].get( 'public_ip') connect_server_info['private_ip'] = server_list[0].get( 'private_ip') connect_server_info['port'] = server_list[0].get('port') connect_server_info['admin_user'] = server_list[0].get( 'admin_user') connect_server_info['system_user'] = admin_user_list[0].get( 'system_user') connect_server_info['user_key'] = admin_user_list[0].get( 'user_key') connect_server_info_list.append(connect_server_info) except (IndexError, KeyError) as err: ins_log.read_log('error', '解析的时候出错:{err}'.format(err=err)) return False return self.write( dict(code=0, msg='获取成功', data=connect_server_info_list))
def delete(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) nickname = self.get_current_nickname() server_id = data.get('server_id', None) id_list = data.get('id_list', None) # 记录操作,不成功直接Pass try: with DBContext('w', None, True) as session: if server_id: 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=origion_data.get('ip'), request_method='删除', original_data=origion_data) session.add(new_record) elif id_list: for i in id_list: data_info = session.query(Server).filter(Server.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('ip'), 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 server_id: server_info = session.query(Server).filter(Server.id == int(server_id)).first() session.query(ServerDetail).filter(ServerDetail.ip == server_info.ip).delete(synchronize_session=False) session.query(Server).filter(Server.id == int(server_id)).delete(synchronize_session=False) session.query(ServerTag).filter(ServerTag.server_id == int(server_id)).delete(synchronize_session=False) elif id_list: for i in id_list: server_info = session.query(Server).filter(Server.id == i).first() session.query(ServerDetail).filter(ServerDetail.ip == server_info.ip).delete( synchronize_session=False) session.query(Server).filter(Server.id == i).delete(synchronize_session=False) session.query(ServerTag).filter(ServerTag.server_id == i).delete(synchronize_session=False) else: return self.write(dict(code=1, msg='关键参数不能为空')) return self.write(dict(code=0, msg='删除成功'))
def get(self, *args, **kwargs): data_list = [] data_list2 = [] with DBContext('r') as session: todata = session.query(Storage).filter().order_by( Storage.create_time.desc()).all() for msg in todata: temp_dict = {} data_dict = model_to_dict(msg) temp_dict["id"] = data_dict["id"] temp_dict["name"] = data_dict["name"] temp_dict["mode"] = data_dict["mode"] temp_dict["consume"] = data_dict["consume"] temp_dict["dictvalue"] = str(data_dict["dictvalue"]) # temp_dict["authorized"] = str(data_dict["authorized"]) ins_log.read_log('info', "800000000000000000000000000000000000") ins_log.read_log('info', data_dict["mode"]) ins_log.read_log('info', "800000000000000000000000000000000000") if data_dict["mode"] == "定时": data_list.append(temp_dict) if data_dict["mode"] == "触发": data_list2.append(temp_dict) if len(data_list) > 0 or len(data_list2) > 0: self.write( dict(code=0, msg='获取成功', data=data_list, data2=data_list2)) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): data_list = [] data_list2 = [] temp_list = [] temp_list2 = [] department = self.get_argument('department', strip=True) with DBContext('r') as session: conditions = [] department = "'" + department + "'" conditions.append( AssetSql.department.like('%{}%'.format(department))) todata = session.query(AssetSql).filter(*conditions).all() for msg in todata: case_dict = {} data_dict = model_to_dict(msg) case_dict["id"] = data_dict["id"] case_dict["obj"] = data_dict["obj"] case_dict["name"] = data_dict["name"] case_dict["department"] = data_dict["department"] case_dict["storage"] = data_dict["storage"] case_dict["storage2"] = data_dict["storage2"] case_dict["dictvalue"] = data_dict["dictvalue"] case_dict["dictvalue2"] = data_dict["dictvalue2"] case_dict["flag"] = data_dict["flag"] case_dict["authorized"] = data_dict["authorized"] case_dict["fieldname"] = data_dict["fieldname"] case_dict["fieldlist"] = data_dict["fieldlist"] if case_dict["obj"] not in temp_list: temp_list.append(case_dict["obj"]) data_list.append({ "v": case_dict["obj"], "t": case_dict["flag"] }) data_list2.append(case_dict) for i in temp_list: temp_list2.append({"name": i, "date": []}) for j in temp_list2: for d in data_list2: if str(j["name"]) == str(d["obj"]): j["date"].append({ "k": d["id"], "v": d["name"], "t": d["flag"], "s": d["storage"], "st": d["storage2"], "d": d["fieldlist"], "f": d["fieldname"], "c": d["dictvalue"] }) if len(data_list) > 0: self.write( dict(code=0, msg='获取成功', data=data_list, storagelist=temp_list2)) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): data_list = [] department = self.get_argument('department', strip=True) obj = self.get_argument('obj', strip=True) with DBContext('r') as session: conditions = [] # conditions.append(AssetSql.department == department) department = "'" + department + "'" conditions.append( AssetSql.department.like('%{}%'.format(department))) conditions.append(AssetSql.obj == obj) todata = session.query(AssetSql).filter(*conditions).all() for msg in todata: case_dict = {} data_dict = model_to_dict(msg) case_dict["id"] = data_dict["id"] case_dict["obj"] = data_dict["obj"] case_dict["name"] = data_dict["name"] case_dict["department"] = data_dict["department"] case_dict["storage"] = data_dict["storage"] data_list.append({ "k": case_dict["id"], "v": case_dict["name"], "n": case_dict["storage"] }) if len(data_list) > 0: self.write(dict(code=0, msg='获取成功', data=data_list)) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): data_list = [] value = int(self.get_argument('value', default=None, strip=True)) with DBContext('r') as session: todata = session.query(AssetSql).filter(AssetSql.id == value).all() # tocount = session.query(AssetSql).filter().count() for msg in todata: case_dict = {} data_dict = model_to_dict(msg) case_dict["id"] = data_dict["id"] case_dict["name"] = data_dict["name"] case_dict["header"] = data_dict["header"] case_dict["dbname_id"] = data_dict["dbname_id"] case_dict["dbname"] = data_dict["dbname"] case_dict["totype"] = data_dict["totype"] case_dict["sqlstr"] = data_dict["sqlstr"] case_dict["remarks"] = data_dict["remarks"] case_dict["username"] = data_dict["username"] case_dict["obj"] = data_dict["obj"] case_dict["department"] = data_dict["department"] case_dict["storage"] = data_dict["storage"] case_dict["storage2"] = data_dict["storage2"] case_dict["mode"] = data_dict["mode"] case_dict["state"] = data_dict["state"] case_dict["fieldname"] = data_dict["fieldname"] case_dict["create_time"] = str(data_dict["create_time"]) data_list.append(case_dict) if len(data_list) > 0: self.write(dict(code=0, msg='获取成功', data=data_list)) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
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 get(self, *args, **kwargs): data_list = [] with DBContext('r') as session: todata = session.query(Record).filter().order_by( Record.create_time.desc()).all() for msg in todata: temp_dict = {} data_dict = model_to_dict(msg) temp_dict["id"] = data_dict["id"] temp_dict["recordname"] = data_dict["recordname"] temp_dict["recordlist"] = data_dict["recordlist"] temp_dict["zhname"] = data_dict["zhname"] temp_dict["username"] = data_dict["username"] temp_dict["tablename"] = str(data_dict["tablename"]) temp_dict["number"] = str(data_dict["number"]) data_list.append(temp_dict) if len(data_list) > 0 > 0: self.write(dict( code=0, msg='获取成功', data=data_list, )) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): nickname = self.get_current_nickname() _tree = [{"expand": True, "title": 'root', "children": []}] with DBContext('r', None, True) as session: if self.is_superuser: # TODO 超管看所有Tag Tree db_tags = session.query(Tag).order_by(Tag.id).all() else: # TODO 普通用户看有权限的TAG Tree db_tags = session.query(Tag).order_by(Tag.id).filter( Tag.users.like('%{}%'.format(nickname))) for msg in db_tags: server_dict = {} data_dict = model_to_dict(msg) server_tags = session.query(ServerTag.id).outerjoin( Server, Server.id == ServerTag.server_id).filter( ServerTag.tag_id == msg.id).all() server_dict['the_len'] = len(server_tags) server_dict['title'] = data_dict['tag_name'] + ' ({})'.format( len(server_tags)) # print(server_dict['title']) server_dict['tag_name'] = data_dict['tag_name'] # print(server_dict['tag_name']) server_dict['node'] = 'root' _tree[0]["children"].append(server_dict) self.write(dict(code=0, msg='获取成功', data=_tree))
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=15, strip=True) limit_start = (int(page_size) - 1) * int(limit) admin_user_list = [] with DBContext('w') as session: if key and value: count = session.query(AdminUser).filter_by(**{ key: value }).count() admin_user_data = session.query(AdminUser).filter_by(**{ key: value }).order_by(AdminUser.id).offset(limit_start).limit(int(limit)) else: count = session.query(AdminUser).count() admin_user_data = session.query(AdminUser).order_by( AdminUser.id).offset(limit_start).limit(int(limit)) for data in admin_user_data: data_dict = model_to_dict(data) data_dict['update_time'] = str(data_dict['update_time']) admin_user_list.append(data_dict) return self.write( dict(code=0, msg='获取成功', count=count, data=admin_user_list))
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 get(self, *args, **kwargs): data_list = [] username = str(self.get_argument('username', default=None)) #用戶名 tablename = str(self.get_argument('tablename', default=None)) #數據表名 with DBContext('r') as session: conditions = [] conditions.append(Record.username == username) conditions.append(Record.tablename == tablename) todata = session.query(Record).filter(*conditions).order_by( Record.number.desc()).all() # todata = session.query(Record).filter(*conditions).all() for msg in todata: temp_dict = {} data_dict = model_to_dict(msg) temp_dict["id"] = data_dict["id"] temp_dict["recordname"] = data_dict["recordname"] temp_dict["recordlist"] = data_dict["recordlist"] temp_dict["zhname"] = data_dict["zhname"] temp_dict["username"] = data_dict["username"] temp_dict["tablename"] = str(data_dict["tablename"]) temp_dict["number"] = str(data_dict["number"]) data_list.append(temp_dict) if len(data_list) > 0: self.write(dict( code=0, msg='获取成功', data=data_list, )) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=10, strip=True) limit_start = (int(page_size) - 1) * int(limit) system_user_list = [] with DBContext('r', None, True) as session: if key and value: count = session.query(SystemUser).filter_by(**{key: value}).count() system_user_data = session.query(SystemUser).filter_by(**{key: value}).order_by( SystemUser.id).offset(limit_start).limit(int(limit)) else: count = session.query(SystemUser).count() system_user_data = session.query(SystemUser).order_by(SystemUser.id).offset( limit_start).limit(int(limit)) for data in system_user_data: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) if data_dict['platform_users']: data_dict['platform_users'] = data_dict.get('platform_users', '').split(',') system_user_list.append(data_dict) return self.write(dict(code=0, msg='获取成功', count=count, data=system_user_list))
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default="888", strip=True) limit_start = (int(page_size) - 1) * int(limit) tag_list = [] with DBContext('r') as session: if key: all_tags = session.query(Tag).filter( or_(Tag.tag_name.like('%{}%'.format(key)), Tag.tag_name.like('%{}%'.format(key)), Tag.tag_value.like('%{}%'.format(key)), Tag.tag_description.like('%{}%'.format(key)), Tag.tag_aws_service.like( '%{}%'.format(key)))).order_by( Tag.id).offset(limit_start).limit(int(limit)) count = all_tags.count() else: all_tags = session.query(Tag).order_by( Tag.id).offset(limit_start).limit(int(limit)) count = all_tags.count() for data in all_tags: data_dict = model_to_dict(data) tag_list.append(data_dict) self.write(dict(code=0, msg='获取成功', count=count, data=tag_list))
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=10, strip=True) limit_start = (int(page_size) - 1) * int(limit) server_detail_list = [] with DBContext('w', None, True) as session: if key and value: count = session.query(ServerDetail).filter_by(**{ key: value }).count() server_detail_info = session.query(ServerDetail).filter_by( **{ key: value }).order_by(ServerDetail.id) # .offset(limit_start).limit(int(limit)) #这里加上分页的话,有时候有时候在别的页面进行全局搜可能会有点小问题 else: count = session.query(ServerDetail).count() server_detail_info = session.query(ServerDetail).order_by( ServerDetail.id) # .offset(limit_start).limit(int(limit)) #这里加上分页的话,有时候有时候在别的页面进行全局搜可能会有点小问题 for data in server_detail_info: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) server_detail_list.append(data_dict) if not server_detail_list: return self.write(dict(code=-1, msg='获取失败')) return self.write( dict(code=0, msg='获取成功', count=count, data=server_detail_list))
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 get(self, *args, **kwargs): nickname = self.get_current_nickname() key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default="888", strip=True) limit_start = (int(page_size) - 1) * int(limit) tag_list = [] with DBContext('r') as session: if key == 'tag_name' and value: count = session.query(Tag).filter( Tag.tag_name.like('%{}%'.format(value))).count() all_tags = session.query(Tag).filter( Tag.tag_name.like('%{}%'.format(value))).order_by( Tag.id).offset(limit_start).limit(int(limit)) elif limit == '888': count = session.query(Tag).count() all_tags = session.query(Tag).order_by(Tag.id).all() elif key and key != 'tag_name' and value: count = session.query(Tag).filter_by(**{key: value}).count() all_tags = session.query(Tag).order_by( Tag.id).filter_by(**{ key: value }).order_by(Tag.id).offset(limit_start).limit(int(limit)) else: count = session.query(Tag).count() all_tags = session.query(Tag).order_by( Tag.id).offset(limit_start).limit(int(limit)) for msg in all_tags: db_list = [] server_list = [] data_dict = model_to_dict(msg) data_dict['create_time'] = str(data_dict['create_time']) if data_dict['users']: data_dict['users'] = data_dict.get('users', '').split(',') else: data_dict['users'] = [] server_tags = session.query(ServerTag.id, Server.id).outerjoin( Server, Server.id == ServerTag.server_id).filter( ServerTag.tag_id == msg.id).all() for i in server_tags: server_list.append(i[1]) data_dict['servers'] = server_list data_dict['server_len'] = len(server_tags) db_tags = session.query(DBTag.id, DB.id, DB.db_code).outerjoin( DB, DB.id == DBTag.db_id).filter(DBTag.tag_id == msg.id).all() for i in db_tags: db_list.append(i[1]) data_dict['db_len'] = len(db_tags) data_dict['dbs'] = db_list tag_list.append(data_dict) self.write(dict(code=0, msg='获取成功', count=count, data=tag_list))
def get(self, *args, **kwargs): project_list = [] with DBContext('r') as session: host_info = session.query(Server).all() for host in host_info: data_dict = model_to_dict(host) project_list.append(data_dict["project"]) project = list(filter(None, list(set(project_list)))) return self.write(dict(code=0, msg='获取成功', data=project))
def get_asset_info(self): '''获取资产信息''' server_list = [] with DBContext('r') as session: server_data = session.query(Server).filter(Server.state == 'true').all() for data in server_data: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) server_list.append(data_dict) return server_list
def get_asset_server(self): """ 获取CMDB里面所有主机 :return: """ server_list = [] with DBContext('r') as session: server_info = session.query(Server).order_by(Server.id).all() for msg in server_info: data_dict = model_to_dict(msg) data_dict['create_time'] = str(data_dict['create_time']) server_list.append(data_dict) return server_list
def get_configs(): """ get id / key / region info :return: """ aws_configs_list = [] with DBContext('r') as session: aws_configs_info = session.query(AssetConfigs).filter(AssetConfigs.account == 'AWS').all() for data in aws_configs_info: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) aws_configs_list.append(data_dict) return aws_configs_list
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) tag_rule_list = [] with DBContext('r') as session: if key and value: tag_rule_data = session.query(TagRule).filter_by(**{key: value}).all() else: tag_rule_data = session.query(TagRule).all() for data in tag_rule_data: data_dict = model_to_dict(data) tag_rule_list.append(data_dict) return self.write(dict(code=0, msg='获取成功', data=tag_rule_list))
def get_system_user(self): """ 获取所有系统用户 :return: """ with DBContext('r') as session: system_user_list = [] system_user_data = session.query(SystemUser).all() for data in system_user_data: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) system_user_list.append(data_dict) return system_user_list
def get(self, *args, **kwargs): nickname = self.get_current_nickname() tag_list = [] with DBContext('r') as session: the_tags = session.query(Tag).order_by(Tag.id).all() for msg in the_tags: data_dict = model_to_dict(msg) data_dict.pop('create_time') if self.is_superuser: tag_list.append(data_dict) elif data_dict['users'] and nickname in data_dict['users'].split(','): tag_list.append(data_dict) return self.write(dict(code=0, msg='获取成功', data=tag_list))
def check_server_state(self): '''查询Server状态, 新建的推送Key并更新''' new_asset_list = [] id_list = [] with DBContext('r') as session: # 新加的资产,state:new new_asset = session.query(Server).filter(Server.state == self.state).all() for msg in new_asset: data_dict = model_to_dict(msg) data_dict['create_time'] = str(data_dict['create_time']) new_asset_list.append(data_dict) for i in new_asset_list: id_list.append(i.get('id')) return id_list
def get_configs(): """ get id / key / region info :return: """ qcloud_configs_list = [] with DBContext('r') as session: qcloud_configs_info = session.query(AssetConfigs).filter( AssetConfigs.account == '腾讯云', AssetConfigs.state == 'true').all() for data in qcloud_configs_info: data_dict = model_to_dict(data) data_dict['create_time'] = str(data_dict['create_time']) data_dict['update_time'] = str(data_dict['update_time']) qcloud_configs_list.append(data_dict) return qcloud_configs_list
def get(self, *args, **kwargs): data_list = [] superuser_flag = 0 if self.is_superuser: superuser_flag = 1 key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=30, strip=True) limit_start = (int(page_size) - 1) * int(limit) user_list = [] with DBContext('r') as session: conditions = [] if key == "name": conditions.append(Storage.name.like('%{}%'.format(value))) if key == "mode": conditions.append(Storage.mode.like('%{}%'.format(value))) todata = session.query(Storage).filter(*conditions).order_by( Storage.create_time.desc()).offset(limit_start).limit( int(limit)).all() tocount = session.query(Storage).filter(*conditions).count() for msg in todata: temp_dict = {} data_dict = model_to_dict(msg) temp_dict["id"] = data_dict["id"] temp_dict["name"] = data_dict["name"] temp_dict["mode"] = data_dict["mode"] temp_dict["dictvalue"] = data_dict["dictvalue"] temp_dict["authorized"] = data_dict["authorized"] temp_dict["remarks"] = data_dict["remarks"] temp_dict["username"] = str(data_dict["username"]) temp_dict["consume"] = str(data_dict["consume"]) temp_dict["create_time"] = str(data_dict["create_time"]) data_list.append(temp_dict) if len(data_list) > 0: self.write( dict(code=0, msg='获取成功', count=tocount, data=data_list, flag=superuser_flag)) else: self.write(dict(code=-1, msg='没有相关数据', count=0, data=[]))
def get(self, *args, **kwargs): id = self.get_argument('id', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=10, strip=True) key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) start_date = self.get_argument('start_date', default=None, strip=True) end_date = self.get_argument('end_date', default=None, strip=True) limit_start = (int(page_size) - 1) * int(limit) if not start_date: start_date = datetime.date.today() - relativedelta(months=+1) if not end_date: end_date = datetime.date.today() + datetime.timedelta(days=1) start_time_tuple = time.strptime(str(start_date), '%Y-%m-%d') end_time_tuple = time.strptime(str(end_date), '%Y-%m-%d') log_list = [] with DBContext('r') as session: if id: count = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.id == id).count() log_info = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.id == id).all() elif key and value: count = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.ctime > start_time_tuple, AssetOperationalAudit.ctime < end_time_tuple).filter_by( **{key: value}).count() log_info = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.ctime > start_time_tuple, AssetOperationalAudit.ctime < end_time_tuple).filter_by( **{key: value}).order_by(-AssetOperationalAudit.ctime) else: count = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.ctime > start_time_tuple, AssetOperationalAudit.ctime < end_time_tuple).count() log_info = session.query(AssetOperationalAudit).filter(AssetOperationalAudit.ctime > start_time_tuple, AssetOperationalAudit.ctime < end_time_tuple).order_by( -AssetOperationalAudit.ctime).offset(limit_start).limit(int(limit)) for msg in log_info: data_dict = model_to_dict(msg) data_dict['ctime'] = str(data_dict['ctime']) log_list.append(data_dict) return self.write(dict(code=0, msg='获取日志成功', count=count, data=log_list))
def get(self, *args, **kwargs): key = self.get_argument('key', default=None, strip=True) value = self.get_argument('value', default=None, strip=True) page_size = self.get_argument('page', default=1, strip=True) limit = self.get_argument('limit', default=10, strip=True) limit_start = (int(page_size) - 1) * int(limit) with DBContext('r', None, True) as session: log_list = [] if key and value: log_info = session.query(AssetErrorLog).filter_by(**{key: value}).order_by( AssetErrorLog.id).offset(limit_start).limit(int(limit)) else: log_info = session.query(AssetErrorLog).order_by(AssetErrorLog.id).all() for msg in log_info: data_dict = model_to_dict(msg) data_dict['create_time'] = str(data_dict['create_time']) log_list.append(data_dict) return self.write(dict(code=0, msg='获取成功', data=log_list))