def cc_search_create_object_attribute(request, obj_id, biz_cc_id, supplier_account): client = get_client_by_user(request.user.username) kwargs = {'bk_obj_id': obj_id, 'bk_supplier_account': supplier_account} cc_result = client.cc.search_object_attribute(kwargs) if not cc_result['result']: message = handle_api_error('cc', 'cc.search_object_attribute', kwargs, cc_result['message']) logger.error(message) result = {'result': False, 'data': [], 'message': message} return JsonResponse(result) obj_property = [] for item in cc_result['data']: if item['editable']: prop_dict = { 'tag_code': item['bk_property_id'], 'type': "input", 'attrs': { 'name': item['bk_property_name'], 'editable': 'true', }, } if item['bk_property_id'] in ['bk_set_name']: prop_dict["attrs"]["validation"] = [{"type": "required"}] obj_property.append(prop_dict) return JsonResponse({'result': True, 'data': obj_property})
def cmdb_get_mainline_object_topo(request, bk_biz_id, bk_supplier_account=''): """ @summary: 获取配置平台业务拓扑模型 @param request: @param bk_biz_id: @param bk_supplier_account: @return: """ kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, } client = get_client_by_request(request) cc_result = client.cc.get_mainline_object_topo(kwargs) if not cc_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.get_mainline_object_topo', kwargs, cc_result['message']) return { 'result': cc_result['result'], 'code': cc_result['code'], 'message': message } data = cc_result['data'] for bk_obj in data: if bk_obj['bk_obj_id'] == 'host': bk_obj['bk_obj_name'] = 'IP' result = { 'result': cc_result['result'], 'code': cc_result['code'], 'data': cc_result['data'] } return JsonResponse(result)
def cc_search_object_attribute(request, obj_id, biz_cc_id, supplier_account): """ @summary: 获取对象自定义属性 @param request: @param biz_cc_id: @return: """ client = get_client_by_user(request.user.username) kwargs = {'bk_obj_id': obj_id, 'bk_supplier_account': supplier_account} cc_result = client.cc.search_object_attribute(kwargs) if not cc_result['result']: message = handle_api_error('cc', 'cc.search_object_attribute', kwargs, cc_result['message']) logger.error(message) result = {'result': False, 'data': [], 'message': message} return JsonResponse(result) obj_property = [] for item in cc_result['data']: if item['editable']: obj_property.append({ 'value': item['bk_property_id'], 'text': item['bk_property_name'] }) return JsonResponse({'result': True, 'data': obj_property})
def job_get_script_list(request, biz_cc_id): """ 查询业务脚本列表 :param request: :param biz_cc_id: :return: """ # 查询脚本列表 client = get_client_by_request(request) script_type = request.GET.get('type') kwargs = { 'bk_biz_id': biz_cc_id, 'is_public': True if script_type == 'public' else False } script_result = client.job.get_script_list(kwargs) if not script_result['result']: message = handle_api_error('cc', 'job.get_script_list', kwargs, script_result['message']) logger.error(message) result = {'result': False, 'message': message} return JsonResponse(result) script_dict = {} for script in script_result['data']['data']: script_dict.setdefault(script['name'], []).append(script['id']) version_data = [] for name, version in script_dict.items(): version_data.append({"text": name, "value": max(version)}) return JsonResponse({'result': True, 'data': version_data})
def get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account): client = get_client_by_user(username) kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, } topo_result = client.cc.search_biz_inst_topo(kwargs) if not topo_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_biz_inst_topo', kwargs, topo_result['message']) result = {'result': False, 'code': 100, 'message': message, 'data': []} return result inter_result = client.cc.get_biz_internal_module(kwargs) if not inter_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.get_biz_internal_module', kwargs, inter_result['message']) result = {'result': False, 'code': 101, 'message': message, 'data': []} return result inter_data = inter_result['data'] data = topo_result['data'] if 'bk_set_id' in inter_data: default_set = { 'default': 1, 'bk_obj_id': 'set', 'bk_obj_name': _(u"集群"), 'bk_inst_id': inter_data['bk_set_id'], 'bk_inst_name': inter_data['bk_set_name'], 'child': [{ 'default': 1, 'bk_obj_id': 'module', 'bk_obj_name': _(u"模块"), 'bk_inst_id': mod['bk_module_id'], 'bk_inst_name': mod['bk_module_name'], } for mod in inter_data['module']] } data[0]['child'].insert(0, default_set) return {'result': True, 'code': 0, 'data': data, 'messsage': ''}
def cmdb_get_mainline_object_topo(request, bk_biz_id, bk_supplier_account=''): """ @summary: 获取配置平台业务拓扑模型 @param request: @param bk_biz_id: @param bk_supplier_account: @return: """ kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, } client = get_client_by_user(request.user.username) cc_result = client.cc.get_mainline_object_topo(kwargs) if not cc_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.get_mainline_object_topo', kwargs, cc_result) if cc_result.get('code', 0) == AUTH_FORBIDDEN_CODE: logger.warning(message) raise AuthFailedException( permissions=cc_result.get('permission', [])) return JsonResponse({ 'result': cc_result['result'], 'code': cc_result['code'], 'message': message }) data = cc_result['data'] for bk_obj in data: if bk_obj['bk_obj_id'] == 'host': bk_obj['bk_obj_name'] = 'IP' result = { 'result': cc_result['result'], 'code': cc_result['code'], 'data': cc_result['data'] } return JsonResponse(result)
def cc_search_topo(request, obj_id, category, biz_cc_id, supplier_account): """ @summary: 查询对象拓扑 @param request: @param biz_cc_id: @return: """ client = get_client_by_user(request.user.username) kwargs = {'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account} cc_result = client.cc.search_biz_inst_topo(kwargs) if not cc_result['result']: message = handle_api_error('cc', 'cc.search_biz_inst_topo', kwargs, cc_result['message']) logger.error(message) result = {'result': False, 'data': [], 'message': message} return JsonResponse(result) if category in ["normal", "prev", "picker"]: cc_topo = cc_format_topo_data(cc_result['data'], obj_id, category) else: cc_topo = [] return JsonResponse({'result': True, 'data': cc_topo})
def job_get_own_db_account_list(request, biz_cc_id): """ 查询用户有权限的DB帐号列表 :param biz_cc_id: :param request: :return: """ client = get_client_by_user(request.user.username) kwargs = {'bk_biz_id': biz_cc_id} job_result = client.job.get_own_db_account_list(kwargs) if not job_result['result']: message = handle_api_error('job', 'get_own_db_account_list', kwargs, job_result['message']) logger.error(message) result = {'result': False, 'message': message} return JsonResponse(result) data = [{ 'text': item['db_alias'], 'value': item['db_account_id'] } for item in job_result['data']] return JsonResponse({'result': True, 'data': data})
def cc_handle_api_error(api_name, params, error): message = handle_api_error(__group_name__, api_name, params, error) return message
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs('executor') biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id') supplier_account = parent_data.get_one_of_inputs( 'biz_supplier_account') client = get_client_by_user(executor) if parent_data.get_one_of_inputs('language'): setattr(client, 'language', parent_data.get_one_of_inputs('language')) translation.activate(parent_data.get_one_of_inputs('language')) cc_hosts = data.get_one_of_inputs('cc_host_replace_detail') # 查询主机可编辑属性 search_attr_kwargs = { 'bk_obj_id': 'host', 'bk_supplier_account': supplier_account } search_attr_result = client.cc.search_object_attribute( search_attr_kwargs) if not search_attr_result['result']: message = handle_api_error(__group_name__, 'cc.search_object_attribute', search_attr_kwargs, search_attr_result['message']) logger.error(message) data.outputs.ex_data = message return False editable_attrs = [] for item in search_attr_result['data']: if item['editable']: editable_attrs.append(item['bk_property_id']) # 拉取所有主机信息 search_kwargs = { 'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account, 'condition': [{ 'bk_obj_id': 'module', 'fields': ['bk_module_id'], 'condition': [] }] } fault_replace_ip_map = {} for item in cc_hosts: fault_replace_ip_map[''.join(get_ip_by_regex( item['cc_fault_ip']))] = ''.join( get_ip_by_regex(item['cc_new_ip'])) all_hosts = [] all_hosts.extend(fault_replace_ip_map.keys()) all_hosts.extend(fault_replace_ip_map.values()) search_kwargs['ip'] = { 'data': all_hosts, 'exact': 1, 'flag': 'bk_host_innerip' } hosts_result = client.cc.search_host(search_kwargs) if not hosts_result['result']: message = handle_api_error(__group_name__, 'cc.search_host', search_attr_kwargs, hosts_result['message']) logger.error(message) data.outputs.ex_data = message return False # 更新替换机信息 batch_update_kwargs = { 'bk_obj_id': 'host', 'bk_supplier_account': supplier_account, 'update': [] } host_dict = { host_info['host']['bk_host_innerip']: host_info['host'] for host_info in hosts_result['data']['info'] } host_id_to_ip = { host_info['host']['bk_host_id']: host_info['host']['bk_host_innerip'] for host_info in hosts_result['data']['info'] } fault_replace_id_map = {} for fault_ip, new_ip in fault_replace_ip_map.items(): fault_host = host_dict.get(fault_ip) new_host = host_dict.get(new_ip) if not fault_host: data.outputs.ex_data = _(u"无法查询到 %s 机器信息,请确认该机器是否在当前业务下" % fault_ip) return False if not new_host: data.outputs.ex_data = _(u"无法查询到 %s 机器信息,请确认该机器是否在当前业务下" % new_ip) return False update_item = {'datas': {}, 'inst_id': new_host['bk_host_id']} for attr in [ attr for attr in editable_attrs if attr in fault_host ]: update_item['datas'][attr] = fault_host[attr] batch_update_kwargs['update'].append(update_item) fault_replace_id_map[ fault_host['bk_host_id']] = new_host['bk_host_id'] update_result = client.cc.batch_update_inst(batch_update_kwargs) if not update_result['result']: message = handle_api_error(__group_name__, 'cc.batch_update_inst', batch_update_kwargs, update_result['message']) logger.error(message) data.outputs.ex_data = message return False # 将主机上交至故障机模块 fault_transfer_kwargs = { 'bk_supplier_account': supplier_account, 'bk_biz_id': biz_cc_id, 'bk_host_id': list(fault_replace_id_map.keys()) } fault_transfer_result = client.cc.transfer_host_to_faultmodule( fault_transfer_kwargs) if not fault_transfer_result['result']: message = handle_api_error(__group_name__, 'cc.transfer_host_to_faultmodule', fault_transfer_kwargs, fault_transfer_result['message']) data.set_outputs('ex_data', message) return False # 转移主机模块 transfer_kwargs_list = [] for host_info in hosts_result['data']['info']: new_host_id = fault_replace_id_map.get( host_info['host']['bk_host_id']) if new_host_id: transfer_kwargs_list.append({ 'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account, 'bk_host_id': [new_host_id], 'bk_module_id': [ module_info['bk_module_id'] for module_info in host_info['module'] ], 'is_increment': True }) success = [] for kwargs in transfer_kwargs_list: transfer_result = client.cc.transfer_host_module(kwargs) if not transfer_result['result']: message = handle_api_error(__group_name__, 'cc.transfer_host_module', kwargs, transfer_result['message']) logger.error(message) data.outputs.ex_data = u"{msg}\n{success}".format( msg=message, success=_(u"成功替换的机器: %s") % ','.join(success)) return False success.append(host_id_to_ip[kwargs['bk_host_id'][0]])
def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): selector = kwargs['selectors'][0] filters = kwargs['filters'] excludes = kwargs['excludes'] cc_kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, 'condition': [{ 'bk_obj_id': 'module', 'fields': ['bk_module_id', 'bk_module_name'], 'condition': [] }] } topo_result = get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account) if not topo_result['result']: return topo_result biz_topo_tree = topo_result['data'][0] if selector == 'topo': topo = kwargs['topo'] if not topo: return {'result': False, 'data': [], 'message': ''} # transfer topo to modules topo_dct = {} for tp in topo: topo_dct.setdefault(tp['bk_obj_id'], []).append(tp['bk_inst_id']) topo_objects = get_objects_of_topo_tree(biz_topo_tree, topo_dct) topo_modules = [] for obj in topo_objects: topo_modules += get_modules_of_bk_obj(obj) topo_modules_id = get_modules_id(topo_modules) condition = [{ 'field': 'bk_module_id', 'operator': '$in', 'value': topo_modules_id }] cc_kwargs['condition'][0]['codition'] = condition else: host_list = kwargs['ip'] if not host_list: return [] ip_list = [ '%s:%s' % (str(host['cloud'][0]['id']), host['bk_host_innerip']) for host in host_list ] cc_kwargs['condition'].append({ "bk_obj_id": "host", "fields": [ "bk_host_id", "bk_host_innerip", "bk_host_outerip", "bk_host_name" ], "condition": [{ "field": "bk_host_innerip", "operator": "$in", "value": [host['bk_host_innerip'] for host in host_list] }] }) client = get_client_by_user(username) host_result = client.cc.search_host(cc_kwargs) if not host_result: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', cc_kwargs, host_result['message']) return {'result': False, 'data': [], 'message': message} host_info = host_result['data']['info'] data = [] for host in host_info: host_modules_id = get_modules_id(host['module']) if selector == 'topo' or '%s:%s' % ( str(host['host']['bk_cloud_id'][0]['id']), host['host']['bk_host_innerip']) in ip_list: data.append({ 'bk_host_id': host['host']['bk_host_id'], 'bk_host_innerip': host['host']['bk_host_innerip'], 'bk_host_outerip': host['host']['bk_host_outerip'], 'bk_host_name': host['host']['bk_host_name'], 'bk_cloud_id': host['host']['bk_cloud_id'][0]['id'], 'host_modules_id': host_modules_id }) if filters: filters_dct = {} for ft in filters: filters_dct.setdefault(ft['field'], []) filters_dct[ft['field']] += ft['value'] filter_modules = get_modules_of_filters(biz_topo_tree, filters_dct) filter_modules_id = get_modules_id(filter_modules) data = [ host for host in data if set(host['host_modules_id']) & set(filter_modules_id) ] if 'ip' in filters_dct: data = [ host for host in data if host['bk_host_innerip'] in filters_dct['ip'] ] if excludes: excludes_dct = {} for ex in filters: excludes_dct.setdefault(ex['field'], []) excludes_dct[ex['field']] += ex['value'] exclude_modules = get_modules_of_filters(biz_topo_tree, excludes_dct) exclude_modules_id = get_modules_id(exclude_modules) data = [ host for host in data if not set(host['host_modules_id']) & set(exclude_modules_id) ] if 'ip' in excludes_dct: data = [ host for host in data if host['bk_host_innerip'] not in excludes_dct['ip'] ] result = {'result': True, 'code': 0, 'data': data, 'message': ''} return result
def cmdb_search_host(request, bk_biz_id, bk_supplier_account='', bk_supplier_id=0): """ @summary: 获取 CMDB 上业务的 IP 列表,以及 agent 状态等信息 @param request: @param bk_biz_id: 业务 CMDB ID @param bk_supplier_account: 业务开发商账号 @param bk_supplier_id: 业务开发商ID @params fields: list 查询字段,默认只返回 bk_host_innerip、bk_host_name、bk_host_id, 可以查询主机的任意字段,也可以查询 set、module、cloud、agent等信息 @return: """ fields = json.loads(request.GET.get('fields', '[]')) client = get_client_by_user(request.user.username) condition = [{ 'bk_obj_id': 'host', 'fields': [], }] if 'set' in fields: condition.append({ 'bk_obj_id': 'set', 'fields': [], }) if 'module' in fields: condition.append({ 'bk_obj_id': 'module', 'fields': [], }) kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, 'condition': condition } host_result = client.cc.search_host(kwargs) if not host_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', kwargs, host_result) result = { 'result': False, 'code': ERROR_CODES.API_CMDB_ERROR, 'message': message } return JsonResponse(result) host_info = host_result['data']['info'] data = [] if host_info: default_fields = ['bk_host_innerip', 'bk_host_name', 'bk_host_id'] fields = set(default_fields + fields) for host in host_info: host_detail = { field: host['host'][field] for field in fields if field in host['host'] } host_detail['bk_host_innerip'] = format_sundry_ip( host_detail['bk_host_innerip']) if 'set' in fields: host_detail['set'] = host['set'] if 'module' in fields: host_detail['module'] = host['module'] if 'cloud' in fields or 'agent' in fields: host_detail['cloud'] = host['host']['bk_cloud_id'] data.append(host_detail) if 'agent' in fields: agent_kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_id': bk_supplier_id, 'hosts': [{ 'bk_cloud_id': host['cloud'][0]['id'], 'ip': host['bk_host_innerip'] } for host in data] } agent_result = client.gse.get_agent_status(agent_kwargs) if not agent_result['result']: message = handle_api_error(_(u"管控平台(GSE)"), 'gse.get_agent_status', agent_kwargs, agent_result) result = { 'result': False, 'code': ERROR_CODES.API_GSE_ERROR, 'message': message } return JsonResponse(result) agent_data = agent_result['data'] for host in data: # agent在线状态,0为不在线,1为在线,-1为未知 agent_info = agent_data.get( '{cloud}:{ip}'.format(cloud=host['cloud'][0]['id'], ip=host['bk_host_innerip']), {}) host['agent'] = agent_info.get('bk_agent_alive', -1) result = {'result': True, 'code': NO_ERROR, 'data': data} return JsonResponse(result)
def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): """ @summary:根据前端表单数据获取合法的IP @param username: @param bk_biz_id: @param bk_supplier_account: @param kwargs: @return: """ topo_result = get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account) if not topo_result['result']: return topo_result biz_topo_tree = topo_result['data'][0] build_result = build_cmdb_search_host_kwargs(bk_biz_id, bk_supplier_account, kwargs, biz_topo_tree) if not build_result['result']: return { 'result': False, 'code': ERROR_CODES.PARAMETERS_ERROR, 'data': [], 'message': build_result['message'] } cmdb_kwargs = build_result['data'] client = get_client_by_user(username) host_result = client.cc.search_host(cmdb_kwargs) if not host_result: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', cmdb_kwargs, host_result['message']) return {'result': False, 'data': [], 'message': message} host_info = host_result['data']['info'] # IP选择器 selector = kwargs['selectors'][0] if selector == 'ip': ip_list = [ '%s:%s' % (str(host['cloud'][0]['id']), host['bk_host_innerip']) for host in kwargs['ip'] ] else: ip_list = [] data = [] for host in host_info: host_modules_id = get_modules_id(host['module']) if selector == 'topo' or '%s:%s' % ( str(host['host']['bk_cloud_id'][0]['id']), host['host']['bk_host_innerip']) in ip_list: data.append({ 'bk_host_id': host['host']['bk_host_id'], 'bk_host_innerip': host['host']['bk_host_innerip'], 'bk_host_outerip': host['host']['bk_host_outerip'], 'bk_host_name': host['host']['bk_host_name'], 'bk_cloud_id': host['host']['bk_cloud_id'][0]['id'], 'host_modules_id': host_modules_id }) # 筛选条件 filters = kwargs['filters'] if filters: filters_dct = {} for ft in filters: filters_dct.setdefault(ft['field'], []) filters_dct[ft['field']] += format_condition_value(ft['value']) new_topo_tree = process_topo_tree_by_condition(biz_topo_tree, filters_dct) filter_host = set(filters_dct.pop('host', [])) # 把拓扑筛选条件转换成 modules 筛选条件 filter_modules = get_modules_by_condition(new_topo_tree, filters_dct) filter_modules_id = get_modules_id(filter_modules) data = [ host for host in data if set(host['host_modules_id']) & set(filter_modules_id) ] if filter_host: data = [ host for host in data if host['bk_host_innerip'] in filter_host ] # 过滤条件 excludes = kwargs['excludes'] if excludes: excludes_dct = {} for ex in excludes: excludes_dct.setdefault(ex['field'], []) excludes_dct[ex['field']] += format_condition_value(ex['value']) new_topo_tree = process_topo_tree_by_condition(biz_topo_tree, excludes_dct) exclude_host = set(excludes_dct.pop('host', [])) # 把拓扑排除条件转换成 modules 排除条件 exclude_modules = [] if not excludes_dct else get_modules_by_condition( new_topo_tree, excludes_dct) exclude_modules_id = get_modules_id(exclude_modules) data = [ host for host in data if not (set(host['host_modules_id']) & set(exclude_modules_id)) ] if exclude_host: data = [ host for host in data if host['bk_host_innerip'] not in exclude_host ] result = {'result': True, 'code': NO_ERROR, 'data': data, 'message': ''} return result
def get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account): """ @summary: 从 CMDB API 获取业务完整拓扑树,包括空闲机池 @param username: @param bk_biz_id: @param bk_supplier_account: @return: """ client = get_client_by_user(username) kwargs = { 'bk_biz_id': bk_biz_id, 'bk_supplier_account': bk_supplier_account, } topo_result = client.cc.search_biz_inst_topo(kwargs) if not topo_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_biz_inst_topo', kwargs, topo_result) result = { 'result': False, 'code': ERROR_CODES.API_CMDB_ERROR, 'message': message, 'data': [] } return result inter_result = client.cc.get_biz_internal_module(kwargs) if not inter_result['result']: message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.get_biz_internal_module', kwargs, inter_result) result = { 'result': False, 'code': ERROR_CODES.API_CMDB_ERROR, 'message': message, 'data': [] } return result inter_data = inter_result['data'] data = topo_result['data'] if 'bk_set_id' in inter_data: default_set = { 'default': 1, 'bk_obj_id': 'set', 'bk_obj_name': _(u"集群"), 'bk_inst_id': inter_data['bk_set_id'], 'bk_inst_name': inter_data['bk_set_name'], 'child': [{ 'default': 1, 'bk_obj_id': 'module', 'bk_obj_name': _(u"模块"), 'bk_inst_id': mod['bk_module_id'], 'bk_inst_name': mod['bk_module_name'], } for mod in inter_data['module']] } data[0]['child'].insert(0, default_set) return {'result': True, 'code': NO_ERROR, 'data': data, 'messsage': ''}