diff --git a/sdks/apigw-manager/README.md b/sdks/apigw-manager/README.md index 21c5305c..30b67167 100644 --- a/sdks/apigw-manager/README.md +++ b/sdks/apigw-manager/README.md @@ -74,3 +74,6 @@ definition.yaml 指定的环境配置适用于 `sync_apigw_stage` 命令,而 访问 `http://apigw.__bk_domain__/backend/admin42/core/apirelatedapp/`,添加应用操作网关数据的权限 +### 6. 注册网关资源时如果只想注册生成版本,不进行版本发布,该如何操作? + +`create_version_and_release_apigw` 是进行网关资源生成版本和发布的命令,可以通过指定 `--no-pub` 只进行版本生成,不进行资源发布。 \ No newline at end of file diff --git a/sdks/apigw-manager/docs/sync-apigateway-with-django.md b/sdks/apigw-manager/docs/sync-apigateway-with-django.md index aaa36288..a40a0628 100644 --- a/sdks/apigw-manager/docs/sync-apigateway-with-django.md +++ b/sdks/apigw-manager/docs/sync-apigateway-with-django.md @@ -39,7 +39,8 @@ python manage.py sync_apigw_resources --delete --gateway-name=${gateway_name} -- # 可选,同步资源文档 python manage.py sync_resource_docs_by_archive --gateway-name=${gateway_name} --file="${definition_file}" -# 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 创建资源版本、发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 python manage.py create_version_and_release_apigw --gateway-name=${gateway_name} --file="${definition_file}" # 可选,为应用主动授权 @@ -153,6 +154,7 @@ python manage.py add_related_apps --gateway-name=${gateway_name} --file="${defin python manage.py apply_apigw_permissions --gateway-name=${gateway_name} --file="${definition_file}" # 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 python manage.py create_version_and_release_apigw --gateway-name=${gateway_name} --file="${definition_file}" # 获取网关公钥 diff --git a/sdks/apigw-manager/docs/sync-apigateway-with-docker.md b/sdks/apigw-manager/docs/sync-apigateway-with-docker.md index 2b07e916..f17be25e 100644 --- a/sdks/apigw-manager/docs/sync-apigateway-with-docker.md +++ b/sdks/apigw-manager/docs/sync-apigateway-with-docker.md @@ -80,6 +80,8 @@ title "fetch apigateway public key" apigw-manager.sh fetch_apigw_public_key --gateway-name=${gateway_name} --print > "apigateway.pub" title "releasing" +# 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK, 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 call_definition_command_or_exit create_version_and_release_apigw "${definition_file}" --gateway-name=${gateway_name} log_info "done" @@ -274,6 +276,7 @@ call_definition_command_or_exit add_related_apps "${definition_file}" --gateway- call_definition_command_or_exit apply_apigw_permissions "${definition_file}" --gateway-name=${gateway_name} # 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK, 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 call_definition_command_or_exit create_version_and_release_apigw "${definition_file}" --gateway-name=${gateway_name} # 获取网关公钥,存放到文件 apigateway.pub diff --git a/sdks/apigw-manager/examples/chart/use-configmap/files/support-files/bin/sync-apigateway.sh b/sdks/apigw-manager/examples/chart/use-configmap/files/support-files/bin/sync-apigateway.sh index 5879f32c..507dae38 100755 --- a/sdks/apigw-manager/examples/chart/use-configmap/files/support-files/bin/sync-apigateway.sh +++ b/sdks/apigw-manager/examples/chart/use-configmap/files/support-files/bin/sync-apigateway.sh @@ -26,6 +26,8 @@ title "fetch apigateway public key" apigw-manager.sh fetch_apigw_public_key --gateway-name=${gateway_name} --print > "/tmp/apigateway.pub" title "releasing" +# 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK, 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 call_definition_command_or_exit create_version_and_release_apigw "${definition_file}" --gateway-name=${gateway_name} title "done" diff --git a/sdks/apigw-manager/examples/chart/use-custom-docker-image/my-apigw-manager/support-files/bin/sync-apigateway.sh b/sdks/apigw-manager/examples/chart/use-custom-docker-image/my-apigw-manager/support-files/bin/sync-apigateway.sh index eb86200d..f9c10001 100755 --- a/sdks/apigw-manager/examples/chart/use-custom-docker-image/my-apigw-manager/support-files/bin/sync-apigateway.sh +++ b/sdks/apigw-manager/examples/chart/use-custom-docker-image/my-apigw-manager/support-files/bin/sync-apigateway.sh @@ -26,6 +26,8 @@ title "fetch apigateway public key" apigw-manager.sh fetch_apigw_public_key --gateway-name=${gateway_name} --print > "apigateway.pub" title "releasing" +# 创建资源版本并发布;指定参数 --generate-sdks 时,会同时生成资源版本对应的网关 SDK, 指定 --stage stage1 stage2 时会发布指定环境,不设置则发布所有环境 +# 指定参数 --no-pub 则只生成版本,不发布 call_definition_command_or_exit create_version_and_release_apigw "${definition_file}" --gateway-name=${gateway_name} title "done" diff --git a/sdks/apigw-manager/pyproject.toml b/sdks/apigw-manager/pyproject.toml index a0510fee..8a7f651f 100644 --- a/sdks/apigw-manager/pyproject.toml +++ b/sdks/apigw-manager/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "apigw-manager" -version = "3.1.0" +version = "3.1.1" description = "The SDK for managing blueking gateway resource." readme = "README.md" authors = ["blueking "] diff --git a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/create_version_and_release_apigw.py b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/create_version_and_release_apigw.py index 5cc87d97..a7dabdc7 100644 --- a/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/create_version_and_release_apigw.py +++ b/sdks/apigw-manager/src/apigw_manager/apigw/management/commands/create_version_and_release_apigw.py @@ -35,6 +35,7 @@ def add_arguments(self, parser): parser.add_argument("-c", "--comment", default="", help="release comment") parser.add_argument("-s", "--stage", default=[], nargs="+", help="release stages") parser.add_argument("--generate-sdks", default=False, action="store_true", help="with sdks generation") + parser.add_argument("--no-pub", default=False, action="store_true", help="only make version") def _parse_version_from_definition(self, definition): version = definition.get("version") @@ -89,7 +90,7 @@ def _generate_sdks(self, releaser, version, *args, **kwargs): except Exception as err: print("warning!! generate sdks failed: %s" % err) - def handle(self, stage, title, comment, generate_sdks, *args, **kwargs): + def handle(self, stage, title, comment, generate_sdks, no_pub, *args, **kwargs): configuration = self.get_configuration(**kwargs) definition = self.get_definition(**kwargs) defined_version = self._parse_version_from_definition(definition) @@ -121,16 +122,18 @@ def handle(self, stage, title, comment, generate_sdks, *args, **kwargs): generate_sdks = False print("resource_version %s already exists, skip creating" % latest_version) - result = releaser.release( - version=resource_version["version"], - title=title or resource_version.get("title", ""), - comment=comment or resource_version.get("comment", ""), - stage_names=stage, - ) - print( - "API gateway released %s, title %s, stages %s" - % (result.get("version"), result["resource_version_title"], result["stage_names"]) - ) + # 如果没有设置不发布 + if not no_pub: + result = releaser.release( + version=resource_version["version"], + title=title or resource_version.get("title", ""), + comment=comment or resource_version.get("comment", ""), + stage_names=stage, + ) + print( + "API gateway released %s, title %s, stages %s" + % (result.get("version"), result["resource_version_title"], result["stage_names"]) + ) # create a sdk when released a new version if generate_sdks: diff --git a/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_create_version_and_release_apigw.py b/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_create_version_and_release_apigw.py index 335e6398..1f1be184 100644 --- a/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_create_version_and_release_apigw.py +++ b/sdks/apigw-manager/tests/apigw_manager/apigw/management/commands/test_create_version_and_release_apigw.py @@ -27,6 +27,7 @@ def default_command_flags(definition_file): "title": "", "comment": "", "generate_sdks": False, + "no_pub": False, } @@ -160,11 +161,11 @@ def test_should_create_resource_version(mocker, command, defined_version, latest ], ) def test_get_version_to_be_created( - command, - datetime_now, - defined_version, - resource_version_exists, - expected, + command, + datetime_now, + defined_version, + resource_version_exists, + expected, ): result = command._get_version_to_be_created( parse_version(defined_version), @@ -177,15 +178,15 @@ def test_get_version_to_be_created( class TestHandle: def test_handle_version_not_change( - self, - command, - fetcher, - releaser, - faker, - definition_file, - resource_sync_manager, - fake_resource_version, - default_command_flags, + self, + command, + fetcher, + releaser, + faker, + definition_file, + resource_sync_manager, + fake_resource_version, + default_command_flags, ): definition_file.write(yaml.dump(fake_resource_version)) stage = faker.pystr() @@ -210,15 +211,15 @@ def test_handle_version_not_change( ) def test_handle_version_not_change_but_dirty( - self, - command, - fetcher, - releaser, - faker, - definition_file, - resource_sync_manager, - fake_resource_version, - default_command_flags, + self, + command, + fetcher, + releaser, + faker, + definition_file, + resource_sync_manager, + fake_resource_version, + default_command_flags, ): definition_file.write(yaml.dump(fake_resource_version)) stage = faker.pystr() @@ -255,14 +256,14 @@ def test_handle_version_not_change_but_dirty( ) def test_handle_version_changed( - self, - command, - fetcher, - releaser, - faker, - definition_file, - fake_resource_version, - default_command_flags, + self, + command, + fetcher, + releaser, + faker, + definition_file, + fake_resource_version, + default_command_flags, ): defined_version = "1.0.0-alpha2" definition_file.write( @@ -299,15 +300,15 @@ def test_handle_version_changed( ) def test_handle_version_not_set( - self, - command, - fetcher, - releaser, - faker, - definition_file, - resource_sync_manager, - fake_resource_version, - default_command_flags, + self, + command, + fetcher, + releaser, + faker, + definition_file, + resource_sync_manager, + fake_resource_version, + default_command_flags, ): definition_file.write( yaml.dump( @@ -351,3 +352,29 @@ def test_handle_version_not_set( comment=fake_resource_version["comment"], stage_names=stage, ) + + def test_handle_without_release( + self, + command, + fetcher, + releaser, + faker, + definition_file, + resource_sync_manager, + fake_resource_version, + default_command_flags, + ): + definition_file.write(yaml.dump(fake_resource_version)) + stage = faker.pystr() + + default_command_flags["no_pub"] = True + + fetcher.latest_resource_version.return_value = fake_resource_version + + resource_sync_manager.is_dirty.return_value = False + + command.handle(stage=stage, **default_command_flags) + + releaser.create_resource_version.assert_not_called() + + releaser.releaser.release.assert_not_called()