Skip to content

Commit

Permalink
feat: support no-pub (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun authored Sep 6, 2024
1 parent 33f9491 commit 3b99fdb
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 53 deletions.
3 changes: 3 additions & 0 deletions sdks/apigw-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 只进行版本生成,不进行资源发布。
4 changes: 3 additions & 1 deletion sdks/apigw-manager/docs/sync-apigateway-with-django.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

# 可选,为应用主动授权
Expand Down Expand Up @@ -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}"

# 获取网关公钥
Expand Down
3 changes: 3 additions & 0 deletions sdks/apigw-manager/docs/sync-apigateway-with-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion sdks/apigw-manager/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def default_command_flags(definition_file):
"title": "",
"comment": "",
"generate_sdks": False,
"no_pub": False,
}


Expand Down Expand Up @@ -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),
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

0 comments on commit 3b99fdb

Please sign in to comment.