def user_all_area_ids_by_userid(user_id): ''' 获取指定用户的所属区域(父区域、子区域)ID列表 :return: ''' # 由于这里获取的父区域ID,所以先将父区域下面的子区域ID也取出来 user_area_ids = user_area(user_id)[user_id] all_area_ids = [] for _area_id in user_area_ids: _area_info = area_s.AreaService().get_area_info(_area_id) if _area_info: # 父区域 if _area_info['parent_id'] == -1: _child_nums, _child_data = area_s.AreaService( ).get_child_areas(_area_id) # 有子区域,把子区域的ID都加上 if _child_nums > 0: for _child in _child_data: all_area_ids.append(_child['id']) else: # 子区域,把其父区域ID加上 all_area_ids.append(_area_info['parent_id']) # 同时把本区域加上 all_area_ids.append(_area_id) # 去除重复的 return list(set(all_area_ids))
def get_parent_areas(): ''' 获取所有适合做父区域的数据 :return: ''' parent_list = [] user_areas_list = current_user_all_area_ids() if not user_areas_list: return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=parent_list) parent_nums, parent_datas = area_s.AreaService().get_available_parents( user_areas_list) for i in parent_datas: # 没有子区域但有机房的区域不能作为父区域 _dc_nums = dc_s.DataCenterService().get_datacenter_nums_in_area( i['id']) _child_nums = area_s.AreaService().get_child_areas_nums(i['id']) if _child_nums < 1 and _dc_nums > 0: continue _parent = { "parent_id": i["id"], "parent_name": i["displayname"], } parent_list.append(_parent) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=parent_list)
def get_datacenter_nums_in_area(self, area_id): ''' 获取指定区域下的机房数量 这里包括区域和其子区域 :param area_id: :return: ''' child_nums, child_datas = area_s.AreaService().get_child_areas(area_id) area_ids_list = [child['id'] for child in child_datas] area_ids_list.append(area_id) area_ids_list = list(set(area_ids_list)) params = { 'WHERE_AND': { '=': { 'isdeleted': '0', }, 'in': { 'area_id': area_ids_list }, }, } total_nums, data = self.datacenter_db.simple_query(**params) return total_nums
def net_area_level_info_get(): resp = NetArealevelInfoResp() user_all_area_ids = current_user_all_area_ids() level_datas = net_area.get_level_info() for i in level_datas: # 只获取当前用户所在区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue _level = net_area_level_info.NetArealevelInfo().init_from_db(i) # 如果有父区域 if i['parent_id']: _parent_data = area_s.AreaService().get_area_info(i['parent_id']) if _parent_data: _level.area = _parent_data['displayname'] _level.child_area = i['area_name'] else: # 有父区域ID但没有相应信息,则当做没有父区域 _level.area = i['area_name'] else: # 如果没有父区域,则本身作为区域,子区域为空 _level.area = i['area_name'] resp.level_info.append(_level) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json())
def v2v_esx_get_hostpool_info(): ''' 获取层级信息 机房 - 网络区域 - 集群 :return: ''' user_all_area_ids = current_user_all_area_ids() area_ZB = [] area_DQ = [] # area层级信息 - 总部 area_zb_data = hostpool_service.v2v_get_level_info_hostpool_zb() for i in area_zb_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue area_ZB.append(i) # area层级信息 - 地区 area_dq_data = hostpool_service.get_level_info_hostpool_dq() for i in area_dq_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue _area_dq_info = area_dq_init_info.AreaDQInitInfo().init_from_db(i) # 表示有父区域 if i['parent_id']: _parent_info = area_service.AreaService().get_area_info( i['parent_id']) if _parent_info: _area_dq_info.area_name = _parent_info['displayname'] _area_dq_info.child_area_name = i['area_name'] else: # 有父区域ID但没有相应信息,则当做没有父区域 _area_dq_info.area_name = i['area_name'] else: _area_dq_info.area_name = i['area_name'] area_DQ.append(_area_dq_info) return area_ZB, area_DQ
def area_update(area_id): name = request.values.get('name') manager = request.values.get('manager') if not area_id or not name or not manager: logging.info('no area_id or name or manager when update area') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) is_exist = user_service.UserService().query_user_info('userid', manager) if not is_exist: logging.error("no such manager %s exist when update area", manager) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg="对不起,你输入的管理员ID不存在") # 区域名不能重复 old_area = area_s.AreaService().get_area_info(area_id) if not old_area: logging.error('area %s is not exist in db when update area', area_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR) if old_area['name'] != name: name_exist = area_s.AreaService().check_area_name_exist(name) if name_exist: logging.error('name %s is duplicated when update area', name) return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR, msg="区域名不能重复,请修改区域名") update_data = { 'name': name, 'displayname': name, 'manager': manager, 'updated_at': get_datetime_str() } where_data = { 'id': area_id, } ret = area_s.AreaService().update_area_info(update_data, where_data) if ret < 0: logging.error("update area error, update_data:%s, where_data:%s", str(update_data), str(where_data)) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR) return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
def area_list(): ''' request :return: ''' resp = AreaListResp() user_areas_list = current_user_all_area_ids() if not user_areas_list: return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json()) params = { 'WHERE_AND': { 'in': { 'id': user_areas_list }, '=': { 'parent_id': -1, 'isdeleted': '0' } }, 'ORDER': [ ['id', 'desc'], ], 'PAGINATION': { 'page_size': request.values.get('page_size', 20), 'page_no': request.values.get('page_no', 1), } } total_nums, data = area_service.AreaService().query_data(**params) resp.total = total_nums for i in data: _area_info = area_info.AreaInfo().init_from_db(i) _area_info.child_areas_nums = area_service.AreaService().get_child_areas_nums(i['id']) _area_info.datacenter_nums = datacenter_service.DataCenterService().get_datacenter_nums_in_area(i['id']) resp.rows.append(_area_info) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json())
def area_add(): name = request.values.get('name') manager = request.values.get('manager') parent_id = request.values.get('parent_id') if not name or not parent_id: logging.info('no name or parent_id when add area') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) is_exist = user_service.UserService().query_user_info('userid', manager) if not is_exist: logging.error("no such manager %s exist when add area", manager) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg="对不起,你输入的管理员ID不存在") # 区域名不能重复 name_exist = area_s.AreaService().check_area_name_exist(name) if name_exist: logging.error('name %s is duplicated when add area', name) return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR, msg="区域名不能重复,请修改区域名") insert_data = { 'name': name, 'displayname': name, 'parent_id': parent_id, 'manager': manager, 'area_type': '1', 'isdeleted': '0', 'created_at': get_datetime_str() } ret = area_s.AreaService().add_area(insert_data) if ret.get('row_num') <= 0: logging.error("add area error, insert_data:%s", str(insert_data)) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR) return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
def get_area_level_info(): resp = ArealevelInfoResp() user_all_area_ids = current_user_all_area_ids() all_areas_nums, all_areas_data = area_s.AreaService().get_all_areas() parent_ids_list = [] for i in all_areas_data: if i['parent_id'] != -1: parent_ids_list.append(i['parent_id']) # 去除重复的 parent_ids_list = list(set(parent_ids_list)) for i in all_areas_data: # 不显示有子区域的区域 if i['id'] in parent_ids_list: continue # 只显示当前用户所属的区域 if user_all_area_ids and i['id'] not in user_all_area_ids: continue _area = area_level_info.ArealevelInfo().init_from_db(i) if i['parent_id']: _parent = area_s.AreaService().get_area_info(i['parent_id']) if _parent: _area.area = _parent['displayname'] _area.child_area = i['displayname'] else: _area.area = i['displayname'] else: _area.area = i['displayname'] resp.level_info.append(_area) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json())
def add_child_area(parent_area_id, child_area_id): if not parent_area_id or not child_area_id: logging.info('no parent_area_id or child_area_id when add child area') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) update_data = { 'parent_id': parent_area_id, } where_data = { 'id': child_area_id, } ret = area_s.AreaService().update_area_info(update_data, where_data) if ret.get('row_num') <= 0: logging.error("add child area error, update_data:%s, where_data:%s", str(update_data), str(where_data)) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR) return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
def get_child_areas(area_id): if not area_id: logging.info('no area_id when get child areas') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) child_nums, child_data = area_s.AreaService().get_child_areas(area_id) child_list = [] for i in child_data: if i["id"] not in current_user_all_area_ids(): continue _child = { "child_id": i["id"], "child_name": i["displayname"], "datacenter_nums": dc_s.DataCenterService().get_datacenter_nums_in_area(i['id']), "manager": i['manager'] } child_list.append(_child) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=child_list)
def area_delete(): area_ids = request.values.get('area_ids') if not area_ids: logging.error('no area_ids when delete area') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) area_ids_list = area_ids.split(',') # 操作的area数 all_num = len(area_ids_list) msg = None fail_num = 0 for _id in area_ids_list: # 有子区域、机房、组关联的都不能删除 _child_nums = area_s.AreaService().get_child_areas_nums(_id) if _child_nums > 0: logging.error('no allow to delete area %s that has child area', _id) fail_num += 1 # 单台操作且已失败则直接跳出循环 if all_num == 1: msg = '该区域下有子区域,不允许删除' break continue else: _dc_nums = dc_s.DataCenterService().get_datacenter_nums_in_area( _id) if _dc_nums > 0: logging.error('no allow to delete area %s that has datacenter', _id) fail_num += 1 # 单台操作且已失败则直接跳出循环 if all_num == 1: msg = '该区域下已分配有机房,不允许删除' break continue _ret = area_s.AreaService().delete_area(_id) if _ret <= 0: logging.error('db delete area %s fail when delete area', _id) fail_num += 1 continue _ret_a = access_s.delete_access_info_by_area_id(_id) if _ret_a <= 0: logging.error('db delete area %s access info when delete area', _id) fail_num += 1 continue # 全失败 if fail_num == all_num: logging.error("delete area all failed") if msg: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=msg) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR) # 部分成功 if 0 < fail_num < all_num: logging.error("delete all area %s part %s failed", all_num, fail_num) return json_helper.format_api_resp(code=ErrorCode.SUCCESS_PART, msg="部分区域删除成功") return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
def instance_init_info(): def _filter_least_host_num(hostpool_id, least_host_num): ''' 过滤掉不满足最少host数的集群 :param hostpool_id: :param least_host_num: :return: ''' # 获取主机列表 all_hosts_nums, all_hosts_data = host_s.HostService().get_hosts_of_hostpool(hostpool_id) if all_hosts_nums < least_host_num or all_hosts_nums < 1: logging.info('filter hostpool %s that has no least host nums %s when get create init info', hostpool_id, least_host_num) return False return True def _filter_no_segment_info(hostpool_id): ''' 过滤掉没有网段信息的集群 :param hostpool_id: :return: ''' segments_list = hostpool_service.get_segment_info(hostpool_id) if not segments_list: logging.error('filter hostpool %s that has no segment info when get create init info', hostpool_id) return False return True resp = InstanceInitInfoResp() user_all_area_ids = current_user_all_area_ids() # area层级信息 - 总部 area_zb_data = hostpool_service.get_level_info_hostpool_zb() for i in area_zb_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue resp.area_ZB.append(i) # area层级信息 - 地区 area_dq_data = hostpool_service.get_level_info_hostpool_dq() for i in area_dq_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue _area_dq_info = area_dq_init_info.AreaDQInitInfo().init_from_db(i) # 表示有父区域 if i['parent_id']: _parent_info = area_service.AreaService().get_area_info(i['parent_id']) if _parent_info: _area_dq_info.area_name = _parent_info['displayname'] _area_dq_info.child_area_name = i['area_name'] else: # 有父区域ID但没有相应信息,则当做没有父区域 _area_dq_info.area_name = i['area_name'] else: _area_dq_info.area_name = i['area_name'] resp.area_DQ.append(_area_dq_info) # flavor信息 flavors_nums, flavors_data = flavor_service.FlavorService().get_all_flavors() for i in flavors_data: _flavor_info = flavor_init_info.FlavorInitInfo().init_from_db(i) resp.flavors.append(_flavor_info) # image信息 - windows images_windows_nums, images_windows_data = image_service.ImageService().get_all_images('windows') for i in images_windows_data: _image_windows_info = image_init_info.ImageInitInfo().init_from_db(i) resp.images_windows.append(_image_windows_info) # image信息 - linux images_linux_nums, images_linux_data = image_service.ImageService().get_all_images('linux') for i in images_linux_data: _image_linux_info = image_init_info.ImageInitInfo().init_from_db(i) resp.images_linux.append(_image_linux_info) # group信息 user_groups = current_user_groups() user_group_ids_list = [] is_super_group = False for _groups in user_groups: user_group_ids_list.append(_groups['id']) # 超级管理员组 if _groups['name'] == "supergroup": is_super_group = True groups_params = { 'WHERE_AND': { '=': { 'isdeleted': '0' } }, } groups_nums, groups_data = group_service.GroupService().query_data(**groups_params) for i in groups_data: # 管理员组的成员可以显示所有组,而非管理员组的只显示当前用户所在应用组 if not is_super_group and i['id'] not in user_group_ids_list: continue _group_info = group_info.GroupInitInfo().init_from_db_1(i) resp.groups.append(_group_info) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json())
def _user_group_check(env, sys_code, sys_opr_name, sys_opr_id, cluster_id): # 检查应用系统管理员用户是否存在,不存在则新增 user = user_s.UserService().get_user_info_by_user_id(sys_opr_id) if not user: # 用户不存在,新增,并将用户锁定,待后续开放用户运维在解锁 user_data = { 'userid': sys_opr_id, 'username': sys_opr_name, 'status': '1', 'created_at': get_datetime_str() } user_ret = user_s.UserService().add_user(user_data) # 记录安全日志 field_data = {'User_name': sys_opr_name or None, 'Oper_type': 'add'} if user_ret.get('row_num') > 0: field_data.update({'Oper_result': '1 Success'}) CloudLogger.audit(AuditType.USERMGR, field_data) else: field_data.update({ 'Oper_result': '0 Fail', 'fail_reason': 'insert new user info to db fail' }) CloudLogger.audit(AuditType.USERMGR, field_data) return False, 'add new user info to db fail' # 检查应用组是否存在,不存在新建 group = group_s.get_group_info_by_name_and_env(sys_code, env) if not group: group_data = { 'name': sys_code, 'displayname': sys_code, 'isdeleted': '0', 'dc_type': env, 'owner': sys_opr_id, 'cpu': 20000, 'mem': 40000, 'disk': 1000000, 'vm': 5000, 'p_cluster_id': cluster_id, 'created_at': get_datetime_str() } group_ret = group_s.GroupService().add_group_info(group_data) if group_ret.get('row_num') <= 0: return False, 'add group info to db fail' group_id = group_ret.get('last_id') role_id = 2 area_zb_ret = area_s.AreaService().get_area_zb_info() if not area_zb_ret: return False, 'get zongbu area id fail' area_zb_id = area_zb_ret['id'] ret_result = access_service.add_access_list(int(group_id), int(role_id), str(area_zb_id)) if ret_result.get('row_num') <= 0: return False, 'add access info to db fail' user_group_data = { 'user_id': sys_opr_id, 'user_name': sys_opr_name, 'group_id': group_id, 'group_name': sys_code, 'role_id': role_id, 'status': '0', 'created_at': get_datetime_str(), 'expire_at': get_datetime_str(), # todo } ret_u_g = user_g_s.UserGroupService().add_user_group(user_group_data) if ret_u_g.get('row_num') <= 0: logging.error( 'add user group info error when add group, insert_data:%s', user_group_data) return False, 'add user group info to db fail' else: # 获取应用组对应用户信息 role_id = 2 opr_is_exist = False group_id = group['id'] ret_num, ret_query_g_u = user_g_s.UserGroupService().get_alluser_group( group_id) for one_ret_query_g_u in ret_query_g_u: if one_ret_query_g_u['user_id'] == sys_opr_id: opr_is_exist = True if not opr_is_exist: # 将用户加入应用组中 user_group_data = { 'user_id': sys_opr_id, 'user_name': sys_opr_name, 'group_id': group_id, 'group_name': sys_code, 'role_id': role_id, 'status': '0', 'created_at': get_datetime_str(), 'expire_at': get_datetime_str(), # todo } ret_u_g = user_g_s.UserGroupService().add_user_group( user_group_data) if ret_u_g.get('row_num') <= 0: logging.error( 'add user group info error when add group, insert_data:%s', user_group_data) return False, 'add user group info to db fail' # 修改应用组owner update_data = {'owner': sys_opr_id} where_data = { 'id': group_id, } ret_change_owner = group_s.update_group_info( update_data, where_data) if ret_change_owner < 0: logging.error( "update group error, update_data:%s, where_data:%s", str(update_data), str(where_data)) return False, 'update group owner to db fail' return True, group_id
def kvm_common_info(): def _filter_least_host_num(hostpool_id, least_host_num): ''' 过滤掉不满足最少host数的集群 :param hostpool_id: :param least_host_num: :return: ''' # 获取主机列表 all_hosts_nums, all_hosts_data = host_s.HostService( ).get_hosts_of_hostpool(hostpool_id) if all_hosts_nums < least_host_num or all_hosts_nums < 1: logging.info( 'filter hostpool %s that has no least host nums %s when get create init info', hostpool_id, least_host_num) return False return True def _filter_no_segment_info(hostpool_id): ''' 过滤掉没有网段信息的集群 :param hostpool_id: :return: ''' segments_list = hostpool_service.get_segment_info(hostpool_id) if not segments_list: logging.error( 'filter hostpool %s that has no segment info when get create init info', hostpool_id) return False return True data_from_api = request.data data_requset = json_helper.loads(data_from_api) user_id = data_requset['userId'] resp = KvmInfoResp() user_all_area_ids = user_s.user_all_area_ids_by_userid(user_id) # area层级信息 - 总部 area_zb_data = hostpool_service.get_level_info_hostpool_zb() for i in area_zb_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有负载均衡网络的区域 if "LoadBalance" not in i['net_area_name']: continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue resp.area_ZB.append(i) # area层级信息 - 地区 area_dq_data = hostpool_service.get_level_info_hostpool_dq() for i in area_dq_data: # 不显示不满足最少host数的集群 if not _filter_least_host_num(i['hostpool_id'], i['least_host_num']): continue # 不显示没有负载均衡网络的区域 if "LoadBalance" not in i['net_area_name']: continue # 不显示没有网段信息的集群 if not _filter_no_segment_info(i['hostpool_id']): continue # 只显示当前用户所属的区域 if user_all_area_ids and i['area_id'] not in user_all_area_ids: continue _area_dq_info = area_dq_init_info.AreaDQInitInfo().init_from_db(i) # 表示有父区域 if i['parent_id']: _parent_info = area_service.AreaService().get_area_info( i['parent_id']) if _parent_info: _area_dq_info.area_name = _parent_info['displayname'] _area_dq_info.child_area_name = i['area_name'] else: # 有父区域ID但没有相应信息,则当做没有父区域 _area_dq_info.area_name = i['area_name'] else: _area_dq_info.area_name = i['area_name'] resp.area_DQ.append(_area_dq_info) # group信息 user_groups = user_s.current_user_groups_by_userid(user_id) user_group_ids_list = [] is_middleware_admin_group = False for _groups in user_groups: user_group_ids_list.append(_groups['id']) # 中间件管理员组,这里后期要注意,如果中间件管理员组id不为2,则识别不出用户是否是中间件管理员组 if _groups['id'] == 2: is_middleware_admin_group = True groups_params = { 'WHERE_AND': { '=': { 'isdeleted': '0' } }, } groups_nums, groups_data = group_service.GroupService().query_data( **groups_params) for i in groups_data: # 中间件管理员组可以显示所有组,而非管理员组的只显示当前用户所在应用组 if not is_middleware_admin_group and i['id'] not in user_group_ids_list: continue _group_info = group_info.GroupInitInfo().init_from_db_1(i) resp.groups.append(_group_info) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=resp.to_json())