From 4b73d2072f7864016f0628603bd882c49c0a4c70 Mon Sep 17 00:00:00 2001 From: durant <826035498@qq.com> Date: Thu, 31 Oct 2024 20:46:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20=E6=8F=90=E4=BE=9B=20dns=20?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=20api=20#7695?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/legacy/__init__.py | 12 ++++++++ dbm-ui/backend/legacy/serializers.py | 46 ++++++++++++++++++++++++++++ dbm-ui/backend/legacy/urls.py | 18 +++++++++++ dbm-ui/backend/legacy/views.py | 40 ++++++++++++++++++++++++ dbm-ui/backend/urls.py | 1 + 5 files changed, 117 insertions(+) create mode 100644 dbm-ui/backend/legacy/__init__.py create mode 100644 dbm-ui/backend/legacy/serializers.py create mode 100644 dbm-ui/backend/legacy/urls.py create mode 100644 dbm-ui/backend/legacy/views.py diff --git a/dbm-ui/backend/legacy/__init__.py b/dbm-ui/backend/legacy/__init__.py new file mode 100644 index 0000000000..2cc06edde0 --- /dev/null +++ b/dbm-ui/backend/legacy/__init__.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. +Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" + +"""用于提供历史遗留的接口/功能,仅供指定环境使用,收敛后逐步下架接口""" diff --git a/dbm-ui/backend/legacy/serializers.py b/dbm-ui/backend/legacy/serializers.py new file mode 100644 index 0000000000..5b8fdd948c --- /dev/null +++ b/dbm-ui/backend/legacy/serializers.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. +Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" + +from django.utils.translation import ugettext as _ +from rest_framework import serializers + +from backend.db_meta.models import Cluster +from backend.exceptions import ValidationError + + +class CreateDNSSerializer(serializers.Serializer): + class CreateDomainSerializer(serializers.Serializer): + domain_name = serializers.CharField(help_text=_("域名")) + instances = serializers.ListField(help_text=_("实例列表"), child=serializers.CharField(), allow_empty=False) + manager = serializers.CharField(help_text=_("管理者"), required=False, allow_blank=True) + remark = serializers.CharField(help_text=_("域名备注信息"), required=False, allow_blank=True) + domain_type = serializers.CharField(help_text=_("域名类型"), required=False, allow_blank=True) + extends = serializers.CharField(help_text=_("域名自定义扩展字段"), required=False, allow_blank=True) + + app = serializers.CharField(help_text=_("GCS业务英文缩写")) + bk_cloud_id = serializers.IntegerField(help_text=_("云区域 ID")) + domains = serializers.ListField(help_text=_("域名列表"), child=CreateDomainSerializer(), allow_empty=False) + + def validate(self, attrs): + domains = attrs.get("domains", []) + domains = [domain["domain_name"] for domain in domains] + if Cluster.objects.filter(immute_domain__in=domains).exists(): + raise ValidationError(_("域名存在于 DBM 中,不允许通过此接口修改,请联系管理员")) + return attrs + + +class DeleteDNSSerializer(CreateDNSSerializer): + class DeleteDomainSerializer(CreateDNSSerializer.CreateDomainSerializer): + instances = serializers.ListField( + help_text=_("实例列表"), child=serializers.IntegerField(), allow_empty=True, required=False, default=[] + ) + + domains = serializers.ListField(help_text=_("域名列表"), child=DeleteDomainSerializer(), allow_empty=False) diff --git a/dbm-ui/backend/legacy/urls.py b/dbm-ui/backend/legacy/urls.py new file mode 100644 index 0000000000..05790454f1 --- /dev/null +++ b/dbm-ui/backend/legacy/urls.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. +Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" +from rest_framework.routers import DefaultRouter + +from .views import DnsViewSet + +routers = DefaultRouter(trailing_slash=True) +routers.register(r"dns", DnsViewSet, basename="dns") + +urlpatterns = routers.urls diff --git a/dbm-ui/backend/legacy/views.py b/dbm-ui/backend/legacy/views.py new file mode 100644 index 0000000000..e5e01735cc --- /dev/null +++ b/dbm-ui/backend/legacy/views.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. +Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at https://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" + +from django.utils.translation import ugettext_lazy as _ +from rest_framework.decorators import action +from rest_framework.response import Response + +from backend.bk_web import viewsets +from backend.bk_web.swagger import common_swagger_auto_schema +from backend.components import DnsApi +from backend.iam_app.dataclass.actions import ActionEnum +from backend.iam_app.handlers.drf_perm.base import ResourceActionPermission +from backend.legacy.serializers import CreateDNSSerializer, DeleteDNSSerializer + +SWAGGER_TAG = ["legacy"] + + +class DnsViewSet(viewsets.SystemViewSet): + def get_default_permission_class(self) -> list: + return [ResourceActionPermission([ActionEnum.GLOBAL_MANAGE])] + + @common_swagger_auto_schema(operation_summary=_("创建 DNS"), tags=SWAGGER_TAG, request_body=CreateDNSSerializer()) + @action(methods=["PUT"], detail=False, serializer_class=CreateDNSSerializer) + def create_domain(self, request, *args, **kwargs): + data = self.params_validate(self.get_serializer_class()) + return Response(DnsApi.create_domain(data)) + + @common_swagger_auto_schema(operation_summary=_("删除 DNS"), tags=SWAGGER_TAG, request_body=CreateDNSSerializer()) + @action(methods=["DELETE"], detail=False, serializer_class=DeleteDNSSerializer) + def delete_domain(self, request, *args, **kwargs): + data = self.params_validate(self.get_serializer_class()) + return Response(DnsApi.delete_domain(data)) diff --git a/dbm-ui/backend/urls.py b/dbm-ui/backend/urls.py index 0ebaa392c9..51f2d65164 100644 --- a/dbm-ui/backend/urls.py +++ b/dbm-ui/backend/urls.py @@ -62,6 +62,7 @@ path("dbbase/", include("backend.db_services.dbbase.urls")), path("quick_search/", include("backend.db_services.quick_search.urls")), path("plugin/", include("backend.db_services.plugin.urls")), + path("legacy/", include("backend.legacy.urls")), ] urlpatterns = [