Skip to content

Commit e0e65b8

Browse files
committed
feat: 配置平台(CMDB) 相关插件支持 云区域:IP #7432
1 parent 815bcb6 commit e0e65b8

File tree

2 files changed

+77
-23
lines changed
  • pipeline_plugins/components/collections/sites/open/cc

2 files changed

+77
-23
lines changed

pipeline_plugins/components/collections/sites/open/cc/base.py

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import logging
1515
from abc import ABCMeta
16+
from collections import Counter
1617
from enum import Enum
1718
from functools import partial
1819

@@ -31,6 +32,7 @@
3132
get_ip_by_regex_type,
3233
ip_pattern,
3334
ipv6_pattern,
35+
plat_ip_reg,
3436
)
3537
from pipeline_plugins.components.collections.sites.open.cc.ipv6_utils import (
3638
get_hosts_by_hosts_ids,
@@ -88,7 +90,7 @@ class ModuleCreateMethod(Enum):
8890

8991

9092
def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
91-
"""根据主机内网 IP 获取主机 ID
93+
"""根据主机内网cloud_id:IP 获取主机 ID
9294
9395
:param executor: API 请求用户身份
9496
:type executor: string
@@ -107,7 +109,7 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
107109
executor,
108110
bk_biz_id,
109111
supplier_account,
110-
["bk_host_id", "bk_host_innerip", "bk_cloud_id"],
112+
["bk_host_id", "bk_host_innerip"],
111113
ip_list,
112114
)
113115

@@ -118,20 +120,14 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
118120

119121
if len(host_list) > len(ip_list):
120122
# find repeat innerip host
121-
hosts = []
122-
c_hosts = []
123-
for host in host_list:
124-
if {host["bk_host_innerip"]: host.get("bk_cloud_id", "")} in hosts:
125-
c_hosts.append(host["bk_host_innerip"])
126-
else:
127-
hosts.append({host["bk_host_innerip"]: host.get("bk_cloud_id", "")})
128-
if len(c_hosts) > 0:
129-
message = _(f"IP [{', '.join(c_hosts)}] 在本业务下重复: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip")
130-
logger.error(message)
131-
return {
132-
"result": False,
133-
"message": message,
134-
}
123+
host_counter = Counter([host["bk_host_innerip"] for host in host_list])
124+
mutiple_innerip_hosts = [innerip for innerip, count in host_counter.items() if count > 1]
125+
message = _(f"IP [{', '.join(mutiple_innerip_hosts)}] 在本业务下重复: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip")
126+
logger.error(message)
127+
return {
128+
"result": False,
129+
"message": message,
130+
}
135131

136132
if len(host_list) < len(ip_list):
137133
return_innerip_set = {host["bk_host_innerip"] for host in host_list}
@@ -143,6 +139,70 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
143139
return {"result": True, "data": [str(host["bk_host_id"]) for host in host_list]}
144140

145141

142+
def cc_get_host_id_by_str(executor, bk_biz_id, ip_str, supplier_account):
143+
"""
144+
:param executor: API 请求用户身份
145+
:type executor: string
146+
:param bk_biz_id: 业务 CC ID
147+
:type bk_biz_id: int
148+
:param ip_list: 主机内网 IP 列表
149+
:type ip_list: list
150+
:param supplier_account: 开发商账号
151+
:type supplier_account: int
152+
:return: 主机 id 列表
153+
@note: 需要兼容的ip_str格式有
154+
1: IP,纯IP格式
155+
3: 管控区域ID:IP
156+
:return: 主机 id 列表
157+
:rtype: list
158+
["1", "2", "3", ...]
159+
"""
160+
ip_list = get_ip_by_regex(ip_str)
161+
# 格式2 管控区域ID:IP
162+
if plat_ip_reg.match(ip_str):
163+
host_list = cmdb.get_business_host(
164+
executor,
165+
bk_biz_id,
166+
supplier_account,
167+
["bk_host_id", "bk_host_innerip", "bk_cloud_id"],
168+
ip_list,
169+
)
170+
171+
if not host_list:
172+
message = _(f"IP {ip_list} 在本业务下不存在: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip")
173+
logger.error(message)
174+
return {"result": False, "message": message}
175+
176+
if len(host_list) > len(ip_list):
177+
# find repeat innerip host
178+
hosts = []
179+
c_hosts = []
180+
for host in host_list:
181+
if {host["bk_host_innerip"]: host.get("bk_cloud_id", "")} in hosts:
182+
c_hosts.append(host["bk_host_innerip"])
183+
else:
184+
hosts.append({host["bk_host_innerip"]: host.get("bk_cloud_id", "")})
185+
if len(c_hosts) > 0:
186+
message = _(f"IP [{', '.join(c_hosts)}] 在本业务下重复: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip")
187+
logger.error(message)
188+
return {
189+
"result": False,
190+
"message": message,
191+
}
192+
193+
if len(host_list) < len(ip_list):
194+
return_innerip_set = {host["bk_host_innerip"] for host in host_list}
195+
absent_innerip = set(ip_list).difference(return_innerip_set)
196+
message = _(f"IP [{', '.join(absent_innerip)}] 在本业务下不存在: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip")
197+
logger.error(message)
198+
return {"result": False, "message": message}
199+
200+
return {"result": True, "data": [str(host["bk_host_id"]) for host in host_list]}
201+
# 格式1 纯IP格式
202+
else:
203+
return cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account)
204+
205+
146206
def cc_get_host_by_innerip_with_ipv6(
147207
executor, bk_biz_id, ip_str, supplier_account, is_biz_set=False, host_id_detail=False
148208
):
@@ -463,8 +523,7 @@ def get_host_list(self, executor, biz_cc_id, ip_str, supplier_account):
463523
if not host_result["result"]:
464524
return host_result
465525
return {"result": True, "data": [str(host["bk_host_id"]) for host in host_result["data"]]}
466-
ip_list = get_ip_by_regex(ip_str)
467-
return cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account)
526+
return cc_get_host_id_by_str(executor, biz_cc_id, ip_str, supplier_account)
468527

469528
def get_ip_info_list(self, executor, biz_cc_id, ip_str, supplier_account):
470529
"""

pipeline_plugins/components/collections/sites/open/cc/batch_update_host/v1_0.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from pipeline_plugins.base.utils.inject import supplier_account_for_business
2525
from pipeline_plugins.components.collections.sites.open.cc.base import CCPluginIPMixin, cc_format_prop_data
2626
from pipeline_plugins.components.utils import chunk_table_data, convert_num_to_str
27-
from pipeline_plugins.components.utils.sites.open.utils import plat_ip_reg
2827

2928
logger = logging.getLogger("celery")
3029
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
@@ -123,10 +122,6 @@ def execute(self, data, parent_data):
123122
update_host_message = []
124123
for host_property_dir in host_property_copy:
125124
inner_host_ip = host_property_dir["bk_host_innerip"]
126-
# 兼容填写管控区域ID:IP的情况, 只获取对应IP, 判断ipv4
127-
if plat_ip_reg.match(inner_host_ip) and ":" in inner_host_ip:
128-
inner_host_ip = inner_host_ip.split(":")[1]
129-
130125
host_result = self.get_host_list(executor, biz_cc_id, inner_host_ip, supplier_account)
131126
if not host_result["result"]:
132127
data.outputs.ex_data = host_result.get("message")

0 commit comments

Comments
 (0)