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 5b31b20 commit 3e89347
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 36 deletions.
36 changes: 0 additions & 36 deletions apps/backend/subscription/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,53 +989,17 @@ def get_instances_by_scope(scope: Dict[str, Union[Dict, int, Any]]) -> Dict[str,
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(
{
Expand Down
26 changes: 26 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()

from apps.backend.subscription.tools import get_instances_by_scope

scope = {
"bk_biz_id": None,
"object_type": "HOST",
"node_type": "DYNAMIC_GROUP",
"nodes": [
{
"bk_biz_id": 5,
"bk_group_id": "544a9026-b2b4-11ef-9ead-fa9249cab81d",
}
],
"need_register": False,
"instance_selector": None,
"with_info": {
"process": False
}
}

get_instances_by_scope(scope=scope)
132 changes: 132 additions & 0 deletions test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()

from apps.backend.subscription.tools import get_instances_by_scope

scope = {
"bk_biz_id": None,
"object_type": "HOST",
"node_type": "INSTANCE",
"nodes": [
{
"bk_biz_id": 1,
"bk_host_id": 205,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 210,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 212,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 213,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 214,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 216,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 221,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 223,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 224,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 240,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 313,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 388,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 389,
"ip": None
},
{
"bk_biz_id": 1,
"bk_host_id": 431,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 458,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 459,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 481,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 482,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 484,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 485,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 487,
"ip": None
},
{
"bk_biz_id": 5,
"bk_host_id": 488,
"ip": None
}
],
"need_register": False,
"instance_selector": None,
"with_info": {
"process": False
}
}

get_instances_by_scope(scope=scope)
30 changes: 30 additions & 0 deletions test3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()

from apps.backend.subscription.tools import get_instances_by_scope

scope = {
"bk_biz_id": 5,
"object_type": "HOST",
"node_type": "DYNAMIC_GROUP",
"nodes": [
{
"bk_biz_id": 5,
"bk_group_id": "416454dc-b2db-11ef-9ead-fa9249cab81d",
},
{
"bk_biz_id": 5,
"bk_group_id": "717017fc-b2b4-11ef-9ead-fa9249cab81d",
}
],
"need_register": False,
"instance_selector": None,
"with_info": {
"process": False
}
}

get_instances_by_scope(scope=scope)
30 changes: 30 additions & 0 deletions test4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()

from apps.backend.subscription.tools import get_instances_by_scope

scope = {
"bk_biz_id": 5,
"object_type": "HOST",
"node_type": "DYNAMIC_GROUP",
"nodes": [
{
"bk_biz_id": 5,
"bk_group_id": "416454dc-b2db-11ef-9ead-fa9249cab81d",
},
{
"bk_biz_id": 5,
"bk_group_id": "544a9026-b2b4-11ef-9ead-fa9249cab81d",
}
],
"need_register": False,
"instance_selector": None,
"with_info": {
"process": False
}
}

get_instances_by_scope(scope=scope)

0 comments on commit 3e89347

Please sign in to comment.