Skip to content

Commit

Permalink
节点管理-免评审: 节点管理「管控区域」「agent版本」筛选能力优化 (closed TencentBlueKing#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Huayeaaa committed Dec 13, 2023
1 parent 31d29ee commit 7df5e1b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
19 changes: 16 additions & 3 deletions apps/node_man/handlers/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,30 @@ def fetch_host_process_unique_col(

return cursor

def fetch_host_condition(self):
def fetch_host_condition(self, params):
"""
获取Host接口的条件
:param username: 用户名
:param params: 参数
:return: Host接口所有条件
"""

# 用户有权限的业务
biz_id_name = CmdbHandler().biz_id_name({"action": constants.IamActionType.agent_view})
biz_permission = list(biz_id_name.keys())

params = params or {}
bk_biz_ids = params.get("bk_biz_ids", [])
# 传入业务id列表的情况
if bk_biz_ids:
biz_permission = list(set(bk_biz_ids) & set(biz_id_name.keys()))

if not biz_permission:
return [
{"name": _("IP"), "id": "ip"},
{"name": _("管控区域ID:IP"), "id": "bk_cloud_ip"},
{"name": _("主机名称"), "id": "bk_host_name"},
]

bk_cloud_ids = set()
os_types = set()
is_manuals = set()
Expand Down Expand Up @@ -520,7 +533,7 @@ def filter_condition(self, category, params=None):
"""

if category == "host":
return self.fetch_host_condition()
return self.fetch_host_condition(params=params)
elif category == "job":
return self.fetch_job_list_condition("job", params=params)
elif category == "agent_job":
Expand Down
36 changes: 36 additions & 0 deletions apps/node_man/tests/test_handlers/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,39 @@ def test_job_filter_condition_with_reverse_query(self, *args, **kwargs):
sorted(created_by["name"] for created_by in created_by_info["children"]),
["test2", "test3", "test4", "test5"],
)

@patch("apps.node_man.handlers.cmdb.CmdbHandler.cmdb_or_cache_biz", cmdb_or_cache_biz)
@patch("apps.node_man.handlers.cmdb.client_v2", MockClient)
def test_fetch_host_list_condition_no_permission(self, *args, **kwargs):
self.maxDiff = None
number = 100
host_to_create, _, _ = create_host(number)
process_to_create = []
for host in host_to_create:
process_to_create.append(
ProcessStatus(
bk_host_id=host.bk_host_id,
proc_type=const.ProcType.PLUGIN,
version=f"{random.randint(11, 20)}",
name=settings.HEAD_PLUGINS[random.randint(0, len(settings.HEAD_PLUGINS) - 1)],
status="RUNNING",
)
)

# 验证不传业务ID的情况;即返回用户所有权限的筛选项key
result = MetaHandler().filter_condition("host", params={"bk_biz_ids": []})
self.assertEqual(len(result), 10)

# 验证传入没有的业务ID的情况
result = MetaHandler().filter_condition("host", params={"bk_biz_ids": [2, 4, 5]})
self.assertEqual(len(result), 3)
self.assertEqual(result[0], {"name": "IP", "id": "ip"})
self.assertEqual(result[1], {"name": "管控区域ID:IP", "id": "bk_cloud_ip"})
self.assertEqual(result[2], {"name": "主机名称", "id": "bk_host_name"})

# 验证传入部分业务ID的情况
result = MetaHandler().filter_condition("host", params={"bk_biz_ids": [27, 30, 31, 35]})
# 验证管控区域的数量
self.assertEqual(len(result[3].get("children")), 2)
# 验证agent版本数量
self.assertLessEqual(len(result[6].get("children")), 100)

0 comments on commit 7df5e1b

Please sign in to comment.