Skip to content

Commit

Permalink
feat: 配置平台(CMDB) 相关插件支持 云区域:IP #7432
Browse files Browse the repository at this point in the history
  • Loading branch information
lTimej committed Aug 19, 2024
1 parent 58349d4 commit 0e52170
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion gcloud/utils/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_business_host(username, bk_biz_id, supplier_account, host_fields, ip_lis
kwargs = {"bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or [])}

# 带管控区域的主机数据查询
if ip_list and bk_cloud_id:
if ip_list and bk_cloud_id is not None:
kwargs["host_property_filter"] = {
"condition": "AND",
"rules": [
Expand Down
94 changes: 45 additions & 49 deletions pipeline_plugins/components/collections/sites/open/cc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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 @@ -139,68 +138,47 @@ 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):
"""
def cc_get_host_id_by_innerip_and_cloudid(executor, bk_biz_id, ip_str, supplier_account):
"""根据主机内网 cloudid:IP 获取主机 ID
:param executor: API 请求用户身份
:type executor: string
:param bk_biz_id: 业务 CC ID
:type bk_biz_id: int
:param ip_str: 主机内网 IP
:param ip_list: 主机内网 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)
# 格式2 管控区域ID:IP
if plat_ip_reg.match(ip_str):
host_list = cmdb.get_business_host(
executor,
bk_biz_id,
supplier_account,
["bk_host_id", "bk_host_innerip", "bk_cloud_id"],
ip_list,
)
cloud_id = int(ip_str.split(":")[0])
host_list = cmdb.get_business_host(
executor,
bk_biz_id,
supplier_account,
["bk_host_id", "bk_host_innerip", "bk_cloud_id"],
ip_list,
cloud_id,
)

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 not host_list:
message = _(f"IP {ip_list} 在本业务下不存在: 请检查配置, 修复后重新执行 | cc_get_host_id_by_innerip_and_cloudid")
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}
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_innerip_and_cloudid"
)
logger.error(message)
return {"result": False, "message": message}

return {"result": True, "data": [str(host["bk_host_id"]) for host in host_list]}
# 格式1 纯IP格式
else:
return 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_by_innerip_with_ipv6(
Expand Down Expand Up @@ -523,7 +501,8 @@ 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"]]}
return cc_get_host_id_by_str(executor, biz_cc_id, ip_str, supplier_account)
ip_list = get_ip_by_regex(ip_str)
return cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account)

def get_ip_info_list(self, executor, biz_cc_id, ip_str, supplier_account):
"""
Expand Down Expand Up @@ -583,6 +562,23 @@ def get_host_topo(self, executor, biz_cc_id, supplier_account, host_attrs, ip_st
executor, biz_cc_id, supplier_account, host_attrs, ip_list=None, property_filters=property_filters
)

def get_host_list_by_cloud_id(self, executor, biz_cc_id, ip_str, supplier_account):
"""
获取host_list
@param executor: executor 执行人
@param biz_cc_id: biz_cc_id 业务id
@param ip_str: ip_str ip字符串
@param supplier_account: supplier_account
@return:
"""
# 如果开启IPV6
if settings.ENABLE_IPV6:
host_result = cc_get_host_by_innerip_with_ipv6(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"]]}
return cc_get_host_id_by_innerip_and_cloudid(executor, biz_cc_id, ip_str, supplier_account)


class BaseTransferHostToModuleService(Service, CCPluginIPMixin, metaclass=ABCMeta):
def inputs_format(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 @@ -122,7 +123,11 @@ 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"]
host_result = self.get_host_list(executor, biz_cc_id, inner_host_ip, supplier_account)
# 兼容填写管控区域ID:IP的情况, 只获取对应IP, 判断ipv4
if plat_ip_reg.match(inner_host_ip) and ":" in inner_host_ip:
host_result = self.get_host_list_by_cloud_id(executor, biz_cc_id, inner_host_ip, supplier_account)
else:
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@

from django.utils import translation
from django.utils.translation import ugettext_lazy as _

from pipeline.component_framework.component import Component
from pipeline.core.flow.activity import Service
from pipeline.core.flow.io import StringItemSchema
from pipeline.component_framework.component import Component

from pipeline_plugins.base.utils.inject import supplier_account_for_business
from pipeline_plugins.components.collections.sites.open.cc.base import (
cc_format_prop_data,
CCPluginIPMixin,
)

from gcloud.conf import settings
from gcloud.utils.handlers import handle_api_error
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.common import chunk_table_data
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 @@ -79,12 +76,28 @@ def execute(self, data, parent_data):
supplier_account = supplier_account_for_business(biz_cc_id)

# 查询主机id
ip_list = data.get_one_of_inputs("cc_host_ip")
host_result = self.get_host_list(executor, biz_cc_id, ip_list, supplier_account)
if not host_result["result"]:
data.set_outputs("ex_data", host_result["message"])
host_property = []
host_ip = data.get_one_of_inputs("cc_host_ip")
# 根据换行符分割
column_result = chunk_table_data({"bk_host_innerip": host_ip}, "\n")
if not column_result["result"]:
message = _(f"单行扩展失败: 请检查输入参数格式是否合法, 修复后重试. 错误内容: {column_result['message']}")
data.outputs.ex_data = message
self.logger.error(message)
return False

host_property.extend(column_result["data"])
hosts = []
for host_property_dir in host_property:
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:
host_result = self.get_host_list_by_cloud_id(executor, biz_cc_id, inner_host_ip, supplier_account)
else:
host_result = self.get_host_list(executor, biz_cc_id, inner_host_ip, supplier_account)
if not host_result["result"]:
data.set_outputs("ex_data", host_result["message"])
return False
hosts.extend(host_result["data"])
# 更新主机属性
cc_host_property = data.get_one_of_inputs("cc_host_property")
if cc_host_property == "bk_isp_name":
Expand Down Expand Up @@ -142,7 +155,7 @@ def execute(self, data, parent_data):
break

cc_kwargs = {
"bk_host_id": ",".join(host_result["data"]),
"bk_host_id": ",".join(hosts),
"bk_supplier_account": supplier_account,
"data": {cc_host_property: cc_host_prop_value},
}
Expand Down

0 comments on commit 0e52170

Please sign in to comment.