Skip to content

Commit

Permalink
feat: IP 选择器接口兼容 cc 字段命名规范 (closed TencentBlueKing#2162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Apr 9, 2024
1 parent fa53792 commit 817d3cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
69 changes: 38 additions & 31 deletions apps/core/ipchooser/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def format2tree_node(node: types.ReadableTreeNode) -> types.TreeNode:
def get_meta_data(bk_biz_id: int) -> types.MetaData:
return {"scope_type": constants.ScopeType.BIZ.value, "scope_id": str(bk_biz_id), "bk_biz_id": bk_biz_id}

@classmethod
def fill_cc_field(cls, host_info: types.HostInfo):
host_info["bk_host_id"] = host_info["host_id"]
host_info["bk_biz_id"] = host_info["biz"]["id"]
host_info["bk_agent_id"] = host_info["agent_id"]
host_info["bk_agent_alive"] = host_info["alive"]
host_info["bk_cloud_id"] = host_info["cloud_area"]["id"]

@classmethod
def format_hosts(cls, untreated_host_infos: typing.List[types.HostInfo]) -> typing.List[types.FormatHostInfo]:
"""
Expand Down Expand Up @@ -62,37 +70,36 @@ def format_hosts(cls, untreated_host_infos: typing.List[types.HostInfo]) -> typi

treated_host_infos: typing.List[types.HostInfo] = []
for untreated_host_info in untreated_host_infos:
treated_host_infos.append(
{
"meta": BaseHandler.get_meta_data(untreated_host_info["bk_biz_id"]),
"host_id": untreated_host_info["bk_host_id"],
"agent_id": untreated_host_info["bk_agent_id"],
"ip": untreated_host_info["inner_ip"],
"ipv6": untreated_host_info["inner_ipv6"],
"host_name": untreated_host_info["bk_host_name"],
"os_name": node_man_constants.OS_CHN.get(
untreated_host_info["os_type"], untreated_host_info["os_type"]
treated_host_info: types.HostInfo = {
"meta": BaseHandler.get_meta_data(untreated_host_info["bk_biz_id"]),
"host_id": untreated_host_info["bk_host_id"],
"agent_id": untreated_host_info["bk_agent_id"],
"ip": untreated_host_info["inner_ip"],
"ipv6": untreated_host_info["inner_ipv6"],
"host_name": untreated_host_info["bk_host_name"],
"os_name": node_man_constants.OS_CHN.get(
untreated_host_info["os_type"], untreated_host_info["os_type"]
),
"os_type": node_man_constants.OS_CHN.get(
untreated_host_info["os_type"], untreated_host_info["os_type"]
),
# TODO 实时获取状态
"alive": (constants.AgentStatusType.NO_ALIVE.value, constants.AgentStatusType.ALIVE.value)[
untreated_host_info["status"] == node_man_constants.ProcStateType.RUNNING
],
"cloud_area": {
"id": untreated_host_info["bk_cloud_id"],
"name": cloud_id__info_map.get(untreated_host_info["bk_cloud_id"], {}).get(
"bk_cloud_name", untreated_host_info["bk_cloud_id"]
),
"os_type": node_man_constants.OS_CHN.get(
untreated_host_info["os_type"], untreated_host_info["os_type"]
},
"biz": {
"id": untreated_host_info["bk_biz_id"],
"name": biz_id__info_map.get(untreated_host_info["bk_biz_id"], {}).get(
"bk_biz_name", untreated_host_info["bk_biz_id"]
),
# TODO 实时获取状态
"alive": (constants.AgentStatusType.NO_ALIVE.value, constants.AgentStatusType.ALIVE.value)[
untreated_host_info["status"] == node_man_constants.ProcStateType.RUNNING
],
"cloud_area": {
"id": untreated_host_info["bk_cloud_id"],
"name": cloud_id__info_map.get(untreated_host_info["bk_cloud_id"], {}).get(
"bk_cloud_name", untreated_host_info["bk_cloud_id"]
),
},
"biz": {
"id": untreated_host_info["bk_biz_id"],
"name": biz_id__info_map.get(untreated_host_info["bk_biz_id"], {}).get(
"bk_biz_name", untreated_host_info["bk_biz_id"]
),
},
}
)

},
}
cls.fill_cc_field(treated_host_info)
treated_host_infos.append(treated_host_info)
return treated_host_infos
7 changes: 7 additions & 0 deletions apps/core/ipchooser/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ class HostInfoWithMetaSer(serializers.Serializer):
ip = serializers.IPAddressField(help_text=_("IPv4 协议下的主机IP"), required=False, protocol="ipv4")
host_id = serializers.IntegerField(help_text=_("主机 ID,优先取 `host_id`,否则取 `ip` + `cloud_id`"), required=False)

bk_cloud_id = serializers.IntegerField(help_text=_("管控区域 ID"), required=False)
bk_host_id = serializers.IntegerField(help_text=_("主机 ID,优先取 `host_id`,否则取 `ip` + `cloud_id`"), required=False)

def validate(self, attrs):
field_map: typing.Dict[str, str] = {"bk_cloud_id": "cloud_id", "bk_host_id": "host_id"}
for compatible_field, field in field_map.items():
if compatible_field in attrs:
attrs[field] = attrs[compatible_field]
if not ("host_id" in attrs or ("ip" in attrs and "cloud_id" in attrs)):
raise exceptions.SerValidationError(_("请传入 host_id 或者 cloud_id + ip"))
return attrs

0 comments on commit 817d3cf

Please sign in to comment.