def create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if '1' not in auth_info['r_id']: return json.dumps({'code': 1, 'errmsg': 'you not admin,no power'}) try: data = request.get_json()['params'] if not util.check_name(data['name']): return json.dumps({ 'code': 1, 'errmsg': 'name must be string or num' }) # print data app.config['cursor'].execute_insert_sql('power', data) util.write_log('api').info(username, "create power %s success" % data['name']) return json.dumps({ 'code': 0, 'result': 'create %s success' % data['name'] }) except: util.write_log('api').error('create power error:%s' % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': 'create power failed'})
def role_create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if '1' not in auth_info['r_id']: return json.dumps({'code': 1, 'errmsg': 'you not admin,no power'}) try: data = request.get_json()['params'] if not data.has_key('p_id'): return json.dumps({'code': 1, 'errmsg': 'must hava p_id'}) if not app.config['cursor'].if_id_exist('power', data['p_id'].split(',')): return json.dumps({'code': 1, 'errmsg': 'p_id not exist'}) if not util.check_name(data['name']): return json.dumps({ 'code': 1, 'errmsg': 'name must be string or int' }) app.config['cursor'].execute_insert_sql('role', data) util.write_log('api').info("%s:create role %s scucess" % (username, data['name'])) return json.dumps({ 'code': 0, 'result': 'create role %s scucess' % data['name'] }) except: util.write_log('api').error("create role error: %s" % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': 'create role fail'})
def createuser(auth_info,*arg,**kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] r_id = auth_info['r_id'] #string, eg: '1,2,3' if '1' not in r_id: #角色id = 1 为sa组,超级管理员 return json.dumps({'code': 1,'errmsg':'you not admin,no power' }) try: data = request.get_json()['params'] #api端对传入端参数验证 if 'r_id' not in data: return json.dumps({'code': 1, 'errmsg': "must need a role!"}) if not app.config['cursor'].if_id_exist('role',data['r_id'].split(',')): return json.dumps({'code': 1, 'errmsg': "Role not exist!"}) if not util.check_name(data['username']): return json.dumps({'code': 1, 'errmsg': "username must be string or num!"}) if data['password'] != data['repwd']: return json.dumps({'code': 1, 'errmsg': "password equal repwd!"}) elif len(data['password']) < 6: return json.dumps({'code': 1, 'errmsg': 'passwd must over 6 string !'}) else: data.pop('repwd') #传入的第二次密码字段不存在,需要删除 data['password'] = hashlib.md5(data['password']).hexdigest() data['join_date'] = time.strftime('%Y-%m-%d %H:%M:%S') app.config['cursor'].execute_insert_sql('user', data) util.write_log('api').info(username, "create_user %s" % data['username']) return json.dumps({'code': 0, 'result': 'create user %s success' % data['username']}) except: util.write_log('api').error("Create user error: %s" % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': 'Create user failed'})
def createuser(auth_info, **kwargs): if auth_info['code'] == 1: return json.dump(auth_info) username = auth_info['username'] role = int(auth_info['role']) if role != 0: return json.dumps({'code': 1, 'errmsg': '只有管理员才有此权限'}) try: data = request.get_json()['params'] if 'r_id' not in data: return json.dumps({'code': 1, 'errmsg': "必须选择一个所属组!"}) if not app.config['cursor'].if_id_exist('user_group', data['r_id'].split(',')): return json.dumps({'code': 1, 'errmsg': "提供的组不存在!"}) if not util.check_name(data['username']): return json.dumps({'code': 1, 'errmsg': "用户名必须为字母和数字!"}) if data['password'] != data['repwd']: return json.dumps({'code': 1, 'errmsg': "两次输入的密码不一致!"}) elif len(data['password']) < 6: return json.dumps({'code': 1, 'errmsg': '密码至少需要6位!'}) else: data.pop('repwd') #因为表单是整体打包过来的,第二次输入的密码字段不存在,需要删除 data['password'] = hashlib.md5(data['password']).hexdigest() data['join_date'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) app.config['cursor'].execute_insert_sql('user', data) if not git_passwd(data['username'], data['password']): return json.dumps({'code': 1, 'errmsg': '创建Git密码失败,请检查配置环境'}) util.write_log(username, "create_user %s" % data['username']) return json.dumps({'code': 0, 'result': '创建用户%s成功' % data['username']}) except: logging.getLogger().error("Create user error: %s" % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': '创建用户失败,有异常情况'})
def idc_create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] try: data = request.get_json()['params'] # print data if not util.check_name(data['name']): return json.dumps({'code': 1, 'errmsg': 'name must be string or int'}) app.config['cursor'].execute_insert_sql('idc', data) util.write_log('api').info(username, "create idc %s scucess" % data['name']) return json.dumps({'code':0,'result':'create idc %s scucess' % data['name']}) except: util.write_log('api').error(username,"create idc error: %s" % traceback.format_exc()) return json.dumps({'code':1,'errmsg':'create idc fail'})
def idc_create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] try: data = request.get_json()['params'] print data if not util.check_name(data['name']): return json.dumps({'code': 1, 'errmsg': 'name must be string or int'}) app.config['cursor'].execute_insert_sql('zbhost', data) util.write_log('api').info(username, "create idc %s scucess" % data['name']) return json.dumps({'code':0,'result':'create idc %s scucess' % data['name']}) except: util.write_log('api').error(username,"create idc error: %s" % traceback.format_exc()) return json.dumps({'code':1,'errmsg':'create idc fail'})
def create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = request.get_json()['params'] if not util.check_name(data['idc_name']): return json.dumps({'code': 1, 'errmsg': "用户名必须为字母和数字"}) app.config['cursor'].execute_insert_sql('idc', data) util.write_log(username, "create idc %s success" % data['name']) return json.dumps({'code':0, 'result': '创建IDC"%s"成功' % data['name']}) except: logging.getLogger().error("create Idc error: %s" % traceback.format_exc()) return json.dumps({'code':1, 'errmsg':'创建IDC失败'})
def create(auth_info,**kwargs): if auth_info['code'] == 1: #主要用于判断认证是否过期,过期会会在web提示 return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = request.get_json()['params'] if not util.check_name(data['hostname']): return json.dumps({'code': 1, 'errmsg': "用户名必须为字母和数字"}) app.config['cursor'].execute_insert_sql('host', data) util.write_log(username, "create host %s sucess" % data['hostname']) return json.dumps({'code': 0, 'result': '创建主机%s成功' % data['hostname']}) except: logging.getLogger().error("Create Host error: %s" % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': '创建主机失败'})
def create(auth_info,**kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if '1' not in auth_info['r_id']: return json.dumps({'code': 1,'errmsg':'you not admin,no power' }) try: data = request.get_json()['params'] if not util.check_name(data['name']): return json.dumps({'code': 1, 'errmsg': 'name must be string or num'}) app.config['cursor'].execute_insert_sql('power', data) util.write_log('api').info(username, "create power %s success" % data['name']) return json.dumps({'code':0,'result':'create %s success' % data['name']}) except: util.write_log('api').error('create power error:%s' % traceback.format_exc()) return json.dumps({'code':1,'errmsg': 'create power failed'})
def createuser(auth_info, *arg, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] r_id = auth_info['r_id'] #string, eg: '1,2,3' if '1' not in r_id: #角色id = 1 为sa组,超级管理员 return json.dumps({'code': 1, 'errmsg': 'you not admin,no power'}) try: data = request.get_json()['params'] #api端对传入端参数验证 if 'r_id' not in data: return json.dumps({'code': 1, 'errmsg': "must need a role!"}) if not app.config['cursor'].if_id_exist('role', data['r_id'].split(',')): return json.dumps({'code': 1, 'errmsg': "Role not exist!"}) if not util.check_name(data['username']): return json.dumps({ 'code': 1, 'errmsg': "username must be string or num!" }) if data['password'] != data['repwd']: return json.dumps({'code': 1, 'errmsg': "password equal repwd!"}) elif len(data['password']) < 6: return json.dumps({ 'code': 1, 'errmsg': 'passwd must over 6 string !' }) else: data.pop('repwd') #传入的第二次密码字段不存在,需要删除 data['password'] = hashlib.md5(data['password']).hexdigest() data['join_date'] = time.strftime('%Y-%m-%d %H:%M:%S') app.config['cursor'].execute_insert_sql('user', data) util.write_log('api').info(username, "create_user %s" % data['username']) return json.dumps({ 'code': 0, 'result': 'create user %s success' % data['username'] }) except: util.write_log('api').error("Create user error: %s" % traceback.format_exc()) return json.dumps({'code': 1, 'errmsg': 'Create user failed'})
def role_update(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = request.get_json()['params'] where = data.get('where',None) data = data.get('data',None) if not util.check_name(data['name']): return json.dumps({'code': 1, 'errmsg': '组名必须为字母和数字!'}) result = app.config['cursor'].execute_update_sql('user_group', data, where, ['name', 'name_cn', 'p_id', 'comment']) if result == '': return json.dumps({'code':1, 'errmsg':'需要指定一个组'}) util.write_log(username, 'update group %s success!' % data['name']) return json.dumps({'code':0,'result':'更新组%s成功' % data['name']}) except: logging.getLogger().error("update error: %s" % traceback.format_exc()) return json.dumps({'code':1,'errmsg':"更新组失败"})
def role_create(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = request.get_json()['params'] if not data.has_key('p_id'): return json.dumps({'code':1,'errmsg':'必须选择一个权限!'}) if not app.config['cursor'].if_id_exist('permission',data['p_id'].split(',')): return json.dumps({'code': 1, 'errmsg': "提供的权限不存在!"}) if not util.check_name(data['name']): return json.dumps({'code': 1, 'errmsg': '组名必须为字母和数字!'}) app.config['cursor'].execute_insert_sql('user_group', data) util.write_log(username, "create group %s scucess" % data['name']) return json.dumps({'code':0,'result':'创建组%s成功' % data['name']}) except: logging.getLogger().error(username,"create groups error: %s" % traceback.format_exc()) return json.dumps({'code':1,'errmsg':'创建组失败'})
def idc_update(auth_info, **kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = kwargs.get('data',None) if not util.check_name(data['idc_name']): return json.dumps({'code': 1, 'errmsg': "用户名必须为字母和数字"}) where = kwargs.get('where', None) result = app.config['cursor'].execute_update_sql('idc', data, where, ['idc_name', 'name', 'address', 'email', 'interface_user', 'user_phone', 'pact_cabinet_num', 'rel_cabinet_num', 'remark']) if result == '': return json.dumps({'code':1,'errmsg':'需要指定一个IDC'}) util.write_log(username,'update idc %s success' % data['name']) return json.dumps({'code':0,'result':'更新IDC"%s"成功' % data['name']}) except: logging.getLogger().error("update idc error: %s" % traceback.format_exc()) return json.dumps({'code':1,'errmsg':'更新IDC失败'})
def update(auth_info,**kwargs): if auth_info['code'] == 1: return json.dumps(auth_info) username = auth_info['username'] if auth_info['role'] != '0': return json.dumps({'code': 1,'errmsg':'只有管理员才有此权限' }) try: data = kwargs.get('data',None) if not util.check_name(data['hostname']): return json.dumps({'code': 1, 'errmsg': "用户名必须为字母和数字"}) fields = ['hostname','sn','host_no','inner_ip','mac_address','wan_ip','remote_ip','os_info','cpu_num','disk_num','mem_num',\ 'host_type','manufacturer_id','supplier_id','store_date','expire','idc_id','cabinet_id','service_id','status','vm_status','remark'] where = kwargs.get('where',None) result = app.config['cursor'].execute_update_sql('host', data, where, fields) if result == '': return json.dumps({'code':1,'errmsg':'需要指定一个主机'}) util.write_log(username,'update host %s sucess' % data['hostname']) return json.dumps({'code':0,'result':'更新主机信息%s成功' % data['hostname']}) except: logging.getLogger().error('update Host error : %s' % traceback.format_exc()) return json.dumps({'code':1,'errmsg':'更新主机失败'})