Skip to content

Commit

Permalink
feat: 订阅下发支持动态分组 (closed #2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
ping15 committed Dec 20, 2024
1 parent a5971a5 commit 5b31b20
Showing 1 changed file with 128 additions and 15 deletions.
143 changes: 128 additions & 15 deletions apps/backend/subscription/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ def get_host_detail_by_template(bk_obj_id, template_info_list: list, bk_biz_id:
host_info_result = batch_request(
call_func, dict(bk_service_template_ids=template_ids, bk_biz_id=bk_biz_id, fields=fields)
)
elif bk_obj_id == models.Subscription.NodeType.DYNAMIC_GROUP:
call_func = client_v2.cc.find_host_by_set_template
template_ids = [info["bk_inst_id"] for info in template_info_list]
bk_set_ids = [info["bk_set_id"] for info in template_info_list]
host_info_result = batch_request(
call_func, dict(bk_set_template_ids=template_ids, bk_set_ids=bk_set_ids, bk_biz_id=bk_biz_id, fields=fields)
)
else:
# 集群模板
call_func = client_v2.cc.find_host_by_set_template
Expand Down Expand Up @@ -841,7 +848,7 @@ def get_instances_by_scope_with_checker(
return get_instances_by_scope(scope, *args, **kwargs)


@support_multi_biz
# @support_multi_biz
@SetupObserve(histogram=metrics.app_task_get_instances_by_scope_duration_seconds, get_labels_func=get_scope_labels_func)
@FuncCacheDecorator(cache_time=SUBSCRIPTION_SCOPE_CACHE_TIME)
def get_instances_by_scope(scope: Dict[str, Union[Dict, int, Any]]) -> Dict[str, Dict[str, Union[Dict, Any]]]:
Expand Down Expand Up @@ -968,20 +975,126 @@ def get_instances_by_scope(scope: Dict[str, Union[Dict, int, Any]]) -> Dict[str,
elif scope["node_type"] == models.Subscription.NodeType.DYNAMIC_GROUP:
if scope["object_type"] == models.Subscription.ObjectType.HOST:
"""主机动态分组"""
# nodes = [
# {
# "bk_biz_id": 1,
# "group_id": "xxx",
# },
# {
# "bk_biz_id": 2,
# "group_id": "yyy",
# }
# ]

#

# 3. 当bk_obj_id为set时

host_condition, set_condition = [], []

for node in nodes:
res = CCApi.get_dynamic_group(
format_kwargs={
"bk_biz_id": node["bk_biz_id"],
"id": node["bk_group_id"],
}
)

conditions = []
for cond_info in res["info"]["condition"] + res["info"]["variable_condition"]:
conditions += cond_info["condition"]
conditions = [
cond
for cond_info in res["info"]["condition"] + res["info"]["variable_condition"]
for cond in cond_info["condition"]
]

if res["bk_obj_id"] == "host":
host_condition.extend(conditions)

elif res["bk_obj_id"] == "set":
# 使用字典映射来替换运算符
operator_mapping = {"$in": "in", "$nin": "not_in", "$regex": "contains"}
for condition in conditions:
condition["operator"] = operator_mapping.get(condition["operator"], condition["operator"])

set_condition.extend(conditions)

# set_infos = CCApi.search_set_v2(
# params={
# "filter": {
# "condition": "AND",
# "rules": conditions
# }
# },
# format_kwargs={
# "bk_supplier_account": "0",
# "bk_biz_id": node["bk_biz_id"],
# }
# )["info"]
#
# instances.extend(
# [
# {"host": inst}
# for inst in get_host_detail_by_template(
# bk_obj_id=models.Subscription.NodeType.DYNAMIC_GROUP,
# template_info_list=[
# {
# "bk_set_id": set_info["bk_set_id"],
# "bk_inst_id": set_info["set_template_id"],
# }
# for set_info in set_infos
# ],
# bk_biz_id=bk_biz_id
# )
# ]
# )

if host_condition:
res = CCApi.post_findmany_hosts_search_with_biz(
{
"bk_biz_id": bk_biz_id,
"condition": [
{
"bk_obj_id": "host",
"fields": [
"bk_host_id",
],
"condition": host_condition,
}
],
}
)

instances.extend(
[
{"host": inst}
for inst in get_host_detail(
[
{
"bk_biz_id": bk_biz_id,
"bk_host_id": host_info["host"]["bk_host_id"],
}
for host_info in res["info"]
],
bk_biz_id=bk_biz_id,
source="get_instances_by_scope",
)
]
)

if set_condition:
set_infos = CCApi.search_set_v2(
params={"filter": {"condition": "OR", "rules": set_condition}},
format_kwargs={
"bk_supplier_account": "0",
"bk_biz_id": bk_biz_id,
},
)["info"]

instances.extend(
[
{"host": inst}
for inst in get_host_detail_by_template(
bk_obj_id=models.Subscription.NodeType.DYNAMIC_GROUP,
template_info_list=[
{
"bk_set_id": set_info["bk_set_id"],
"bk_inst_id": set_info["set_template_id"],
}
for set_info in set_infos
],
bk_biz_id=bk_biz_id,
)
]
)

else:
"""服务动态分组"""

Expand Down

0 comments on commit 5b31b20

Please sign in to comment.