Skip to content

Commit

Permalink
feat(backend): 域名选择器接口开发 #8078
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud authored and zhangzhw8 committed Nov 29, 2024
1 parent d39507d commit 96ced77
Show file tree
Hide file tree
Showing 31 changed files with 267 additions and 80 deletions.
129 changes: 128 additions & 1 deletion dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ def list_machines(cls, bk_biz_id: int, query_params: Dict, limit: int, offset: i
resource_list = cls._list_machines(bk_biz_id, query_params, limit, offset)
return resource_list

@classmethod
def list_cluster_entries(cls, bk_biz_id: int, query_params: Dict, limit: int, offset: int) -> ResourceList:
"""查询域名列表"""
resource_list = cls._list_cluster_entries(bk_biz_id, query_params, limit, offset)
return resource_list

@classmethod
@abc.abstractmethod
def _list_machines(
Expand All @@ -323,6 +329,20 @@ def _list_machines(
"""查询机器列表. 具体方法在子类中实现"""
raise NotImplementedError

@classmethod
@abc.abstractmethod
def _list_cluster_entries(
cls,
bk_biz_id: int,
query_params: Dict,
limit: int,
offset: int,
filter_params_map: Dict[str, Q] = None,
**kwargs,
) -> ResourceList:
"""查询域名列表(适用于域名选择器),具体方法在子类中实现"""
raise NotImplementedError

@classmethod
@abc.abstractmethod
def get_topo_graph(cls, bk_biz_id: int, cluster_id: int) -> dict:
Expand Down Expand Up @@ -631,7 +651,7 @@ def _filter_instance_hook(cls, bk_biz_id, query_params, instances, **kwargs):
cloud = ResourceQueryHelper.search_cc_cloud(get_cache=True)
# 获取DB模块的映射信息
db_module_names_map = {
module.db_module_id: module.db_module_name
module.db_module_id: module.alias_name
for module in DBModule.objects.filter(bk_biz_id=bk_biz_id, cluster_type__in=cls.cluster_types)
}
# 将实例的查询结果序列化为实例字典信息
Expand Down Expand Up @@ -901,3 +921,110 @@ def _get_machine_extra_info(cls, machine: Machine) -> dict:
"related_instances": instances,
"related_clusters": related_clusters_map.values(),
}

@classmethod
def _list_cluster_entries(
cls,
bk_biz_id: int,
query_params: Dict,
limit: int,
offset: int,
filter_params_map: Dict[str, Q] = None,
**kwargs,
) -> ResourceList:
"""
查询域名信息
@param bk_biz_id: 业务 ID
@param query_params: 查询条件. 通过 .serializers.ListResourceSLZ 完成数据校验
@param limit: 分页查询, 每页展示的数目
@param offset: 分页查询, 当前页的偏移数
@param filter_params_map: 过滤参数map
"""
query_filters = Q(cluster__bk_biz_id=bk_biz_id, cluster__cluster_type__in=cls.cluster_types)

# 实例筛选
def filter_instance_func(query_params):
"""实例过滤ip:port 以及 ip 两种情况"""
f = build_q_for_instance_filter(query_params) & Q(bk_biz_id=bk_biz_id, cluster_type__in=cls.cluster_types)
inst_q = Q(cluster__storageinstance__in=StorageInstance.objects.filter(f)) | Q(
cluster__proxyinstance__in=ProxyInstance.objects.filter(f)
)
return inst_q

# 定义内置的过滤参数map
filter_params_map = filter_params_map or {}
inner_filter_params_map = {
"cluster_entry_type": Q(cluster_entry_type=query_params.get("cluster_entry_type")),
"role": Q(role=query_params.get("role")),
"domain": (
Q(entry__icontains=query_params.get("domain")) | Q(entry__in=query_params.get("domain", "").split(","))
),
"cluster_name": Q(cluster__name=query_params.get("cluster_name")),
"instance": filter_instance_func(query_params),
}
filter_params_map = {**inner_filter_params_map, **filter_params_map}

# 通过基础过滤参数进行cluster过滤
for param in filter_params_map:
if query_params.get(param):
query_filters &= filter_params_map[param]

entry_queryset = ClusterEntry.objects.filter(query_filters).distinct()
entry_infos = cls._filter_entry_hook(bk_biz_id, entry_queryset, limit, offset, **kwargs)
return entry_infos

@classmethod
def _filter_entry_hook(
cls,
bk_biz_id,
entry_queryset: QuerySet,
limit: int,
offset: int,
**kwargs,
) -> ResourceList:
"""
为查询的域名填充额外信息, 子类可继承此方法实现其他回调
@param bk_biz_id: 业务ID
@param machine_queryset: 过滤机器查询集
@param limit: 分页限制
@param offset: 分页起始
"""

count = entry_queryset.count()
limit = count if limit == -1 else limit
if count == 0:
return ResourceList(count=0, data=[])

# 预取proxy_queryset,storage_queryset,加块查询效率
entry_queryset = entry_queryset.order_by("-create_at")[offset : limit + offset].prefetch_related(
"cluster__storageinstance_set__machine", "cluster__proxyinstance_set__machine"
)

# 将集群的查询结果序列化为集群字典信息
entry_infos: List[Dict[str, Any]] = []
for entry in entry_queryset:
entry_infos.append(cls._to_entry_representation(entry, **kwargs))

return ResourceList(count=count, data=entry_infos)

@classmethod
def _to_entry_representation(
cls,
entry: ClusterEntry,
**kwargs,
) -> Dict[str, Any]:
"""
将域名对象转为可序列化的 dict 结构
@param entry: model ClusterEntry 对象, 增加了 storages 和 proxies 属性
"""
storage_list = [inst.ip_port for inst in entry.cluster.storageinstance_set.all()]
proxy_list = [inst.ip_port for inst in entry.cluster.proxyinstance_set.all()]
entry_info = {
"domain": entry.entry,
"role": entry.role,
"cluster_entry_type": entry.cluster_entry_type,
"cluster_name": entry.cluster.name,
"cluster_status": entry.cluster.status,
"instances": storage_list + proxy_list,
}
return entry_info
18 changes: 17 additions & 1 deletion dbm-ui/backend/db_services/dbbase/resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from backend.db_meta.enums import ClusterStatus, ClusterType, InstanceStatus, MachineType, TenDBClusterSpiderRole
from backend.db_meta.enums import (
ClusterEntryRole,
ClusterEntryType,
ClusterStatus,
ClusterType,
InstanceStatus,
MachineType,
TenDBClusterSpiderRole,
)
from backend.db_meta.models.cluster import Cluster
from backend.db_services.dbbase.constants import IP_PORT_DIVIDER
from backend.flow.consts import SqlserverSyncMode
Expand Down Expand Up @@ -155,3 +163,11 @@ class ListTendbClusterMachineResourceSLZ(ListMachineSLZ):
spider_role = serializers.ChoiceField(
help_text=_("spider角色"), choices=TenDBClusterSpiderRole.get_choices(), required=False
)


class ListClusterEntriesSLZ(serializers.Serializer):
instance = serializers.CharField(required=False)
domain = serializers.CharField(required=False)
role = serializers.ChoiceField(required=False, choices=ClusterEntryRole.get_choices())
cluster_entry_type = serializers.ChoiceField(required=False, choices=ClusterEntryType.get_choices())
cluster_name = serializers.CharField(required=False)
9 changes: 9 additions & 0 deletions dbm-ui/backend/db_services/dbbase/resources/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class ResourceViewSet(SystemViewSet):
retrieve_instances_slz = serializers.RetrieveInstancesSerializer
# 机器列表serializer
list_machine_slz = serializers.ListMachineSLZ
# 域名列表serializer
list_entry_slz = serializers.ListClusterEntriesSLZ
# 分页
pagination_class = ResourceLimitOffsetPagination

Expand Down Expand Up @@ -103,6 +105,13 @@ def retrieve_instance(self, request, bk_biz_id: int):
self.query_class.retrieve_instance(bk_biz_id, query_params.get("cluster_id"), query_params["instance"])
)

@action(methods=["GET"], detail=False, url_path="list_cluster_entries")
def list_cluster_entries(self, request, bk_biz_id: int):
"""查询机器列表"""
query_params = self.params_validate(self.list_entry_slz)
data = self.paginator.paginate_list(request, bk_biz_id, self.query_class.list_cluster_entries, query_params)
return self.get_paginated_response(data)

@action(methods=["GET"], detail=False, url_path="list_machines")
def list_machines(self, request, bk_biz_id: int):
"""查询机器列表"""
Expand Down
12 changes: 12 additions & 0 deletions dbm-ui/backend/db_services/dbbase/resources/yasg_slz.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ class Meta:
}
]
}


class PaginatedMachineResourceSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": paginated_machine_resource_example}
ref_name = "PaginatedMachineResourceSLZ"


class PaginatedEntryResourceSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": paginated_machine_resource_example}
ref_name = "PaginatedEntryResourceSLZ"
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/dbbase/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def query_biz_cluster_attrs(self, request, *args, **kwargs):
if "db_module_id" in cluster_attrs:
db_modules = DBModule.objects.filter(bk_biz_id=data["bk_biz_id"], cluster_type__in=data["cluster_type"])
if db_modules:
db_module_names_map = {module.db_module_id: module.db_module_name for module in db_modules}
db_module_names_map = {module.db_module_id: module.alias_name for module in db_modules}
cluster_attrs["db_module_id"] = [
{"value": module, "text": db_module_names_map.get(module, "--")}
for module in existing_values["db_module_id"]
Expand Down
16 changes: 14 additions & 2 deletions dbm-ui/backend/db_services/mongodb/resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
from backend.db_services.dbbase.resources.constants import ResourceNodeType
from backend.db_services.dbbase.resources.serializers import SearchResourceTreeSLZ
from backend.db_services.dbbase.resources.viewsets import ResourceViewSet
from backend.db_services.dbbase.resources.yasg_slz import ResourceTreeSLZ
from backend.db_services.dbbase.resources.yasg_slz import (
PaginatedEntryResourceSLZ,
PaginatedMachineResourceSLZ,
ResourceTreeSLZ,
)
from backend.db_services.mongodb.resources import constants, yasg_slz
from backend.db_services.mongodb.resources.query import MongoDBListRetrieveResource
from backend.iam_app.dataclass.actions import ActionEnum
Expand Down Expand Up @@ -72,7 +76,15 @@
name="list_machines",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListMachineSLZ(),
responses={status.HTTP_200_OK: yasg_slz.PaginatedMachineResourceSLZ()},
responses={status.HTTP_200_OK: PaginatedMachineResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
@method_decorator(
name="list_cluster_entries",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListClusterEntriesSLZ(),
responses={status.HTTP_200_OK: PaginatedEntryResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
Expand Down
8 changes: 0 additions & 8 deletions dbm-ui/backend/db_services/mongodb/resources/yasg_slz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"""
from rest_framework import serializers

from backend.db_services.dbbase.resources.yasg_slz import paginated_machine_resource_example

REF_NAME = "mongodb"

paginated_resource_example = {
Expand Down Expand Up @@ -142,12 +140,6 @@ class Meta:
ref_name = f"{REF_NAME}_ResourceInstanceSLZ"


class PaginatedMachineResourceSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": paginated_machine_resource_example}
ref_name = f"{REF_NAME}_PaginatedMachineResourceSLZ"


class ResourceTopoGraphSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": resource_topo_graph_example}
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/mysql/cluster/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _fill_spider_instance_info(_cluster: Cluster, _cluster_info: Dict):
)
cluster_db_module_ids: List[int] = [cluster.db_module_id for cluster in clusters]
db_module_names: Dict[int, str] = {
module.db_module_id: module.db_module_name
module.db_module_id: module.alias_name
for module in DBModule.objects.filter(db_module_id__in=cluster_db_module_ids)
}

Expand Down
11 changes: 10 additions & 1 deletion dbm-ui/backend/db_services/mysql/resources/tendbcluster/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from backend.bk_web.swagger import common_swagger_auto_schema
from backend.configuration.constants import DBType
from backend.db_services.dbbase.resources import serializers, viewsets
from backend.db_services.dbbase.resources.yasg_slz import PaginatedEntryResourceSLZ, PaginatedMachineResourceSLZ
from backend.db_services.mysql.resources import constants
from backend.db_services.mysql.resources.tendbcluster import yasg_slz
from backend.db_services.mysql.resources.tendbcluster.query import ListRetrieveResource
Expand Down Expand Up @@ -58,7 +59,15 @@
name="list_machines",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListTendbClusterMachineResourceSLZ(),
responses={status.HTTP_200_OK: yasg_slz.PaginatedMachineResourceSLZ()},
responses={status.HTTP_200_OK: PaginatedMachineResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
@method_decorator(
name="list_cluster_entries",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListClusterEntriesSLZ(),
responses={status.HTTP_200_OK: PaginatedEntryResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"""
from rest_framework import serializers

from backend.db_services.dbbase.resources.yasg_slz import paginated_machine_resource_example

from .query import ListRetrieveResource

REF_NAME = "dbha"
Expand Down Expand Up @@ -279,12 +277,6 @@ class Meta:
ref_name = f"{REF_NAME}_ResourceInstanceSLZ"


class PaginatedMachineResourceSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": paginated_machine_resource_example}
ref_name = f"{REF_NAME}_PaginatedMachineResourceSLZ"


class ResourceTopoGraphSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": resource_topo_graph_example}
Expand Down
11 changes: 10 additions & 1 deletion dbm-ui/backend/db_services/mysql/resources/tendbha/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from backend.bk_web.swagger import common_swagger_auto_schema
from backend.configuration.constants import DBType
from backend.db_services.dbbase.resources import serializers, viewsets
from backend.db_services.dbbase.resources.yasg_slz import PaginatedEntryResourceSLZ, PaginatedMachineResourceSLZ
from backend.db_services.mysql.resources import constants
from backend.db_services.mysql.resources.tendbha import yasg_slz
from backend.db_services.mysql.resources.tendbha.query import ListRetrieveResource
Expand Down Expand Up @@ -55,7 +56,15 @@
name="list_machines",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListMachineSLZ(),
responses={status.HTTP_200_OK: yasg_slz.PaginatedMachineResourceSLZ()},
responses={status.HTTP_200_OK: PaginatedMachineResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
@method_decorator(
name="list_cluster_entries",
decorator=common_swagger_auto_schema(
query_serializer=serializers.ListClusterEntriesSLZ(),
responses={status.HTTP_200_OK: PaginatedEntryResourceSLZ()},
tags=[constants.RESOURCE_TAG],
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"""
from rest_framework import serializers

from backend.db_services.dbbase.resources.yasg_slz import paginated_machine_resource_example

from .query import ListRetrieveResource

REF_NAME = "dbha"
Expand Down Expand Up @@ -107,9 +105,3 @@ class ResourceTopoGraphSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": resource_topo_graph_example}
ref_name = f"{REF_NAME}_ResourceTopoGraphSLZ"


class PaginatedMachineResourceSLZ(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": paginated_machine_resource_example}
ref_name = f"{REF_NAME}_PaginatedMachineResourceSLZ"
Loading

0 comments on commit 96ced77

Please sign in to comment.