diff --git a/pipeline_plugins/cmdb_ip_picker/query.py b/pipeline_plugins/cmdb_ip_picker/query.py index 3b9b3e5cef..fc10138b0a 100644 --- a/pipeline_plugins/cmdb_ip_picker/query.py +++ b/pipeline_plugins/cmdb_ip_picker/query.py @@ -25,7 +25,7 @@ ) from gcloud.conf import settings -from .utils import get_cmdb_topo_tree +from .utils import get_cmdb_topo_tree, get_bk_cloud_id_for_host from .constants import NO_ERROR, ERROR_CODES logger = logging.getLogger('root') @@ -103,7 +103,8 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account='', bk_supplier_id= 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] + 'hosts': [{'bk_cloud_id': get_bk_cloud_id_for_host(host, 'cloud'), + 'ip': host['bk_host_innerip']} for host in data] } agent_result = client.gse.get_agent_status(agent_kwargs) if not agent_result['result']: @@ -117,7 +118,7 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account='', bk_supplier_id= 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'], + agent_info = agent_data.get('{cloud}:{ip}'.format(cloud=get_bk_cloud_id_for_host(host, 'cloud'), ip=host['bk_host_innerip']), {}) host['agent'] = agent_info.get('bk_agent_alive', -1) diff --git a/pipeline_plugins/cmdb_ip_picker/utils.py b/pipeline_plugins/cmdb_ip_picker/utils.py index 9907abb223..fe752dd8ac 100644 --- a/pipeline_plugins/cmdb_ip_picker/utils.py +++ b/pipeline_plugins/cmdb_ip_picker/utils.py @@ -22,6 +22,7 @@ from .constants import NO_ERROR, ERROR_CODES get_client_by_user = settings.ESB_GET_CLIENT_BY_USER +DEFAULT_BK_CLOUD_ID = '-1' def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): @@ -56,7 +57,7 @@ def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): # IP选择器 selector = kwargs['selectors'][0] if selector == 'ip': - ip_list = ['{cloud}:{ip}'.format(cloud=host['cloud'][0]['id'], + ip_list = ['{cloud}:{ip}'.format(cloud=get_bk_cloud_id_for_host(host, 'cloud'), ip=host['bk_host_innerip']) for host in kwargs['ip']] else: ip_list = [] @@ -64,14 +65,14 @@ def get_ip_picker_result(username, bk_biz_id, bk_supplier_account, kwargs): for host in host_info: host_modules_id = get_modules_id(host['module']) host_innerip = format_sundry_ip(host['host']['bk_host_innerip']) - if selector == 'topo' or '{cloud}:{ip}'.format(cloud=host['host']['bk_cloud_id'][0]['id'], + if selector == 'topo' or '{cloud}:{ip}'.format(cloud=get_bk_cloud_id_for_host(host['host'], 'bk_cloud_id'), ip=host_innerip) in ip_list: data.append({ 'bk_host_id': host['host']['bk_host_id'], 'bk_host_innerip': 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'], + 'bk_cloud_id': get_bk_cloud_id_for_host(host['host'], 'bk_cloud_id'), 'host_modules_id': host_modules_id }) @@ -296,3 +297,15 @@ def get_cmdb_topo_tree(username, bk_biz_id, bk_supplier_account): } data[0]['child'].insert(0, default_set) return {'result': True, 'code': NO_ERROR, 'data': data, 'messsage': ''} + + +def get_bk_cloud_id_for_host(host_info, cloud_key='cloud'): + """ + @summary: 从 host 信息中心获取 bk_cloud_id,cloud_key 不存在时返回默认值 + @param host_info: + @param cloud_key: + @return: + """ + if not host_info.get(cloud_key, []): + return DEFAULT_BK_CLOUD_ID + return host_info[cloud_key][0]['id'] diff --git a/pipeline_plugins/components/utils/sites/open/utils.py b/pipeline_plugins/components/utils/sites/open/utils.py index eb98bfc8d2..3dd1f4724e 100644 --- a/pipeline_plugins/components/utils/sites/open/utils.py +++ b/pipeline_plugins/components/utils/sites/open/utils.py @@ -26,6 +26,7 @@ ip_pattern, format_sundry_ip ) +from pipeline_plugins.cmdb_ip_picker.utils import get_bk_cloud_id_for_host from gcloud.conf import settings logger = logging.getLogger('root') @@ -74,17 +75,17 @@ def cc_get_ips_info_by_str(username, biz_cc_id, ip_str, use_cache=True): ip_list = cc_get_ip_list_by_biz_and_user(username=username, biz_cc_id=biz_cc_id, use_cache=use_cache) for ip_info in ip_list: set_dict = {s['bk_set_id']: s for s in ip_info['set']} - for module in ip_info['module']: - if '%s|%s|%s' % (set_dict[module['bk_set_id']]['bk_set_name'], - module['bk_module_name'], + for mod in ip_info['module']: + if '%s|%s|%s' % (set_dict[mod['bk_set_id']]['bk_set_name'], + mod['bk_module_name'], ip_info['host']['bk_host_innerip']) in set_module_ip: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], - 'Source': ip_info['host']['bk_cloud_id'][0]['id'], - 'SetID': module['bk_set_id'], - 'SetName': set_dict[module['bk_set_id']]['bk_set_name'], - 'ModuleID': module['bk_module_id'], - 'ModuleName': module['bk_module_name'], + 'Source': get_bk_cloud_id_for_host(ip_info['host'], 'bk_cloud_id'), + 'SetID': mod['bk_set_id'], + 'SetName': set_dict[mod['bk_set_id']]['bk_set_name'], + 'ModuleID': mod['bk_module_id'], + 'ModuleName': mod['bk_module_name'], }) # 如果是格式3,返回IP的平台信息 @@ -95,10 +96,11 @@ def cc_get_ips_info_by_str(username, biz_cc_id, ip_str, use_cache=True): ip_list = cc_get_ip_list_by_biz_and_user(username=username, biz_cc_id=biz_cc_id, use_cache=use_cache) for ip_info in ip_list: - if '%s:%s' % (ip_info['host']['bk_cloud_id'][0]['id'], ip_info['host']['bk_host_innerip']) in plat_ip: + if '%s:%s' % (get_bk_cloud_id_for_host(ip_info['host'], 'bk_cloud_id'), + ip_info['host']['bk_host_innerip']) in plat_ip: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], - 'Source': ip_info['host']['bk_cloud_id'][0]['id'], + 'Source': get_bk_cloud_id_for_host(ip_info['host'], 'bk_cloud_id'), }) else: @@ -112,7 +114,7 @@ def cc_get_ips_info_by_str(username, biz_cc_id, ip_str, use_cache=True): if ip_info['host']['bk_host_innerip'] in ip and ip_info['host']['bk_host_id'] not in host_id_list: ip_result.append({'InnerIP': ip_info['host']['bk_host_innerip'], 'HostID': ip_info['host']['bk_host_id'], - 'Source': ip_info['host']['bk_cloud_id'][0]['id'], + 'Source': get_bk_cloud_id_for_host(ip_info['host'], 'bk_cloud_id'), }) host_id_list.append(ip_info['host']['bk_host_id'])