Skip to content

Commit

Permalink
Merge pull request #1667 from homholueng/V3.4.X_bugfix_ip_selector_cl…
Browse files Browse the repository at this point in the history
…oud_empty_500

V3.4.x bugfix ip selector cloud empty 500
  • Loading branch information
homholueng committed Jan 6, 2020
2 parents 52c683e + 7da44a6 commit 8914fa1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
7 changes: 4 additions & 3 deletions pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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']:
Expand All @@ -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)

Expand Down
19 changes: 16 additions & 3 deletions pipeline_plugins/cmdb_ip_picker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -56,22 +57,22 @@ 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 = []
data = []
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
})

Expand Down Expand Up @@ -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']
24 changes: 13 additions & 11 deletions pipeline_plugins/components/utils/sites/open/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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的平台信息
Expand All @@ -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:
Expand All @@ -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'])

Expand Down

0 comments on commit 8914fa1

Please sign in to comment.