Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions pipeline_plugins/components/collections/sites/open/cc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
get_ip_by_regex_type,
ip_pattern,
ipv6_pattern,
plat_ip_reg,
)
from pipeline_plugins.components.collections.sites.open.cc.ipv6_utils import (
get_hosts_by_hosts_ids,
Expand Down Expand Up @@ -138,6 +139,70 @@ def cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account):
return {"result": True, "data": [str(host["bk_host_id"]) for host in host_list]}


def cc_get_host_id_by_str(executor, bk_biz_id, ip_str, supplier_account):
"""
:param executor: API 请求用户身份
:type executor: string
:param bk_biz_id: 业务 CC ID
:type bk_biz_id: int
:param ip_str: 主机内网 IP
:type ip_str: string
:param supplier_account: 开发商账号
:type supplier_account: int
:return: 主机 id 列表
@note: 需要兼容的ip_str格式有
1: IP,纯IP格式
2: 管控区域ID:IP
:return: 主机 id 列表
:rtype: list
["1", "2", "3", ...]
"""
ip_list = get_ip_by_regex(ip_str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看实现,这个函数只能识别不带云区域的字符串

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ip_list = ["0:1.1.1.1", "2.2.2.2"]

0:1:1.1.1.1 1:2:2.2.2.2

len(host_list) == len(ip_list)

# 格式2 管控区域ID:IP
if plat_ip_reg.match(ip_str):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里如果部分带了部分没带,看上去有问题

host_list = cmdb.get_business_host(
executor,
bk_biz_id,
supplier_account,
["bk_host_id", "bk_host_innerip", "bk_cloud_id"],
ip_list,
)

if not host_list:
message = _(f"IP {ip_list} 在本业务下不存在: 请检查配置, 修复后重新执行 | cc_get_host_id_by_str")
logger.error(message)
return {"result": False, "message": message}

if len(host_list) > len(ip_list):
# find repeat innerip host
hosts = []
c_hosts = []
for host in host_list:
if {host["bk_host_innerip"]: host.get("bk_cloud_id", "")} in hosts:
c_hosts.append(host["bk_host_innerip"])
else:
hosts.append({host["bk_host_innerip"]: host.get("bk_cloud_id", "")})
if len(c_hosts) > 0:
message = _(f"IP [{', '.join(c_hosts)}] 在本业务下重复: 请检查配置, 修复后重新执行 | cc_get_host_id_by_str")
logger.error(message)
return {
"result": False,
"message": message,
}

if len(host_list) < len(ip_list):
return_innerip_set = {host["bk_host_innerip"] for host in host_list}
absent_innerip = set(ip_list).difference(return_innerip_set)
message = _(f"IP [{', '.join(absent_innerip)}] 在本业务下不存在: 请检查配置, 修复后重新执行 | cc_get_host_id_by_str")
logger.error(message)
return {"result": False, "message": message}

return {"result": True, "data": [str(host["bk_host_id"]) for host in host_list]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

host_list 看上去会有冗余,这里不合理。假如1.1.1.1的机器有两台,那么这里我填了0:1.1.1.1之后,会得到两个 host_id

# 格式1 纯IP格式
else:
return cc_get_host_id_by_innerip(executor, bk_biz_id, ip_list, supplier_account)


def cc_get_host_by_innerip_with_ipv6(
executor, bk_biz_id, ip_str, supplier_account, is_biz_set=False, host_id_detail=False
):
Expand Down Expand Up @@ -458,8 +523,7 @@ def get_host_list(self, executor, biz_cc_id, ip_str, supplier_account):
if not host_result["result"]:
return host_result
return {"result": True, "data": [str(host["bk_host_id"]) for host in host_result["data"]]}
ip_list = get_ip_by_regex(ip_str)
return cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account)
return cc_get_host_id_by_str(executor, biz_cc_id, ip_str, supplier_account)

def get_ip_info_list(self, executor, biz_cc_id, ip_str, supplier_account):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from pipeline_plugins.base.utils.inject import supplier_account_for_business
from pipeline_plugins.components.collections.sites.open.cc.base import CCPluginIPMixin, cc_format_prop_data
from pipeline_plugins.components.utils import chunk_table_data, convert_num_to_str
from pipeline_plugins.components.utils.sites.open.utils import plat_ip_reg

logger = logging.getLogger("celery")
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
Expand Down Expand Up @@ -123,16 +122,10 @@ def execute(self, data, parent_data):
update_host_message = []
for host_property_dir in host_property_copy:
inner_host_ip = host_property_dir["bk_host_innerip"]
# 兼容填写管控区域ID:IP的情况, 只获取对应IP, 判断ipv4
if plat_ip_reg.match(inner_host_ip) and ":" in inner_host_ip:
inner_host_ip = inner_host_ip.split(":")[1]

host_result = self.get_host_list(executor, biz_cc_id, inner_host_ip, supplier_account)
if not host_result["result"]:
data.outputs.ex_data = host_result.get("message")
return False
host_id = int(host_result["data"][0])
host_update = {"bk_host_id": host_id}
host_property_dir.pop("bk_host_innerip")
properties = {}
for cc_host_property, cc_host_prop_value in host_property_dir.items():
Expand All @@ -145,8 +138,9 @@ def execute(self, data, parent_data):
self.logger.error(ex_data)
return False
properties[cc_host_property] = cc_host_prop_value
host_update["properties"] = properties
update_host_message.append(host_update)

for host_id in host_result["data"]:
update_host_message.append({"bk_host_id": int(host_id), "properties": properties})

cc_kwargs = {"bk_supplier_account": supplier_account, "update": update_host_message}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
specific language governing permissions and limitations under the License.
"""

from mock import MagicMock, patch

from django.test import TestCase
from mock import MagicMock, patch

from pipeline_plugins.components.collections.sites.open.cc.base import cc_get_host_id_by_innerip

Expand Down
Loading