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

资源创建/更新/获取时,后端数据放到 backend 中 #297

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ class HttpBackendConfigSLZ(serializers.Serializer):
)


class HttpBackendSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="后端服务 ID")
config = HttpBackendConfigSLZ(help_text="后端配置")


class ResourceInputSLZ(serializers.ModelSerializer):
gateway = serializers.HiddenField(default=CurrentGatewayDefault())
name = serializers.RegexField(RESOURCE_NAME_PATTERN, max_length=256, required=True, help_text="资源名称")
path = serializers.RegexField(PATH_PATTERN, max_length=2048, help_text="前端请求路径")
auth_config = ResourceAuthConfigSLZ(help_text="认证配置")
backend_id = serializers.IntegerField(help_text="后端服务 ID")
backend_config = HttpBackendConfigSLZ(help_text="后端配置")
backend = HttpBackendSLZ(help_text="后端服务")
label_ids = serializers.ListField(
child=serializers.IntegerField(),
allow_empty=True,
Expand All @@ -181,9 +185,8 @@ class Meta:
"allow_apply_permission",
# 认证配置
"auth_config",
# 后端配置
"backend_id",
"backend_config",
# 资源后端
"backend",
# 标签
"label_ids",
]
Expand Down Expand Up @@ -240,7 +243,11 @@ def validate(self, data):
self._validate_match_subpath(data)

data["resource"] = self.instance
data["backend"] = self._validate_backend_id(data["gateway"], data["backend_id"])

# 为方便使用 ResourcesSaver 统一处理数据,对 backend 数据进行转换
data["backend_config"] = data["backend"]["config"]
# NOTE: 使用 backend 对象覆盖了原有的 backend 输入数据
data["backend"] = self._validate_backend_id(data["gateway"], data["backend"]["id"])

return data

Expand All @@ -266,7 +273,7 @@ def _validate_method(self, gateway: Gateway, path: str, method: str):
)

def _validate_match_subpath(self, data):
if data.get("match_subpath", False) != data["backend_config"].get("match_subpath", False):
if data.get("match_subpath", False) != data["backend"]["config"].get("match_subpath", False):
raise serializers.ValidationError(_("资源前端配置中的【匹配所有子路径】与后端配置中的【追加匹配的子路径】值必需相同。"))

def _exclude_current_instance(self, queryset):
Expand All @@ -291,8 +298,7 @@ def validate_label_ids(self, value):

class ResourceOutputSLZ(serializers.ModelSerializer):
auth_config = serializers.SerializerMethodField(help_text="认证配置")
backend_id = serializers.SerializerMethodField(help_text="后端服务 ID")
backend_config = serializers.SerializerMethodField(help_text="后端配置")
backend = serializers.SerializerMethodField(help_text="后端服务")
labels = serializers.SerializerMethodField(help_text="标签列表")

class Meta:
Expand All @@ -308,8 +314,7 @@ class Meta:
"is_public",
"allow_apply_permission",
"auth_config",
"backend_id",
"backend_config",
"backend",
"labels",
]
read_only_fields = fields
Expand Down Expand Up @@ -347,11 +352,11 @@ class Meta:
def get_auth_config(self, obj):
return self.context["auth_config"]

def get_backend_id(self, obj):
return self.context["proxy"].backend_id

def get_backend_config(self, obj):
return self.context["proxy"].config
def get_backend(self, obj):
return {
"id": self.context["proxy"].backend_id,
"config": self.context["proxy"].config,
}

def get_labels(self, obj):
return self.context["labels"].get(obj.id, [])
Expand Down Expand Up @@ -581,22 +586,20 @@ class ResourceExportOutputSLZ(serializers.Serializer):
allow_apply_permission = serializers.BooleanField(help_text="是否允许应用在开发者中心申请访问资源的权限")

labels = serializers.SerializerMethodField(help_text="标签列表")
backend_name = serializers.SerializerMethodField(help_text="后端服务名称")
backend_config = serializers.SerializerMethodField(help_text="后端配置")
backend = serializers.SerializerMethodField(help_text="后端服务")
plugin_configs = serializers.SerializerMethodField(help_text="插件配置")
auth_config = serializers.SerializerMethodField(help_text="认证配置")

def get_labels(self, obj):
labels = self.context["labels"].get(obj.id, [])
return [label["name"] for label in labels]

def get_backend_name(self, obj):
proxy = self.context["proxies"][obj.id]
return self.context["backends"][proxy.backend_id].name

def get_backend_config(self, obj):
def get_backend(self, obj):
proxy = self.context["proxies"][obj.id]
return proxy.config
return {
"name": self.context["backends"][proxy.backend_id].name,
"config": proxy.config,
}

def get_auth_config(self, obj):
return self.context["auth_configs"][obj.id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,14 @@ def _generate_paths(self, resources: List[Dict]) -> Dict[str, Any]:
return paths

def _generate_bk_apigateway_resource(self, operation: Dict[str, Any], resource: Dict[str, Any]):
backend = resource.get("backend", {})

operation[SwaggerExtensionEnum.RESOURCE.value] = {
"isPublic": resource["is_public"],
"allowApplyPermission": resource["allow_apply_permission"],
"matchSubpath": resource.get("match_subpath", False),
"backend": self._adapt_backend(
resource.get("backend_name", ""),
resource.get("backend_config", {}),
backend,
resource.get("proxy_type", ""),
resource.get("proxy_configs", {}),
),
Expand All @@ -320,29 +321,29 @@ def _adapt_method(self, method: str) -> str:

return method.lower()

def _adapt_backend(self, backend_name: str, backend_config: Dict, proxy_type: str, proxy_configs: Dict) -> Dict:
backend = {}
def _adapt_backend(self, backend: Dict, proxy_type: str, proxy_configs: Dict) -> Dict:
result = {}

if backend_name:
backend["name"] = backend_name
if backend.get("name"):
result["name"] = backend["name"]

if proxy_type:
backend["type"] = proxy_type.upper()
result["type"] = proxy_type.upper()

if backend_config:
backend.update(
if backend.get("config"):
result.update(
{
"method": backend_config["method"].lower(),
"path": backend_config["path"],
"matchSubpath": backend_config.get("match_subpath", False),
"timeout": backend_config.get("timeout", 0),
"method": backend["config"]["method"].lower(),
"path": backend["config"]["path"],
"matchSubpath": backend["config"].get("match_subpath", False),
"timeout": backend["config"].get("timeout", 0),
}
)
return backend
return result

if proxy_type == ProxyTypeEnum.HTTP.value:
http_config = proxy_configs[ProxyTypeEnum.HTTP.value]
backend.update(
result.update(
{
"method": http_config["method"].lower(),
"path": http_config["path"],
Expand All @@ -355,7 +356,7 @@ def _adapt_backend(self, backend_name: str, backend_config: Dict, proxy_type: st
else:
raise ValueError(f"unsupported proxy_type: {proxy_type}")

return backend
return result

def _adapt_auth_config(self, auth_config: Dict) -> Dict:
config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ def test_validate(self, fake_resource, faker):
"app_verified_required": True,
"resource_perm_required": True,
},
"backend_config": {
"method": "GET",
"path": "/test",
"match_subpath": False,
"timeout": 0,
"backend": {
"id": backend.id,
"config": {
"method": "GET",
"path": "/test",
"match_subpath": False,
"timeout": 0,
},
},
"backend_id": backend.id,
"label_ids": [],
}

Expand Down Expand Up @@ -206,8 +208,8 @@ def test_validate_method(self, fake_gateway):
@pytest.mark.parametrize(
"data",
[
{"match_subpath": True, "backend_config": {"match_subpath": False}},
{"match_subpath": False, "backend_config": {"match_subpath": True}},
{"match_subpath": True, "backend": {"config": {"match_subpath": False}}},
{"match_subpath": False, "backend": {"config": {"match_subpath": True}}},
],
)
def test_validate_match_subpath__error(self, data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def test_list(self, request_view, fake_gateway, fake_backend, data, expected):
"path": "/echo/",
"match_subpath": False,
"label_ids": [],
"backend_config": {
"method": "GET",
"path": "/echo/",
"match_subpath": False,
"timeout": 30,
"backend": {
"config": {
"method": "GET",
"path": "/echo/",
"match_subpath": False,
"timeout": 30,
}
},
"auth_config": {
"auth_verified_required": False,
Expand All @@ -96,7 +98,7 @@ def test_list(self, request_view, fake_gateway, fake_backend, data, expected):
)
def test_create(self, request_view, fake_gateway, data):
backend = G(Backend, gateway=fake_gateway)
data["backend_id"] = backend.id
data["backend"]["id"] = backend.id

resp = request_view(
method="POST",
Expand Down Expand Up @@ -152,11 +154,13 @@ def test_update(self, request_view, fake_resource):
"method": "POST",
"path": "/echo/",
"label_ids": [],
"backend_id": backend.id,
"backend_config": {
"method": "GET",
"path": "/echo/",
"timeout": 30,
"backend": {
"id": backend.id,
"config": {
"method": "GET",
"path": "/echo/",
"timeout": 30,
},
},
"auth_config": {
"auth_verified_required": False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,14 @@ def fake_resource_dict(self):
"labels": ["testing"],
"is_public": True,
"allow_apply_permission": True,
"backend_name": "default",
"backend_config": {
"method": "POST",
"path": "/users",
"match_subpath": False,
"timeout": 0,
"backend": {
"name": "default",
"config": {
"method": "POST",
"path": "/users",
"match_subpath": False,
"timeout": 0,
},
},
"auth_config": {
"auth_verified_required": True,
Expand Down Expand Up @@ -968,11 +970,11 @@ def test_generate_paths(self, fake_resource_dict):
"allowApplyPermission": fake_resource_dict["allow_apply_permission"],
"matchSubpath": fake_resource_dict["match_subpath"],
"backend": {
"name": fake_resource_dict["backend_name"],
"method": fake_resource_dict["backend_config"]["method"].lower(),
"path": fake_resource_dict["backend_config"]["path"],
"matchSubpath": fake_resource_dict["backend_config"]["match_subpath"],
"timeout": fake_resource_dict["backend_config"]["timeout"],
"name": fake_resource_dict["backend"]["name"],
"method": fake_resource_dict["backend"]["config"]["method"].lower(),
"path": fake_resource_dict["backend"]["config"]["path"],
"matchSubpath": fake_resource_dict["backend"]["config"]["match_subpath"],
"timeout": fake_resource_dict["backend"]["config"]["timeout"],
},
"authConfig": {
"userVerifiedRequired": fake_resource_dict["auth_config"]["auth_verified_required"],
Expand Down Expand Up @@ -1005,8 +1007,10 @@ def test_adapt_method(self):
[
(
{
"backend_name": "foo",
"backend_config": {"method": "GET", "path": "/foo"},
"backend": {
"name": "foo",
"config": {"method": "GET", "path": "/foo"},
},
"proxy_type": "http",
"proxy_configs": {},
},
Expand All @@ -1021,8 +1025,7 @@ def test_adapt_method(self):
),
(
{
"backend_name": "",
"backend_config": {},
"backend": {},
"proxy_type": "http",
"proxy_configs": {
"http": {
Expand Down Expand Up @@ -1053,8 +1056,7 @@ def test_adapt_backend(self, data, expected):

def test_adapt_backend__error(self):
data = {
"backend_name": "",
"backend_config": {},
"backend": {},
"proxy_type": "mock",
"proxy_configs": {},
}
Expand Down