diff --git a/src/dashboard/apigateway/apigateway/biz/validators.py b/src/dashboard/apigateway/apigateway/biz/validators.py index 85464d27b..e1437bb04 100644 --- a/src/dashboard/apigateway/apigateway/biz/validators.py +++ b/src/dashboard/apigateway/apigateway/biz/validators.py @@ -188,7 +188,15 @@ def __init__(self, gateway: Gateway, stage: Stage, resource_version: Optional[Re def _validate_stage_backends(self): """校验待发布环境的backend配置""" - backend_configs = BackendConfig.objects.filter(stage=self.stage) + resource_version = self.resource_version + + if resource_version and resource_version.data: + backend_ids = list({resource["proxy"]["backend_id"] for resource in resource_version.data + if resource["proxy"].get("backend_id", None)}) + backend_configs = BackendConfig.objects.filter(backend_id__in=backend_ids) + else: + backend_configs = BackendConfig.objects.filter(stage=self.stage) + for backend_config in backend_configs: for host in backend_config.config["hosts"]: if not core_constants.HOST_WITHOUT_SCHEME_PATTERN.match(host["host"]): diff --git a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py index f0318a948..84bcdbe42 100644 --- a/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py +++ b/src/dashboard/apigateway/apigateway/tests/biz/test_validators.py @@ -335,6 +335,55 @@ def test_validate_stage_plugins( with pytest.raises(ReleaseValidationError): publish_validator._validate_stage_plugins() + def test_validate_stage_backends( + self, + fake_stage, + fake_gateway + ): + + backend = G( + Backend, + gateway=fake_gateway, + type="http", + name="test_name", + description="test", + ) + + backend_config = G( + BackendConfig, + gateway=fake_gateway, + backend_id=backend.id, + stage_id=227, + config={"timeout": 30, "loadbalance": "roundrobin", + "hosts": [{"scheme": "http", "host": "ee.com", "weight": 100}]}, + ) + + resource_version = G( + ResourceVersion, + gateway=fake_gateway, + version="1", + name="11", + title="11", + _data=json.dumps([{ + "id": 1, + "name": "approval_add_workitems", + "proxy": { + "id": 28, + "type": "http", + "backend_id": backend.id, + "config": json.dumps({ + "method": "ANY", + "path": "/api/v2/", + "match_subpath": False, + "timeout": 0 + }) + } + }]), + ) + + publish_validator = PublishValidator(fake_gateway, fake_stage, resource_version) + publish_validator._validate_stage_backends() + class TestSchemeInputValidator: @pytest.mark.parametrize(