diff --git a/src/dashboard/apigateway/apigateway/apis/open/stage/serializers.py b/src/dashboard/apigateway/apigateway/apis/open/stage/serializers.py index d80801ce9..e678226eb 100644 --- a/src/dashboard/apigateway/apigateway/apis/open/stage/serializers.py +++ b/src/dashboard/apigateway/apigateway/apis/open/stage/serializers.py @@ -33,13 +33,11 @@ from apigateway.common.fields import CurrentGatewayDefault from apigateway.common.mixins.serializers import ExtensibleFieldMixin from apigateway.common.plugin.header_rewrite import HeaderRewriteConvertor -from apigateway.common.release.publish import trigger_gateway_publish from apigateway.core.constants import ( DEFAULT_BACKEND_NAME, DEFAULT_LB_HOST_WEIGHT, STAGE_NAME_PATTERN, LoadBalanceTypeEnum, - PublishSourceEnum, ) from apigateway.core.models import Backend, BackendConfig, MicroGateway, Stage @@ -230,15 +228,13 @@ def _get_stage_backend_config(self, proxy_http_config): scheme, _host = host["host"].rstrip("/").split("://") hosts.append({"scheme": scheme, "host": _host, "weight": host["weight"]}) - config = { + return { "type": "node", "timeout": proxy_http_config["timeout"], "loadbalance": proxy_http_config["upstreams"]["loadbalance"], "hosts": hosts, } - return config - def update(self, instance, validated_data): validated_data.pop("name", None) # 仅能通过发布更新 status,不允许直接更新 status diff --git a/src/dashboard/apigateway/apigateway/apis/web/backend/constants.py b/src/dashboard/apigateway/apigateway/apis/web/backend/constants.py index 7c6a960c7..527f796b5 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/backend/constants.py +++ b/src/dashboard/apigateway/apigateway/apis/web/backend/constants.py @@ -18,34 +18,4 @@ # import re -from blue_krill.data_types.enum import EnumField, StructuredEnum - -from apigateway.core.constants import BackendTypeEnum - BACKEND_NAME_PATTERN = re.compile(r"^[a-zA-Z][a-zA-Z0-9-]{0,19}$") - - -class BackendConfigTypeEnum(StructuredEnum): - NODE = EnumField("node", label="节点") - # NOTE: 当前不支持 - SERVICE_DISCOVERY = EnumField("service_discovery", label="服务发现") - - -class LoadBalanceTypeEnum(StructuredEnum): - RR = EnumField("roundrobin", "RR") - WRR = EnumField("weighted-roundrobin", "Weighted-RR") - - -class BackendConfigSchemeEnum(StructuredEnum): - # for http type backend - HTTP = EnumField("http", label="HTTP") - HTTPS = EnumField("https", label="HTTPS") - # for grpc type backend - GRPC = EnumField("grpc", label="GRPC") - GRPCS = EnumField("grpcs", label="GRPCS") - - -BACKEND_CONFIG_SCHEME_MAP = { - BackendTypeEnum.HTTP.value: [BackendConfigSchemeEnum.HTTP.value, BackendConfigSchemeEnum.HTTPS.value], - BackendTypeEnum.GRPC.value: [BackendConfigSchemeEnum.GRPC.value, BackendConfigSchemeEnum.GRPCS.value], -} diff --git a/src/dashboard/apigateway/apigateway/apis/web/backend/serializers.py b/src/dashboard/apigateway/apigateway/apis/web/backend/serializers.py index 2e685c990..03cb7ba2a 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/backend/serializers.py +++ b/src/dashboard/apigateway/apigateway/apis/web/backend/serializers.py @@ -20,36 +20,13 @@ from rest_framework import serializers from rest_framework.validators import UniqueTogetherValidator -from apigateway.biz.constants import MAX_BACKEND_TIMEOUT_IN_SECOND +from apigateway.apis.web.serializers import BaseBackendConfigSLZ +from apigateway.apis.web.constants import BACKEND_CONFIG_SCHEME_MAP from apigateway.common.fields import CurrentGatewayDefault -from apigateway.core.constants import DEFAULT_BACKEND_NAME, HOST_WITHOUT_SCHEME_PATTERN, BackendTypeEnum +from apigateway.core.constants import DEFAULT_BACKEND_NAME, BackendTypeEnum from apigateway.core.models import Backend, BackendConfig, Stage -from .constants import ( - BACKEND_CONFIG_SCHEME_MAP, - BACKEND_NAME_PATTERN, - BackendConfigSchemeEnum, - BackendConfigTypeEnum, - LoadBalanceTypeEnum, -) - - -class HostSLZ(serializers.Serializer): - scheme = serializers.ChoiceField(choices=BackendConfigSchemeEnum.get_choices()) - host = serializers.RegexField(HOST_WITHOUT_SCHEME_PATTERN) - weight = serializers.IntegerField(min_value=1, required=False) - - class Meta: - ref_name = "apis.web.backend.HostSLZ" - - -class BaseBackendConfigSLZ(serializers.Serializer): - type = serializers.ChoiceField( - choices=BackendConfigTypeEnum.get_choices(), default=BackendConfigTypeEnum.NODE.value - ) - timeout = serializers.IntegerField(max_value=MAX_BACKEND_TIMEOUT_IN_SECOND, min_value=1) - loadbalance = serializers.ChoiceField(choices=LoadBalanceTypeEnum.get_choices()) - hosts = serializers.ListField(child=HostSLZ(), allow_empty=False) +from .constants import BACKEND_NAME_PATTERN class BackendConfigSLZ(BaseBackendConfigSLZ): diff --git a/src/dashboard/apigateway/apigateway/apis/web/constants.py b/src/dashboard/apigateway/apigateway/apis/web/constants.py index d0c6b8f2b..b1218a946 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/constants.py +++ b/src/dashboard/apigateway/apigateway/apis/web/constants.py @@ -17,6 +17,8 @@ # from blue_krill.data_types.enum import EnumField, StructuredEnum +from apigateway.core.constants import BackendTypeEnum + class UserAuthTypeEnum(StructuredEnum): IEOD = EnumField("ieod") @@ -31,3 +33,29 @@ class ExportTypeEnum(StructuredEnum): FILTERED = EnumField("filtered") # 已选资源 SELECTED = EnumField("selected") + + +class BackendConfigTypeEnum(StructuredEnum): + NODE = EnumField("node", label="节点") + # NOTE: 当前不支持 + SERVICE_DISCOVERY = EnumField("service_discovery", label="服务发现") + + +class LoadBalanceTypeEnum(StructuredEnum): + RR = EnumField("roundrobin", "RR") + WRR = EnumField("weighted-roundrobin", "Weighted-RR") + + +class BackendConfigSchemeEnum(StructuredEnum): + # for http type backend + HTTP = EnumField("http", label="HTTP") + HTTPS = EnumField("https", label="HTTPS") + # for grpc type backend + GRPC = EnumField("grpc", label="GRPC") + GRPCS = EnumField("grpcs", label="GRPCS") + + +BACKEND_CONFIG_SCHEME_MAP = { + BackendTypeEnum.HTTP.value: [BackendConfigSchemeEnum.HTTP.value, BackendConfigSchemeEnum.HTTPS.value], + BackendTypeEnum.GRPC.value: [BackendConfigSchemeEnum.GRPC.value, BackendConfigSchemeEnum.GRPCS.value], +} diff --git a/src/dashboard/apigateway/apigateway/apis/web/serializers.py b/src/dashboard/apigateway/apigateway/apis/web/serializers.py new file mode 100644 index 000000000..d4e843e44 --- /dev/null +++ b/src/dashboard/apigateway/apigateway/apis/web/serializers.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# +# TencentBlueKing is pleased to support the open source community by making +# 蓝鲸智云 - API 网关(BlueKing - APIGateway) available. +# Copyright (C) 2017 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 +# +# http://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. +# +# We undertake not to change the open source license (MIT license) applicable +# to the current version of the project delivered to anyone in the future. +# +from rest_framework import serializers + +from apigateway.biz.constants import MAX_BACKEND_TIMEOUT_IN_SECOND +from apigateway.core.constants import HOST_WITHOUT_SCHEME_PATTERN + +from .constants import BackendConfigSchemeEnum, BackendConfigTypeEnum, LoadBalanceTypeEnum + + +class HostSLZ(serializers.Serializer): + scheme = serializers.ChoiceField(choices=BackendConfigSchemeEnum.get_choices()) + host = serializers.RegexField(HOST_WITHOUT_SCHEME_PATTERN) + weight = serializers.IntegerField(min_value=1, required=False) + + class Meta: + ref_name = "apis.web.HostSLZ" + + +class BaseBackendConfigSLZ(serializers.Serializer): + type = serializers.ChoiceField( + choices=BackendConfigTypeEnum.get_choices(), default=BackendConfigTypeEnum.NODE.value + ) + timeout = serializers.IntegerField(max_value=MAX_BACKEND_TIMEOUT_IN_SECOND, min_value=1) + loadbalance = serializers.ChoiceField(choices=LoadBalanceTypeEnum.get_choices()) + hosts = serializers.ListField(child=HostSLZ(), allow_empty=False) diff --git a/src/dashboard/apigateway/apigateway/apis/web/stage/serializers.py b/src/dashboard/apigateway/apigateway/apis/web/stage/serializers.py index 486a2f42f..b728eecde 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/stage/serializers.py +++ b/src/dashboard/apigateway/apigateway/apis/web/stage/serializers.py @@ -23,8 +23,8 @@ from rest_framework.validators import UniqueTogetherValidator from tencent_apigateway_common.i18n.field import SerializerTranslatedField -from apigateway.apis.web.backend.constants import BACKEND_CONFIG_SCHEME_MAP -from apigateway.apis.web.backend.serializers import BaseBackendConfigSLZ +from apigateway.apis.web.constants import BACKEND_CONFIG_SCHEME_MAP +from apigateway.apis.web.serializers import BaseBackendConfigSLZ from apigateway.biz.validators import MaxCountPerGatewayValidator from apigateway.common.fields import CurrentGatewayDefault from apigateway.core.constants import STAGE_NAME_PATTERN, ReleaseStatusEnum, StageStatusEnum