diff --git a/apps/node_man/handlers/meta.py b/apps/node_man/handlers/meta.py index 39b5ea16b..e64ef6599 100644 --- a/apps/node_man/handlers/meta.py +++ b/apps/node_man/handlers/meta.py @@ -121,10 +121,10 @@ 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接口所有条件 """ @@ -132,6 +132,19 @@ def fetch_host_condition(self): 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() @@ -378,27 +391,43 @@ def fetch_plugin_list_condition(self): return self.filter_empty_children(ret_value) - @staticmethod - def fetch_plugin_host_condition(): + def fetch_plugin_host_condition(self, params): + biz_permission = self.plugin_biz_permission(params) + + 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() + col_map = [bk_cloud_ids] + col_list = ["bk_cloud_id"] + col_data = self.fetch_host_process_unique_col( + biz_permission, col_list, [constants.NodeType.AGENT, constants.NodeType.PAGENT] + ) + for sublist in col_data: + for index, item in enumerate(sublist): + col_map[index].add(item) + bk_cloud_names = CloudHandler().list_cloud_info(bk_cloud_ids) + bk_cloud_ids_children = [ + {"name": bk_cloud_names.get(bk_cloud_id, {}).get("bk_cloud_name", bk_cloud_id), "id": bk_cloud_id} + for bk_cloud_id in bk_cloud_ids + ] ret_value = [ {"name": "IP", "id": "ip"}, {"name": _("管控区域ID:IP"), "id": "bk_cloud_ip"}, {"name": _("主机名称"), "id": "bk_host_name"}, { - "id": "node_type", "name": _("节点类型"), + "id": "node_type", "children": [ {"id": node_type, "name": constants.NODE_TYPE_ALIAS_MAP.get(node_type)} for node_type in constants.NODE_TUPLE ], }, + {"name": _("管控区域"), "id": "bk_cloud_id", "children": bk_cloud_ids_children}, ] - - clouds = dict(models.Cloud.objects.values_list("bk_cloud_id", "bk_cloud_name")) - cloud_children = [{"id": cloud_id, "name": cloud_name} for cloud_id, cloud_name in clouds.items()] - cloud_children.insert(0, {"id": constants.DEFAULT_CLOUD, "name": _("直连区域")}) - ret_value.append({"name": _("管控区域"), "id": "bk_cloud_id", "children": cloud_children}) - os_dict = {"name": _("操作系统"), "id": "os_type", "children": []} for os_type in constants.OS_TUPLE: if os_type == constants.OsType.SOLARIS and settings.BKAPP_RUN_ENV == constants.BkappRunEnvType.CE.value: @@ -450,23 +479,47 @@ def fetch_plugin_host_condition(): return ret_value - @staticmethod - def fetch_plugin_version_condition(): + def fetch_plugin_version_condition(self, params): + biz_permission = self.plugin_biz_permission(params) + + if not biz_permission: + return [{"name": name, "id": name} for name in settings.HEAD_PLUGINS] + + plugin_versions = [] + col_list = ["version"] plugin_names = tools.PluginV2Tools.fetch_head_plugins() - versions = ( + for name in plugin_names: + col_data = self.fetch_host_process_unique_col( + biz_permission, + col_list, + [constants.NodeType.AGENT, constants.NodeType.PAGENT, constants.NodeType.PROXY], + name=name, + proc_type="PLUGIN", + ) + + for sublist in col_data: + # 过滤掉版本号为""的插件 + if not sublist[0] == "": + plugin_versions.append({"name": name, "version": sublist[0]}) + + agent_versions = ( models.ProcessStatus.objects.filter( - source_type=models.ProcessStatus.SourceType.DEFAULT, - name__in=plugin_names + [models.ProcessStatus.GSE_AGENT_PROCESS_NAME], + source_type=models.ProcessStatus.SourceType.DEFAULT, name=models.ProcessStatus.GSE_AGENT_PROCESS_NAME ) .values("name", "version") .exclude(version="") .distinct() ) + plugin_version = {} - for version in versions: + for version in plugin_versions: plugin_version.setdefault(version["name"], []).append( {"id": version["version"], "name": version["version"]} ) + for agent_version in agent_versions: + plugin_version.setdefault(agent_version["name"], []).append( + {"id": agent_version["version"], "name": agent_version["version"]} + ) plugin_result = [ { @@ -520,7 +573,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": @@ -530,10 +583,10 @@ def filter_condition(self, category, params=None): elif category == "plugin_job": return self.fetch_job_list_condition("plugin_job") elif category == "plugin_version": - ret = self.fetch_plugin_version_condition() + ret = self.fetch_plugin_version_condition(params=params) return ret elif category == "plugin_host": - ret = self.fetch_plugin_host_condition() + ret = self.fetch_plugin_host_condition(params=params) return ret elif category == "os_type": ret = self.fetch_os_type_children() @@ -624,3 +677,14 @@ def job_setting(self, params): user_setting[setting] = params[setting] global_setting.v_json = user_setting global_setting.save() + + @staticmethod + def plugin_biz_permission(params): + biz_id_name = CmdbHandler().biz_id_name({"action": constants.IamActionType.plugin_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())) + return biz_permission diff --git a/apps/node_man/tests/test_handlers/test_meta.py b/apps/node_man/tests/test_handlers/test_meta.py index 0ba5fd18a..52c020e9e 100644 --- a/apps/node_man/tests/test_handlers/test_meta.py +++ b/apps/node_man/tests/test_handlers/test_meta.py @@ -187,7 +187,6 @@ def test_fetch_plugin_list_condition(self): @patch("apps.node_man.handlers.cmdb.CmdbHandler.cmdb_or_cache_biz", cmdb_or_cache_biz) @patch("apps.node_man.handlers.cmdb.client_v2", MockClient) - @patch("apps.node_man.handlers.cmdb.get_request_username", return_value="special_test") def test_fetch_plugin_list_condition_no_permission(self, *args, **kwargs): self.maxDiff = None # 插件表头接口 @@ -207,15 +206,12 @@ def test_fetch_plugin_list_condition_no_permission(self, *args, **kwargs): ) ) - default_cloud_num = 1 - generated_cloud_area_num = 100 - total_cloud_num = default_cloud_num + generated_cloud_area_num - - result = MetaHandler().filter_condition("plugin_host") + # 验证不传业务ID的情况;即返回用户所有权限的筛选项key + result = MetaHandler().filter_condition("plugin_host", params={"bk_biz_ids": []}) self.assertEqual(result[0], {"name": "IP", "id": "ip"}) self.assertEqual(result[1], {"name": "管控区域ID:IP", "id": "bk_cloud_ip"}) - self.assertEqual(len(result[4]["children"]), total_cloud_num) + # self.assertEqual(len(result[4]["children"]), total_cloud_num) self.assertEqual( result[5], { @@ -260,6 +256,15 @@ def test_fetch_plugin_list_condition_no_permission(self, *args, **kwargs): }, ) + # 验证传入部分业务ID的情况 + result = MetaHandler().filter_condition("plugin_host", params={"bk_biz_ids": [27]}) + # 验证管控区域的数量 + self.assertLessEqual(len(result[4]["children"]), 2) + + # 验证传入没有的业务ID的情况 + result = MetaHandler().filter_condition("plugin_host", params={"bk_biz_ids": [1, 2, 3]}) + self.assertEqual(len(result), 3) + @patch("apps.node_man.handlers.cmdb.client_v2", MockClient) def test_job_setting(self): # 相关参数保存接口 @@ -273,6 +278,8 @@ def test_fetch_plugin_condition(self): result = MetaHandler().fetch_plugin_list_condition() self.assertEqual(len(result), 11) + @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_plugin_version_condition(self): host_to_create, _, _ = create_host(10) process_to_create = [] @@ -286,10 +293,17 @@ def test_fetch_plugin_version_condition(self): status="RUNNING", ) ) - result = MetaHandler().fetch_plugin_version_condition() - + ProcessStatus.objects.bulk_create(process_to_create) + # 验证不传业务ID的情况;即返回用户所有权限的业务ID + result = MetaHandler().fetch_plugin_version_condition(params={"bk_biz_ids": []}) self.assertEqual(len(result), 11) self.assertEqual(len(result[0]["children"]), 10) + # 验证传入部分业务ID的情况 + result = MetaHandler().fetch_plugin_version_condition(params={"bk_biz_ids": [27, 30]}) + self.assertLessEqual(len(result), 11) + # 验证传入没有的业务ID的情况 + result = MetaHandler().fetch_plugin_version_condition(params={"bk_biz_ids": [1, 2, 3]}) + self.assertLessEqual(len(result), 6) @override_settings(BKAPP_DEFAULT_SSH_PORT=22) def test_global_settings__install_default_values(self): @@ -482,3 +496,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)