From 43d18b66b6555d634253d4482bf58b366a2c79f3 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Thu, 26 Sep 2024 14:15:58 +0800 Subject: [PATCH] feat(publish): make resource_verison v1 disable publish (#977) --- .../apigateway/apigateway/biz/validators.py | 9 +++++++++ .../controller/publisher/publish.py | 8 ++++++++ .../apigateway/apigateway/tests/conftest.py | 5 +++++ .../publisher}/__init__.py | 0 .../publisher}/test_publish.py | 20 +++++++++++++------ 5 files changed, 36 insertions(+), 6 deletions(-) rename src/dashboard/apigateway/apigateway/tests/{common/release => controller/publisher}/__init__.py (100%) rename src/dashboard/apigateway/apigateway/tests/{common/release => controller/publisher}/test_publish.py (87%) diff --git a/src/dashboard/apigateway/apigateway/biz/validators.py b/src/dashboard/apigateway/apigateway/biz/validators.py index b72025375..a146138ff 100644 --- a/src/dashboard/apigateway/apigateway/biz/validators.py +++ b/src/dashboard/apigateway/apigateway/biz/validators.py @@ -235,6 +235,14 @@ def _validate_stage_vars(self, stage: Stage, resource_version_id: int): } ) + def _validate_resource_version_schema(self): + if not self.resource_version.is_schema_v2: + raise ReleaseValidationError( + _("版本【{resource_version}】数据结构已经不兼容,不允许发布,请在【资源配置】中新建版本再发布").format( + resource_version=self.resource_version.object_display + ) + ) + def _validate_gateway_status(self): if self.gateway.status != GatewayStatusEnum.ACTIVE.value: raise ReleaseValidationError( @@ -252,6 +260,7 @@ def __call__(self): self._validate_stage_plugins() if self.resource_version: + self._validate_resource_version_schema() self._validate_stage_vars(self.stage, self.resource_version.id) diff --git a/src/dashboard/apigateway/apigateway/controller/publisher/publish.py b/src/dashboard/apigateway/apigateway/controller/publisher/publish.py index f4636eef5..9c1a5f649 100644 --- a/src/dashboard/apigateway/apigateway/controller/publisher/publish.py +++ b/src/dashboard/apigateway/apigateway/controller/publisher/publish.py @@ -64,6 +64,14 @@ def _is_gateway_ok_for_releasing(release: Release, source: PublishSourceEnum) -> msg = f"release(id={release.pk}) stage(name={release.stage.name}) is not active, ignored" return False, msg + # 校验版本,现在只支持v2发布 + if not release.resource_version.is_schema_v2: + msg = ( + f"The data structure of version 【{release.resource_version.object_display}】 is incompatible and is not " + f"allowed to be published. Please create a new version in [Resource Configuration] before publishing." + ) + return False, msg + return True, "" diff --git a/src/dashboard/apigateway/apigateway/tests/conftest.py b/src/dashboard/apigateway/apigateway/tests/conftest.py index 4c46290f1..adc07df1a 100644 --- a/src/dashboard/apigateway/apigateway/tests/conftest.py +++ b/src/dashboard/apigateway/apigateway/tests/conftest.py @@ -433,6 +433,11 @@ def fake_release(fake_gateway, fake_stage, fake_resource_version): return G(Release, gateway=fake_gateway, stage=fake_stage, resource_version=fake_resource_version) +@pytest.fixture +def fake_release_v2(fake_gateway, fake_stage, fake_resource_version_v2): + return G(Release, gateway=fake_gateway, stage=fake_stage, resource_version=fake_resource_version_v2) + + @pytest.fixture def fake_release_history(fake_gateway, fake_stage, fake_resource_version): return G( diff --git a/src/dashboard/apigateway/apigateway/tests/common/release/__init__.py b/src/dashboard/apigateway/apigateway/tests/controller/publisher/__init__.py similarity index 100% rename from src/dashboard/apigateway/apigateway/tests/common/release/__init__.py rename to src/dashboard/apigateway/apigateway/tests/controller/publisher/__init__.py diff --git a/src/dashboard/apigateway/apigateway/tests/common/release/test_publish.py b/src/dashboard/apigateway/apigateway/tests/controller/publisher/test_publish.py similarity index 87% rename from src/dashboard/apigateway/apigateway/tests/common/release/test_publish.py rename to src/dashboard/apigateway/apigateway/tests/controller/publisher/test_publish.py index 544ae096d..d62ac0d9f 100644 --- a/src/dashboard/apigateway/apigateway/tests/common/release/test_publish.py +++ b/src/dashboard/apigateway/apigateway/tests/controller/publisher/test_publish.py @@ -57,12 +57,20 @@ def test__is_gateway_ok_for_releasing_with_inactive_stage(self, fake_stage, fake assert ok is False assert f"stage(name={fake_stage.name}) is not active, ignored" in msg - def test__is_gateway_ok_for_releasing_with_valid_release(self, fake_release): + def test__is_gateway_ok_for_releasing_with_valid_release(self, fake_release_v2): source = PublishSourceEnum.CLI_SYNC - ok, msg = _is_gateway_ok_for_releasing(fake_release, source) + ok, msg = _is_gateway_ok_for_releasing(fake_release_v2, source) assert ok is True assert msg == "" + def test__is_gateway_ok_for_releasing_with_resource_version_v1(self, fake_stage, fake_release): + source = PublishSourceEnum.BACKEND_UPDATE + ok, msg = _is_gateway_ok_for_releasing(fake_release, source) + assert ok is False + assert ( + f"The data structure of version 【{fake_release.resource_version.object_display}】 is incompatible" in msg + ) + def test__save_release_history(self, fake_release): source = PublishSourceEnum.BACKEND_UPDATE release_history = _save_release_history(fake_release, source, "test") @@ -70,20 +78,20 @@ def test__save_release_history(self, fake_release): assert release_history.gateway.pk == fake_release.gateway_id assert release_history.stage.pk == fake_release.stage_id - def test__trigger_rolling_publish(self, fake_shared_gateway, fake_release): + def test__trigger_rolling_publish(self, fake_shared_gateway, fake_release_v2): fake_shared_gateway.id = settings.DEFAULT_MICRO_GATEWAY_ID fake_shared_gateway.save() source = PublishSourceEnum.BACKEND_UPDATE - release_list = [fake_release] + release_list = [fake_release_v2] self.distributor.distribute.return_value = True, "" _trigger_rolling_publish(source, "test", release_list, True) self.distributor.distribute.assert_called() - def test__trigger_revoke_publish_for_disable_with_valid_release(self, fake_shared_gateway, fake_release): + def test__trigger_revoke_publish_for_disable_with_valid_release(self, fake_shared_gateway, fake_release_v2): fake_shared_gateway.id = settings.DEFAULT_MICRO_GATEWAY_ID fake_shared_gateway.save() source = PublishSourceEnum.GATEWAY_DISABLE - release_list = [fake_release] + release_list = [fake_release_v2] self.distributor.revoke.return_value = True, "" _trigger_revoke_publish_for_disable(source, "test", release_list, True) self.distributor.revoke.assert_called()