From 839da616047590cc2560ebf71716bdb81bda6dfb Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Thu, 11 Apr 2024 17:14:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E6=94=AF=E6=8C=81=E8=B7=A8?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E6=B1=A1=E7=82=B9=E6=B1=A0=E8=BD=AC=E7=A7=BB?= =?UTF-8?q?=20&=20=E4=BF=AE=E5=A4=8Des=E4=B8=BB=E6=9C=BA=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#3959?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/db_dirty/handlers.py | 15 ++++++------- dbm-ui/backend/db_dirty/serializers.py | 6 +++++- dbm-ui/backend/db_dirty/views.py | 21 +++++++++++++++++-- .../backend/db_services/bigdata/es/query.py | 3 +-- .../db_services/bigdata/influxdb/query.py | 3 +-- .../db_services/bigdata/resources/query.py | 5 +---- .../ipchooser/handlers/topo_handler.py | 4 ++-- .../db_services/ipchooser/query/resource.py | 19 ++++++++++------- .../mysql/permission/authorize/handlers.py | 4 +++- .../redis/resources/redis_cluster/query.py | 3 +-- dbm-ui/backend/ticket/views.py | 2 +- 11 files changed, 52 insertions(+), 33 deletions(-) diff --git a/dbm-ui/backend/db_dirty/handlers.py b/dbm-ui/backend/db_dirty/handlers.py index 0f90bdcb30..ea2867cf27 100644 --- a/dbm-ui/backend/db_dirty/handlers.py +++ b/dbm-ui/backend/db_dirty/handlers.py @@ -69,9 +69,8 @@ def insert_dirty_machines(cls, bk_biz_id: int, bk_host_ids: List[Dict[str, Any]] "condition": "AND", "rules": [{"field": "bk_host_id", "operator": "in", "value": bk_host_ids}], } - dirty_host_infos = CCApi.list_biz_hosts( + dirty_host_infos = CCApi.list_hosts_without_biz( { - "bk_biz_id": bk_biz_id, # 默认一次性录入的机器不会超过500 "page": {"start": 0, "limit": 500, "sort": "bk_host_id"}, "host_property_filter": host_property_filter, @@ -80,15 +79,13 @@ def insert_dirty_machines(cls, bk_biz_id: int, bk_host_ids: List[Dict[str, Any]] use_admin=True, )["info"] - # 获取空闲机模块,资源池模块和污点池模块 - system_manage_topo = SystemSettings.get_setting_value(key=SystemSettingsEnum.MANAGE_TOPO.value) + # 获取业务空闲机模块,资源池模块和污点池模块 idle_module = CcManage(bk_biz_id, "").get_biz_internal_module(bk_biz_id)[IDLE_HOST_MODULE]["bk_module_id"] + system_manage_topo = SystemSettings.get_setting_value(key=SystemSettingsEnum.MANAGE_TOPO.value) resource_module, dirty_module = system_manage_topo["resource_module_id"], system_manage_topo["dirty_module_id"] - # 获取主机的拓扑信息 - host_topo_infos = TopoHandler.query_host_set_module(bk_biz_id=bk_biz_id, bk_host_ids=bk_host_ids)[ - "hosts_topo_info" - ] - # 将污点机器信息转移至污点池模(如果污点机器不在空闲机/资源池,则放弃转移,认为已到正确拓扑) + # 获取主机的拓扑信息(注:这里不能带上业务信息,因为主机可能转移业务) + host_topo_infos = TopoHandler.query_host_set_module(bk_host_ids=bk_host_ids)["hosts_topo_info"] + # 将污点机器信息转移至DBA污点池模(如果污点机器不在空闲机/资源池,则放弃转移,认为已到正确拓扑) transfer_host_ids = [ info["bk_host_id"] for info in host_topo_infos diff --git a/dbm-ui/backend/db_dirty/serializers.py b/dbm-ui/backend/db_dirty/serializers.py index 6d9bcab601..2b9caccd27 100644 --- a/dbm-ui/backend/db_dirty/serializers.py +++ b/dbm-ui/backend/db_dirty/serializers.py @@ -38,5 +38,9 @@ class Meta: swagger_schema_fields = {"example": DIRTY_MACHINE_LIST} -class DeleteDirtyMachineSerializer(serializers.Serializer): +class TransferDirtyMachineSerializer(serializers.Serializer): bk_host_ids = serializers.ListField(child=serializers.IntegerField(), help_text=_("待转移的主机ID列表")) + + +class DeleteDirtyMachineSerializer(serializers.Serializer): + bk_host_ids = serializers.ListField(child=serializers.IntegerField(), help_text=_("待删除的污点池记录主机ID")) diff --git a/dbm-ui/backend/db_dirty/views.py b/dbm-ui/backend/db_dirty/views.py index 7ed7eb42da..46034adf5b 100644 --- a/dbm-ui/backend/db_dirty/views.py +++ b/dbm-ui/backend/db_dirty/views.py @@ -26,6 +26,7 @@ DeleteDirtyMachineSerializer, QueryDirtyMachineResponseSerializer, QueryDirtyMachineSerializer, + TransferDirtyMachineSerializer, ) from backend.db_meta.models import AppCache from backend.db_services.ipchooser.query.resource import ResourceQueryHelper @@ -85,16 +86,32 @@ def query_operation_list(self, request): @common_swagger_auto_schema( operation_summary=_("将污点池主机转移至待回收模块"), - request_body=DeleteDirtyMachineSerializer(), + request_body=TransferDirtyMachineSerializer(), tags=[SWAGGER_TAG], ) @action( detail=False, methods=["POST"], url_path="transfer_dirty_machines", - serializer_class=DeleteDirtyMachineSerializer, + serializer_class=TransferDirtyMachineSerializer, ) def transfer_dirty_machines(self, request): bk_host_ids = self.params_validate(self.get_serializer_class())["bk_host_ids"] DBDirtyMachineHandler.transfer_dirty_machines(bk_host_ids) return Response() + + @common_swagger_auto_schema( + operation_summary=_("删除污点池记录"), + request_body=DeleteDirtyMachineSerializer(), + tags=[SWAGGER_TAG], + ) + @action( + detail=False, + methods=["DELETE"], + url_path="delete_dirty_records", + serializer_class=DeleteDirtyMachineSerializer, + ) + def delete_dirty_records(self, request): + bk_host_ids = self.params_validate(self.get_serializer_class())["bk_host_ids"] + DBDirtyMachineHandler.remove_dirty_machines(bk_host_ids) + return Response() diff --git a/dbm-ui/backend/db_services/bigdata/es/query.py b/dbm-ui/backend/db_services/bigdata/es/query.py index f29199832d..b3c4a462f0 100644 --- a/dbm-ui/backend/db_services/bigdata/es/query.py +++ b/dbm-ui/backend/db_services/bigdata/es/query.py @@ -11,7 +11,6 @@ from django.utils.translation import ugettext_lazy as _ -from backend import env from backend.db_meta.api.cluster.es.detail import scan_cluster from backend.db_meta.enums import InstanceRole from backend.db_meta.enums.cluster_type import ClusterType @@ -45,7 +44,7 @@ def get_nodes(cls, bk_biz_id: int, cluster_id: int, role: str, keyword: str = No machines = Machine.objects.filter(bk_host_id__in=storage_instances.values_list("machine", flat=True)) role_host_ids = list(machines.values_list("bk_host_id", flat=True)) - return ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword) + return ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword) @classmethod def get_topo_graph(cls, bk_biz_id: int, cluster_id: int) -> dict: diff --git a/dbm-ui/backend/db_services/bigdata/influxdb/query.py b/dbm-ui/backend/db_services/bigdata/influxdb/query.py index b93baa843c..be07f4d850 100644 --- a/dbm-ui/backend/db_services/bigdata/influxdb/query.py +++ b/dbm-ui/backend/db_services/bigdata/influxdb/query.py @@ -13,7 +13,6 @@ from django.db.models import F, Q, Value from django.utils.translation import ugettext_lazy as _ -from backend import env from backend.db_meta.enums import ClusterType, InstanceRole from backend.db_meta.models import Machine from backend.db_meta.models.group import Group, GroupInstance @@ -101,7 +100,7 @@ def list_instances(cls, bk_biz_id: int, query_params: Dict, limit: int, offset: bk_host_ids = [instance["machine__bk_host_id"] for instance in instances] # TODO: 页面上所有涉及到补充主机信息的地方需要调整bk_biz_id为dba业务(因为主机最终别转移到了dba业务) - host_infos = ResourceQueryHelper.search_cc_hosts(bk_biz_id=env.DBA_APP_BK_BIZ_ID, role_host_ids=bk_host_ids) + host_infos = ResourceQueryHelper.search_cc_hosts(role_host_ids=bk_host_ids) host_id__host_map = {host["bk_host_id"]: host for host in host_infos} # fill restart info diff --git a/dbm-ui/backend/db_services/bigdata/resources/query.py b/dbm-ui/backend/db_services/bigdata/resources/query.py index 1103b21768..eb44147a80 100644 --- a/dbm-ui/backend/db_services/bigdata/resources/query.py +++ b/dbm-ui/backend/db_services/bigdata/resources/query.py @@ -14,7 +14,6 @@ from django.db.models import Count, F from django.utils.translation import ugettext_lazy as _ -from backend import env from backend.db_meta.models.cluster import Cluster from backend.db_meta.models.instance import StorageInstance from backend.db_proxy.models import ClusterExtension @@ -154,9 +153,7 @@ def _to_nodes_list( cloud_info = ResourceQueryHelper.search_cc_cloud(get_cache=True) host_id__node_list_map = {node["machine__bk_host_id"]: node for node in node_list} - host_info_map = ResourceQueryHelper.search_cc_hosts( - env.DBA_APP_BK_BIZ_ID, list(host_id__node_list_map.keys()), keyword=None - ) + host_info_map = ResourceQueryHelper.search_cc_hosts(list(host_id__node_list_map.keys()), keyword=None) host_id__host_info_map = {host_info["bk_host_id"]: host_info for host_info in host_info_map} for host_id, node in host_id__node_list_map.items(): node["bk_host_id"] = node.pop("machine__bk_host_id") diff --git a/dbm-ui/backend/db_services/ipchooser/handlers/topo_handler.py b/dbm-ui/backend/db_services/ipchooser/handlers/topo_handler.py index d0afdeea5f..fb499f4ed6 100644 --- a/dbm-ui/backend/db_services/ipchooser/handlers/topo_handler.py +++ b/dbm-ui/backend/db_services/ipchooser/handlers/topo_handler.py @@ -189,11 +189,11 @@ def query_host_topo_infos( return {"total": len(hosts_topo_info), "hosts_topo_info": hosts_topo_info} @classmethod - def query_host_set_module(cls, bk_biz_id: int, bk_host_ids: typing.List) -> typing.Dict: + def query_host_set_module(cls, bk_host_ids: typing.List) -> typing.Dict: """ 根据过滤条件查询主机的集群ID和模块ID(不支持自定义拓扑主机的查询) """ - host_topos = CCApi.find_host_biz_relations({"bk_biz_id": bk_biz_id, "bk_host_id": bk_host_ids}, use_admin=True) + host_topos = CCApi.find_host_biz_relations({"bk_host_id": bk_host_ids}, use_admin=True) host_id__host_topos = defaultdict(lambda: defaultdict(list)) for host_info in host_topos: host_id__host_topos[host_info["bk_host_id"]]["bk_set_ids"].append(host_info["bk_set_id"]) diff --git a/dbm-ui/backend/db_services/ipchooser/query/resource.py b/dbm-ui/backend/db_services/ipchooser/query/resource.py index c72d7b74b2..d0e9785eca 100644 --- a/dbm-ui/backend/db_services/ipchooser/query/resource.py +++ b/dbm-ui/backend/db_services/ipchooser/query/resource.py @@ -394,8 +394,8 @@ def _parse_host_filter(_host: str) -> Dict: @staticmethod def search_cc_hosts( - bk_biz_id: int, role_host_ids: List[int], + bk_biz_id: int = None, keyword: str = None, set_filter: Union[str, List] = None, module_filter: Union[str, List] = None, @@ -456,22 +456,27 @@ def _split_keyword(_keyword: str) -> List: ], } ) - list_biz_hosts_filter = { - "bk_biz_id": bk_biz_id, + list_hosts_filter = { "fields": constants.CommonEnum.DEFAULT_HOST_FIELDS.value, "page": {"start": 0, "limit": limit, "sort": "bk_host_innerip"}, "host_property_filter": {"condition": "AND", "rules": rules}, } + if bk_biz_id: + list_hosts_filter.update(bk_biz_id=bk_biz_id) + if set_filter_field: - list_biz_hosts_filter.update({set_filter_field: set_filter}) + list_hosts_filter.update({set_filter_field: set_filter}) if module_filter_field: - list_biz_hosts_filter.update({module_filter_field: module_filter}) + list_hosts_filter.update({module_filter_field: module_filter}) # 获取主机信息 - resp = CCApi.list_biz_hosts(list_biz_hosts_filter, use_admin=True) - hosts = resp["info"] + if bk_biz_id: + resp = CCApi.list_biz_hosts(list_hosts_filter, use_admin=True) + else: + resp = CCApi.list_hosts_without_biz(list_hosts_filter, use_admin=True) + hosts = resp["info"] ResourceQueryHelper.fill_agent_status(hosts) ResourceQueryHelper.fill_cloud_name(hosts) diff --git a/dbm-ui/backend/db_services/mysql/permission/authorize/handlers.py b/dbm-ui/backend/db_services/mysql/permission/authorize/handlers.py index 68e1a9023d..6b2b3a0854 100644 --- a/dbm-ui/backend/db_services/mysql/permission/authorize/handlers.py +++ b/dbm-ui/backend/db_services/mysql/permission/authorize/handlers.py @@ -205,7 +205,9 @@ def get_host_in_authorize(self, host_filter_meta: HostInAuthorizeFilterMeta): elif not keyword_list or item["ip"] in keyword_list: ip_whitelist.append({"ip": item["ip"]}) - hosts = ResourceQueryHelper.search_cc_hosts(self.bk_biz_id, bk_host_ids, keyword=keyword) + hosts = ResourceQueryHelper.search_cc_hosts( + bk_biz_id=self.bk_biz_id, role_host_ids=bk_host_ids, keyword=keyword + ) return {"hosts": hosts, "ip_whitelist": ip_whitelist} def authorize_apply( diff --git a/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py b/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py index 773a07fd0f..d0d77faace 100644 --- a/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py +++ b/dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py @@ -14,7 +14,6 @@ from django.forms import model_to_dict from django.utils.translation import ugettext_lazy as _ -from backend import env from backend.db_meta.api.cluster.rediscluster.handler import RedisClusterHandler from backend.db_meta.api.cluster.tendiscache.handler import TendisCacheClusterHandler from backend.db_meta.api.cluster.tendispluscluster.handler import TendisPlusClusterHandler @@ -83,7 +82,7 @@ def get_nodes(cls, bk_biz_id: int, cluster_id: int, role: str, keyword: str = No machines = Machine.objects.filter(ip__in=storage_instances.values_list("machine")) role_host_ids = list(machines.values_list("bk_host_id", flat=True)) - return ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword) + return ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword) @classmethod def _filter_cluster_hook( diff --git a/dbm-ui/backend/ticket/views.py b/dbm-ui/backend/ticket/views.py index e730ca9242..cafe6764c4 100644 --- a/dbm-ui/backend/ticket/views.py +++ b/dbm-ui/backend/ticket/views.py @@ -278,7 +278,7 @@ def get_nodes(self, request, *args, **kwargs): if not role_host_ids: return Response([]) - hosts = ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword) + hosts = ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword) # 补充实例数,默认为1 for host in hosts: