13
13
14
14
import logging
15
15
from abc import ABCMeta
16
+ from collections import Counter
16
17
from enum import Enum
17
18
from functools import partial
18
19
31
32
get_ip_by_regex_type ,
32
33
ip_pattern ,
33
34
ipv6_pattern ,
35
+ plat_ip_reg ,
34
36
)
35
37
from pipeline_plugins .components .collections .sites .open .cc .ipv6_utils import (
36
38
get_hosts_by_hosts_ids ,
@@ -88,7 +90,7 @@ class ModuleCreateMethod(Enum):
88
90
89
91
90
92
def cc_get_host_id_by_innerip (executor , bk_biz_id , ip_list , supplier_account ):
91
- """根据主机内网 IP 获取主机 ID
93
+ """根据主机内网cloud_id: IP 获取主机 ID
92
94
93
95
:param executor: API 请求用户身份
94
96
:type executor: string
@@ -107,7 +109,7 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
107
109
executor ,
108
110
bk_biz_id ,
109
111
supplier_account ,
110
- ["bk_host_id" , "bk_host_innerip" , "bk_cloud_id" ],
112
+ ["bk_host_id" , "bk_host_innerip" ],
111
113
ip_list ,
112
114
)
113
115
@@ -118,20 +120,14 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
118
120
119
121
if len (host_list ) > len (ip_list ):
120
122
# 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
+ }
135
131
136
132
if len (host_list ) < len (ip_list ):
137
133
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):
143
139
return {"result" : True , "data" : [str (host ["bk_host_id" ]) for host in host_list ]}
144
140
145
141
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
+
146
206
def cc_get_host_by_innerip_with_ipv6 (
147
207
executor , bk_biz_id , ip_str , supplier_account , is_biz_set = False , host_id_detail = False
148
208
):
@@ -463,8 +523,7 @@ def get_host_list(self, executor, biz_cc_id, ip_str, supplier_account):
463
523
if not host_result ["result" ]:
464
524
return host_result
465
525
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 )
468
527
469
528
def get_ip_info_list (self , executor , biz_cc_id , ip_str , supplier_account ):
470
529
"""
0 commit comments