Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): 域名通过alias name渲染 #7938 #7941

Open
wants to merge 2 commits into
base: v1.5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions dbm-ui/backend/db_meta/api/db_module/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@


@transaction.atomic
def create(bk_biz_id: int, name: str, cluster_type: str, creator: str = ""):
def create(bk_biz_id: int, name: str, cluster_type: str, alias: str = "", creator: str = ""):
"""创建DB模块
说明:这里的模块与cc无任何关系,仅用于关联配置文件,相当于场景化配置模板,比如gamedb,logdb等
"""
bk_biz_id = request_validator.validated_integer(bk_biz_id, min_value=0)
name = request_validator.validated_str(name)
alias_name = alias or name
cluster_type = request_validator.validated_str(cluster_type)

try:
db_module = DBModule.objects.create(
bk_biz_id=bk_biz_id,
cluster_type=cluster_type,
db_module_name=name,
bk_biz_id=bk_biz_id, cluster_type=cluster_type, db_module_name=name, alias_name=alias_name
)
except IntegrityError:
raise DbModuleExistException(db_module_name=name)
Expand Down
1 change: 1 addition & 0 deletions dbm-ui/backend/db_services/cmdb/biz.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def list_modules_by_biz(bk_biz_id: int, cluster_type: str) -> List[Dict]:
"bk_biz_id": module.bk_biz_id,
"db_module_id": module.db_module_id,
"name": module.db_module_name,
"alias_name": module.alias_name,
"db_module_info": module_infos[index][1],
}
for index, module in enumerate(modules)
Expand Down
1 change: 1 addition & 0 deletions dbm-ui/backend/db_services/cmdb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ListModulesSLZ(serializers.Serializer):

class CreateModuleSLZ(serializers.Serializer):
db_module_name = serializers.CharField(help_text=_("DB模块名"))
alias_name = serializers.CharField(help_text=_("DB模块别名"), required=False, default="")
cluster_type = serializers.ChoiceField(help_text=_("集群类型"), choices=ClusterType.get_choices())

def validate(self, attrs):
Expand Down
3 changes: 2 additions & 1 deletion dbm-ui/backend/db_services/cmdb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def create_module(self, request, bk_biz_id):
validated_data = self.params_validate(self.get_serializer_class())
cluster_type = validated_data["cluster_type"]
db_module_name = validated_data["db_module_name"]
return Response(db_module.apis.create(bk_biz_id, db_module_name, cluster_type))
alias_name = validated_data["alias_name"]
return Response(db_module.apis.create(bk_biz_id, db_module_name, cluster_type, alias_name))

@common_swagger_auto_schema(
operation_summary=_("设置业务英文缩写"),
Expand Down
127 changes: 127 additions & 0 deletions 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 @@ -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"
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
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
Loading
Loading