zt-apollo-cli 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zt-apollo-cli
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Apollo configuration management CLI
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -104,6 +104,56 @@ zt-apollo-cli app del-namespace-release-publisher -a your_app_id -n application
104
104
  zt-apollo-cli app update -a your_app_id --file ./app-update.json
105
105
  ```
106
106
 
107
+ ### 11) 修改 Namespace 配置
108
+
109
+ 适用于 yml/json 等文本类 Namespace。`namespaceId` 与 `format` 默认从 `list-namespace` 自动解析。
110
+
111
+ ```bash
112
+ # 从文件更新
113
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --file ./application.yml --env DEV --cluster default
114
+
115
+ # 直接传入配置文本
116
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --text "# config"
117
+ ```
118
+
119
+ ### 12) 发布 Namespace 配置
120
+
121
+ 将未发布的 Namespace 变更发布到指定环境。`--title` 未指定时自动生成(如 `20260613144414-release`)。
122
+
123
+ ```bash
124
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --env DEV --cluster default --comment "发布说明"
125
+
126
+ # 指定发布标题
127
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --title "20260613144414-release" --comment "s"
128
+ ```
129
+
130
+ ### 13) 创建 Namespace
131
+
132
+ 在应用下新建 Namespace。默认追加 appId 前缀(`appendNamespacePrefix=true`)。
133
+
134
+ ```bash
135
+ zt-apollo-cli app create-namespace -a your_app_id -n test --format properties --comment ""
136
+
137
+ # 不追加前缀
138
+ zt-apollo-cli app create-namespace -a your_app_id -n application.yml --format yml --no-append-prefix
139
+ ```
140
+
141
+ ### 14) 删除 Namespace(指定环境)
142
+
143
+ 删除指定 env/cluster 下的 Namespace。
144
+
145
+ ```bash
146
+ zt-apollo-cli app delete-namespace -a your_app_id -n test --env DEV --cluster default
147
+ ```
148
+
149
+ ### 15) 删除应用 Namespace(所有环境)
150
+
151
+ 删除应用下的 Namespace 定义及所有环境的配置。
152
+
153
+ ```bash
154
+ zt-apollo-cli app delete-app-namespace -a your_app_id -n test
155
+ ```
156
+
107
157
  ## 全局参数
108
158
  - `-o, --output`:输出格式,支持:
109
159
  - `json`(默认)
@@ -91,6 +91,56 @@ zt-apollo-cli app del-namespace-release-publisher -a your_app_id -n application
91
91
  zt-apollo-cli app update -a your_app_id --file ./app-update.json
92
92
  ```
93
93
 
94
+ ### 11) 修改 Namespace 配置
95
+
96
+ 适用于 yml/json 等文本类 Namespace。`namespaceId` 与 `format` 默认从 `list-namespace` 自动解析。
97
+
98
+ ```bash
99
+ # 从文件更新
100
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --file ./application.yml --env DEV --cluster default
101
+
102
+ # 直接传入配置文本
103
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --text "# config"
104
+ ```
105
+
106
+ ### 12) 发布 Namespace 配置
107
+
108
+ 将未发布的 Namespace 变更发布到指定环境。`--title` 未指定时自动生成(如 `20260613144414-release`)。
109
+
110
+ ```bash
111
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --env DEV --cluster default --comment "发布说明"
112
+
113
+ # 指定发布标题
114
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --title "20260613144414-release" --comment "s"
115
+ ```
116
+
117
+ ### 13) 创建 Namespace
118
+
119
+ 在应用下新建 Namespace。默认追加 appId 前缀(`appendNamespacePrefix=true`)。
120
+
121
+ ```bash
122
+ zt-apollo-cli app create-namespace -a your_app_id -n test --format properties --comment ""
123
+
124
+ # 不追加前缀
125
+ zt-apollo-cli app create-namespace -a your_app_id -n application.yml --format yml --no-append-prefix
126
+ ```
127
+
128
+ ### 14) 删除 Namespace(指定环境)
129
+
130
+ 删除指定 env/cluster 下的 Namespace。
131
+
132
+ ```bash
133
+ zt-apollo-cli app delete-namespace -a your_app_id -n test --env DEV --cluster default
134
+ ```
135
+
136
+ ### 15) 删除应用 Namespace(所有环境)
137
+
138
+ 删除应用下的 Namespace 定义及所有环境的配置。
139
+
140
+ ```bash
141
+ zt-apollo-cli app delete-app-namespace -a your_app_id -n test
142
+ ```
143
+
94
144
  ## 全局参数
95
145
  - `-o, --output`:输出格式,支持:
96
146
  - `json`(默认)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "zt-apollo-cli"
7
- version = "0.3.0"
7
+ version = "0.4.0"
8
8
  description = "Apollo configuration management CLI"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -77,6 +77,64 @@ class ApolloApi:
77
77
  f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces",
78
78
  )
79
79
 
80
+ def create_app_namespace(
81
+ self,
82
+ app_id: str,
83
+ name: str,
84
+ comment: str = "",
85
+ is_public: bool = False,
86
+ config_format: str = "properties",
87
+ append_namespace_prefix: bool = True,
88
+ ):
89
+ """创建应用 Namespace"""
90
+ payload = {
91
+ "appId": app_id,
92
+ "name": name,
93
+ "comment": comment,
94
+ "isPublic": is_public,
95
+ "format": config_format,
96
+ }
97
+ headers = {
98
+ "Content-Type": "application/json;charset=UTF-8"
99
+ }
100
+ return self._request(
101
+ "POST",
102
+ f"/apps/{app_id}/appnamespaces",
103
+ params={"appendNamespacePrefix": str(append_namespace_prefix).lower()},
104
+ json=payload,
105
+ headers=headers,
106
+ )
107
+
108
+ def clean_namespace(
109
+ self,
110
+ app_id: str,
111
+ namespace: str
112
+ ):
113
+ """删除应用 Namespace(该应用下所有环境)"""
114
+ headers = {
115
+ }
116
+ return self._request(
117
+ "DELETE",
118
+ f"/apps/{app_id}/appnamespaces/{namespace}",
119
+ headers=headers,
120
+ )
121
+
122
+ def delete_namespace(
123
+ self,
124
+ app_id: str,
125
+ namespace: str,
126
+ env: str = "DEV",
127
+ cluster: str = "default"
128
+ ):
129
+ """删除指定环境下的 Namespace"""
130
+ headers = {
131
+ }
132
+ return self._request(
133
+ "DELETE",
134
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}",
135
+ headers=headers,
136
+ )
137
+
80
138
  def get_namespace_active_release(
81
139
  self,
82
140
  app_id: str,
@@ -151,3 +209,96 @@ class ApolloApi:
151
209
  data=user_code,
152
210
  headers={"Content-Type": "text/plain;charset=UTF-8"},
153
211
  )
212
+
213
+ def find_namespace(
214
+ self,
215
+ app_id: str,
216
+ namespace: str,
217
+ env: str = "DEV",
218
+ cluster: str = "default",
219
+ ) -> dict:
220
+ """按名称查找 Namespace,返回 namespaceId 与 format"""
221
+ namespaces = self.list_namespaces(app_id=app_id, env=env, cluster=cluster)
222
+ if not isinstance(namespaces, list):
223
+ raise Exception(f"无法解析 Namespace 列表: {namespaces}")
224
+
225
+ for item in namespaces:
226
+ base_info = item.get("baseInfo") or {}
227
+ if base_info.get("namespaceName") == namespace:
228
+ namespace_id = base_info.get("id")
229
+ if namespace_id is None:
230
+ raise Exception(f"Namespace {namespace!r} 缺少 id")
231
+ return {
232
+ "namespaceId": namespace_id,
233
+ "format": item.get("format"),
234
+ }
235
+
236
+ raise Exception(
237
+ f"未找到 Namespace: {namespace!r} "
238
+ f"(appId={app_id}, env={env}, cluster={cluster})"
239
+ )
240
+
241
+ def update_namespace_config(
242
+ self,
243
+ app_id: str,
244
+ namespace: str,
245
+ config_text: str,
246
+ env: str = "DEV",
247
+ cluster: str = "default",
248
+ namespace_id: int | None = None,
249
+ config_format: str | None = None
250
+ ):
251
+ """更新 Namespace 配置(文本类 format,如 yml/json)"""
252
+ if namespace_id is None or config_format is None:
253
+ info = self.find_namespace(
254
+ app_id=app_id,
255
+ namespace=namespace,
256
+ env=env,
257
+ cluster=cluster,
258
+ )
259
+ namespace_id = namespace_id or info["namespaceId"]
260
+ config_format = config_format or info["format"]
261
+
262
+ if not config_format:
263
+ raise Exception(f"无法确定 Namespace {namespace!r} 的 format")
264
+
265
+ payload = {
266
+ "configText": config_text,
267
+ "namespaceId": namespace_id,
268
+ "format": config_format,
269
+ }
270
+ headers = {
271
+ "Content-Type": "application/json;charset=UTF-8",
272
+ }
273
+ return self._request(
274
+ "PUT",
275
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}/items",
276
+ json=payload,
277
+ headers=headers,
278
+ )
279
+
280
+ def release_namespace(
281
+ self,
282
+ app_id: str,
283
+ namespace: str,
284
+ release_title: str,
285
+ release_comment: str = "",
286
+ is_emergency_publish: bool = False,
287
+ env: str = "DEV",
288
+ cluster: str = "default"
289
+ ):
290
+ """发布 Namespace 配置"""
291
+ payload = {
292
+ "releaseTitle": release_title,
293
+ "releaseComment": release_comment,
294
+ "isEmergencyPublish": is_emergency_publish,
295
+ }
296
+ headers = {
297
+ "Content-Type": "application/json;charset=UTF-8",
298
+ }
299
+ return self._request(
300
+ "POST",
301
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}/releases",
302
+ json=payload,
303
+ headers=headers,
304
+ )
@@ -2,6 +2,7 @@
2
2
  import click
3
3
  import json
4
4
  import shutil
5
+ from datetime import datetime
5
6
  from .api import ApolloApi
6
7
  from .auth import login
7
8
  from .config import config
@@ -316,6 +317,90 @@ def list_namespaces(ctx, app_id, env, cluster):
316
317
  result = api.list_namespaces(app_id=app_id, env=env, cluster=cluster)
317
318
  _print_result(ctx, result, preferred_headers=["name", "appId", "clusterName", "dataChangeLastModifiedTime"])
318
319
 
320
+
321
+ @app.command("create-namespace")
322
+ @click.option("--app-id", "-a", required=True, help="应用 appId")
323
+ @click.option("--name", "-n", required=True, help="Namespace 名称(不含 appId 前缀)")
324
+ @click.option("--comment", default="", show_default=True, help="备注")
325
+ @click.option("--public", is_flag=True, help="是否为公共 Namespace")
326
+ @click.option(
327
+ "--format",
328
+ "config_format",
329
+ type=click.Choice(["properties", "xml", "json", "yaml", "yml"], case_sensitive=False),
330
+ default="properties",
331
+ show_default=True,
332
+ help="配置格式",
333
+ )
334
+ @click.option(
335
+ "--append-prefix/--no-append-prefix",
336
+ default=True,
337
+ show_default=True,
338
+ help="是否追加 Namespace 前缀(appendNamespacePrefix)",
339
+ )
340
+ @click.pass_context
341
+ def create_namespace(ctx, app_id, name, comment, public, config_format, append_prefix):
342
+ """创建应用 Namespace"""
343
+ api = ApolloApi()
344
+ result = api.create_app_namespace(
345
+ app_id=app_id,
346
+ name=name,
347
+ comment=comment,
348
+ is_public=public,
349
+ config_format=config_format.lower(),
350
+ append_namespace_prefix=append_prefix,
351
+ )
352
+
353
+ if ctx.obj["output_format"] == "json":
354
+ click.echo(json.dumps(result, ensure_ascii=False, indent=2))
355
+ else:
356
+ click.echo(f"Namespace 已创建: {name} (appId={app_id}, format={config_format})")
357
+
358
+
359
+ @app.command("delete-namespace")
360
+ @click.option("--app-id", "-a", required=True, help="应用 appId")
361
+ @click.option("--namespace", "-n", required=True, help="Namespace 名称(如 test)")
362
+ @click.option("--env", default="DEV", show_default=True, help="环境")
363
+ @click.option("--cluster", default="default", show_default=True, help="集群")
364
+ @click.pass_context
365
+ def delete_namespace(ctx, app_id, namespace, env, cluster):
366
+ """删除指定环境下的 Namespace"""
367
+ api = ApolloApi()
368
+ result = api.delete_namespace(
369
+ app_id=app_id,
370
+ namespace=namespace,
371
+ env=env,
372
+ cluster=cluster,
373
+ )
374
+
375
+ if ctx.obj["output_format"] == "json":
376
+ click.echo(json.dumps(result, ensure_ascii=False, indent=2))
377
+ else:
378
+ click.echo(
379
+ f"Namespace 已删除: {namespace} "
380
+ f"(appId={app_id}, env={env}, cluster={cluster})"
381
+ )
382
+
383
+
384
+ @app.command("clean-namespace")
385
+ @click.option("--app-id", "-a", required=True, help="应用 appId")
386
+ @click.option("--namespace", "-n", required=True, help="Namespace 名称(如 test)")
387
+ @click.pass_context
388
+ def clean_namespace(ctx, app_id, namespace):
389
+ """删除应用 Namespace(删除该应用下所有环境的配置)"""
390
+ api = ApolloApi()
391
+ result = api.clean_namespace(
392
+ app_id=app_id,
393
+ namespace=namespace,
394
+ )
395
+
396
+ if ctx.obj["output_format"] == "json":
397
+ click.echo(json.dumps(result, ensure_ascii=False, indent=2))
398
+ else:
399
+ click.echo(
400
+ f"应用 Namespace 已删除: {namespace} "
401
+ f"(appId={app_id}, 所有环境)"
402
+ )
403
+
319
404
  @app.command("show-namespace-release")
320
405
  @click.option("--app-id", "-a", required=True, help="应用 appId")
321
406
  @click.option("--namespace", "-n", required=True, help="Namespace 名称")
@@ -354,6 +439,88 @@ def update_app(ctx, app_id, payload_file):
354
439
  else:
355
440
  click.echo("应用配置更新成功")
356
441
 
442
+
443
+ @app.command("update-namespace")
444
+ @click.option("--app-id", "-a", required=True, help="应用 appId")
445
+ @click.option("--namespace", "-n", required=True, help="Namespace 名称(如 application.yml)")
446
+ @click.option("--env", default="DEV", show_default=True, help="环境")
447
+ @click.option("--cluster", default="default", show_default=True, help="集群")
448
+ @click.option(
449
+ "--file",
450
+ "config_file",
451
+ type=click.Path(exists=True, dir_okay=False),
452
+ help="配置文件内容(与 --text 二选一)",
453
+ )
454
+ @click.option("--text", help="配置文本内容(与 --file 二选一)")
455
+ @click.option("--namespace-id", type=int, help="Namespace ID(默认从 list-namespace 自动解析)")
456
+ @click.option("--format", "config_format", help="配置 format(默认从 list-namespace 自动解析,如 yml)")
457
+ @click.pass_context
458
+ def update_namespace(ctx, app_id, namespace, env, cluster, config_file, text, namespace_id, config_format):
459
+ """修改 Namespace 配置(文本类 format,如 yml/json)"""
460
+ if bool(config_file) == bool(text):
461
+ raise click.UsageError("请指定 --file 或 --text 之一")
462
+
463
+ if config_file:
464
+ with open(config_file, encoding="utf-8") as f:
465
+ config_text = f.read()
466
+ else:
467
+ config_text = text
468
+
469
+ api = ApolloApi()
470
+ result = api.update_namespace_config(
471
+ app_id=app_id,
472
+ namespace=namespace,
473
+ config_text=config_text,
474
+ env=env,
475
+ cluster=cluster,
476
+ namespace_id=namespace_id,
477
+ config_format=config_format,
478
+ )
479
+
480
+ if ctx.obj["output_format"] == "json":
481
+ click.echo(json.dumps(result, ensure_ascii=False, indent=2))
482
+ else:
483
+ click.echo(
484
+ f"Namespace 配置已更新: {namespace} "
485
+ f"(appId={app_id}, env={env}, cluster={cluster})"
486
+ )
487
+
488
+
489
+ @app.command("release-namespace")
490
+ @click.option("--app-id", "-a", required=True, help="应用 appId")
491
+ @click.option("--namespace", "-n", required=True, help="Namespace 名称(如 application.yml)")
492
+ @click.option("--env", default="DEV", show_default=True, help="环境")
493
+ @click.option("--cluster", default="default", show_default=True, help="集群")
494
+ @click.option(
495
+ "--title",
496
+ help="发布标题(默认自动生成,如 20260613144414-release)",
497
+ )
498
+ @click.option("--comment", default="", show_default=True, help="发布备注")
499
+ @click.option("--emergency", is_flag=True, help="是否紧急发布")
500
+ @click.pass_context
501
+ def release_namespace(ctx, app_id, namespace, env, cluster, title, comment, emergency):
502
+ """发布 Namespace 配置"""
503
+ release_title = title or f"{datetime.now():%Y%m%d%H%M%S}-release"
504
+
505
+ api = ApolloApi()
506
+ result = api.release_namespace(
507
+ app_id=app_id,
508
+ namespace=namespace,
509
+ release_title=release_title,
510
+ release_comment=comment,
511
+ is_emergency_publish=emergency,
512
+ env=env,
513
+ cluster=cluster,
514
+ )
515
+
516
+ if ctx.obj["output_format"] == "json":
517
+ click.echo(json.dumps(result, ensure_ascii=False, indent=2))
518
+ else:
519
+ click.echo(
520
+ f"Namespace 配置已发布: {namespace} "
521
+ f"(title={release_title}, appId={app_id}, env={env}, cluster={cluster})"
522
+ )
523
+
357
524
  def main():
358
525
  cli()
359
526
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zt-apollo-cli
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Apollo configuration management CLI
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -104,6 +104,56 @@ zt-apollo-cli app del-namespace-release-publisher -a your_app_id -n application
104
104
  zt-apollo-cli app update -a your_app_id --file ./app-update.json
105
105
  ```
106
106
 
107
+ ### 11) 修改 Namespace 配置
108
+
109
+ 适用于 yml/json 等文本类 Namespace。`namespaceId` 与 `format` 默认从 `list-namespace` 自动解析。
110
+
111
+ ```bash
112
+ # 从文件更新
113
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --file ./application.yml --env DEV --cluster default
114
+
115
+ # 直接传入配置文本
116
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --text "# config"
117
+ ```
118
+
119
+ ### 12) 发布 Namespace 配置
120
+
121
+ 将未发布的 Namespace 变更发布到指定环境。`--title` 未指定时自动生成(如 `20260613144414-release`)。
122
+
123
+ ```bash
124
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --env DEV --cluster default --comment "发布说明"
125
+
126
+ # 指定发布标题
127
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --title "20260613144414-release" --comment "s"
128
+ ```
129
+
130
+ ### 13) 创建 Namespace
131
+
132
+ 在应用下新建 Namespace。默认追加 appId 前缀(`appendNamespacePrefix=true`)。
133
+
134
+ ```bash
135
+ zt-apollo-cli app create-namespace -a your_app_id -n test --format properties --comment ""
136
+
137
+ # 不追加前缀
138
+ zt-apollo-cli app create-namespace -a your_app_id -n application.yml --format yml --no-append-prefix
139
+ ```
140
+
141
+ ### 14) 删除 Namespace(指定环境)
142
+
143
+ 删除指定 env/cluster 下的 Namespace。
144
+
145
+ ```bash
146
+ zt-apollo-cli app delete-namespace -a your_app_id -n test --env DEV --cluster default
147
+ ```
148
+
149
+ ### 15) 删除应用 Namespace(所有环境)
150
+
151
+ 删除应用下的 Namespace 定义及所有环境的配置。
152
+
153
+ ```bash
154
+ zt-apollo-cli app delete-app-namespace -a your_app_id -n test
155
+ ```
156
+
107
157
  ## 全局参数
108
158
  - `-o, --output`:输出格式,支持:
109
159
  - `json`(默认)
File without changes