1. Query the Huawei Cloud RDS (Relational Database Service) instance list of a tenant region through KooCLI (`hcloud RDS ListInstances`, the official read-only GET /v3/{project_id}/instances), with an automatic fallback to the official huaweicloudsdkrds v3 SDK (`RdsClient.list_instances`) when hcloud is not installed. 2. Export the instance name list (plus ID / status / engine / version / flavor / private IP / availability zone / created time) into a well-formed local Markdown table file — strictly local file generation, nothing is ever written back to the cloud. `--output` sets the file path (default `rds_instances.md` in the current directory); the parent directory is created automatically and the file is written via a temp file + atomic rename. 3. Region aware: `--region` (default cn-north-4); `--project_id` is mapped to the KooCLI global `--cli-project-id`. Optional `--name` server-side filter and `--status` client-side filter (the real ListInstances API has no status parameter, so the full list is fetched first and filtered locally). Optional `--sort-by name|created` orders the exported rows locally, after the filters: `name` compares instance names case-insensitively (id fallback), `created` compares creation timestamps oldest-first. The API has no ordering parameter, so sorting is always client-side; omitting `--sort-by` keeps the API order and the previous output unchanged. 4. Explicit error classification: empty instance list (still writes the file), missing AK/SK credentials (「缺少 AK/SK 凭证」), 403 / insufficient IAM permission (「无权限访问 RDS,请检查 IAM 权限」), hcloud not installed (falls back to the SDK, else a clear hint), KooCLI usage errors, and generic API/network failures. 5. Built-in write-intent guard: any mutating intent (create / delete / update / resize / restart / stop / restore ...) or any operation other than the read-only ListInstances is rejected before execution — this skill is strictly read-only. 6. Triggers include: "查询RDS生成md文件", "导出RDS实例清单为md", "RDS 实例名称 Markdown", "RDS list markdown", "RDS inventory markdown", "华为云RDS导出 markdown", "export RDS instances to markdown". 7. Offline unit tests (mocked hcloud subprocess) cover the guard, paging, filters, local sorting (`--sort-by name` / `created`), Markdown rendering, atomic file writing, error classification, SDK fallback and all output modes.
---
name: huawei-cloud-rds-export-markdown
description: |
1. Query the Huawei Cloud RDS (Relational Database Service) instance list of a
tenant region through KooCLI (`hcloud RDS ListInstances`, the official
read-only GET /v3/{project_id}/instances), with an automatic fallback to the
official huaweicloudsdkrds v3 SDK (`RdsClient.list_instances`) when hcloud is
not installed.
2. Export the instance name list (plus ID / status / engine / version / flavor /
private IP / availability zone / created time) into a well-formed local
Markdown table file — strictly local file generation, nothing is ever written
back to the cloud. `--output` sets the file path (default `rds_instances.md`
in the current directory); the parent directory is created automatically and
the file is written via a temp file + atomic rename.
3. Region aware: `--region` (default cn-north-4); `--project_id` is mapped to the
KooCLI global `--cli-project-id`. Optional `--name` server-side filter and
`--status` client-side filter (the real ListInstances API has no status
parameter, so the full list is fetched first and filtered locally).
Optional `--sort-by name|created` orders the exported rows locally, after the
filters: `name` compares instance names case-insensitively (id fallback),
`created` compares creation timestamps oldest-first. The API has no ordering
parameter, so sorting is always client-side; omitting `--sort-by` keeps the
API order and the previous output unchanged.
4. Explicit error classification: empty instance list (still writes the file),
missing AK/SK credentials (「缺少 AK/SK 凭证」), 403 / insufficient IAM
permission (「无权限访问 RDS,请检查 IAM 权限」), hcloud not installed (falls
back to the SDK, else a clear hint), KooCLI usage errors, and generic
API/network failures.
5. Built-in write-intent guard: any mutating intent (create / delete / update /
resize / restart / stop / restore ...) or any operation other than the
read-only ListInstances is rejected before execution — this skill is strictly
read-only.
6. Triggers include: "查询RDS生成md文件", "导出RDS实例清单为md", "RDS 实例名称
Markdown", "RDS list markdown", "RDS inventory markdown", "华为云RDS导出
markdown", "export RDS instances to markdown".
7. Offline unit tests (mocked hcloud subprocess) cover the guard, paging,
filters, local sorting (`--sort-by name` / `created`), Markdown rendering,
atomic file writing, error classification, SDK fallback and all output modes.
tags: [huawei-cloud, rds, database, readonly, markdown]
---
# Huawei Cloud RDS Instance List → Markdown Export
## Overview
Query the RDS instance list of a tenant region through KooCLI
`hcloud RDS ListInstances` (official read-only GET `/v3/{project_id}/instances`,
verified in the `huaweicloudsdkrds` v3 client `_list_instances_http_info`
metadata), then render the instance name list as a Markdown table and write it
to a local `.md` file. The skill is aimed at RDS inventory / asset export,
daily operations patrol, handover reports and any "save the instance list as a
Markdown file" request.
Architecture:
```text
agent → python3 scripts/list_rds_export_markdown.py → hcloud RDS ListInstances --cli-region=<region> --limit=N --offset=O
→ Huawei Cloud RDS OpenAPI (regional endpoint) → parsed JSON → Markdown table → local .md file
(hcloud binary missing) → fallback: huaweicloudsdkrds v3 RdsClient.list_instances
```
Key properties:
- **Read-only by design** — the only API ever invoked is `ListInstances` (GET).
A write-intent guard validates the operation name and scans the arguments;
mutating intents (create / delete / update / resize / restart / stop /
restore ...) are rejected with an explicit message before any command runs.
The `.md` file is generated on the **local filesystem only** — no RDS write
happens and no user confirmation can make this skill issue a write.
- **CLI first, SDK fallback** — KooCLI is the primary execution path; when the
hcloud binary is absent, the script falls back to the official
`huaweicloudsdkrds` v3 SDK (`list_instances`). Credentials are always read by
the CLI profile / SDK environment — never handled by the script.
- **Local sorting** — `--sort-by name|created` orders the rows in memory after
the full (paged) list has been fetched; the ListInstances API has no ordering
parameter. `name` compares case-insensitively with an id fallback, `created`
compares parsed timestamps oldest-first (ISO-8601, `Z`/`+0800` offsets and
space-separated values all accepted). Records whose sort key is missing are
always placed last, unparseable timestamps fall back to text comparison, and
the stable sort keeps ties in their original order. Omit `--sort-by` to keep
the API order unchanged.
- **Empty result is not an error** — an empty instance list (or a filter that
matches nothing) still produces a valid `.md` file with the table header and
an explicit "no instances" note, and exits `0`.
- **Atomic local file** — the Markdown body is written to a temporary file and
renamed into place, so an interrupted write never leaves a corrupt / partial
file behind.
- **Offline-testable** — every code path (paging, filters, rendering, atomic
write, error classification, guard, SDK fallback) is covered by unit tests
that mock the hcloud subprocess.
> **Capability boundary:** this skill lists **instances only** and exports them
> locally; it does not manage the instance lifecycle (create / delete / resize /
> restart / stop / restore backup / change password / read-replica management),
> and it does not export to Excel / PDF / CSV. Those require a separate skill.
> **Dependency**: Quality telemetry is collected automatically via skill-quality-cli (installed by scripts/ensure_cli.sh if absent).
## Differentiation from the plain RDS list skill
The repository keeps a terminal-oriented RDS **instance-list** skill and this
**Markdown-export** skill side by side. They share the same read-only API but
produce different artifacts, and they are **not** runtime dependencies of each
other.
| Dimension | Plain RDS instance-list skill | This export skill |
| --------- | ----------------------------- | ----------------- |
| Deliverable | Terminal output (table / JSON / names-only) | A local Markdown file (default `rds_instances.md`) |
| Persistence | None — printed to stdout only | Atomic file write + automatic parent-directory creation |
| Purpose | Interactive inspection / pre-query an `instance_id` for downstream use | Markdown report export for inventory, patrol, handover |
Per-service "query + export to a file" capabilities are an established
repository convention and are kept as their own skill, separate from the plain
list skill and named by the export format (a sibling compute service exports to
Excel, a sibling storage service exports to Excel/PDF). This skill continues
that convention for RDS with Markdown as the output format. Use the plain RDS
instance-list skill to view instances in the terminal; use this skill when the
instance list must be saved as a distributable / archivable Markdown document.
## Prerequisites
1. **Python 3.8+** — the Markdown export uses only the Python standard library.
For the hcloud-absent fallback, run `python3 scripts/ensure_env.py` to install
`huaweicloudsdkcore` / `huaweicloudsdkrds` automatically. See
[references/cli-installation-guide.md](references/cli-installation-guide.md).
2. **KooCLI (`hcloud`) installed** — the primary path. See
[references/cli-installation-guide.md](references/cli-installation-guide.md).
Alternative: `pip install huaweicloudsdkcore huaweicloudsdkrds` to enable the
SDK fallback path.
3. **KooCLI credentials configured out-of-band** — the script never reads,
echoes, or stores AK/SK itself; authentication is delegated to the active
KooCLI profile (CLI path) or the whitelisted environment variables (SDK
fallback). Never paste AK/SK into chat.
4. **IAM permission** — read-only `rds:instance:list` (or the system policy
**RDS ReadOnlyAccess**); least-privilege policy in
[references/iam-policies.md](references/iam-policies.md).
5. **Outbound HTTPS** access to `rds.<region>.myhuaweicloud.com`.
6. **Write access to the local output directory** (for the generated `.md` file).
- **skill-quality-cli** — ensured by bash scripts/ensure_cli.sh (idempotent, skips if present)
- Upgrade: run skill-quality-cli upgrade manually (no auto-upgrade)
- Disable telemetry report: set SKILL_QUALITY_REPORT=0
## Workflow
1. **(Optional) prepare the environment** — `python3 scripts/ensure_env.py`
installs `huaweicloudsdkcore` / `huaweicloudsdkrds` if they are missing
(needed only for the SDK fallback).
2. **Confirm the target region** (default `cn-north-4`); every RDS call is
region-scoped. Optionally pass `--project_id`.
3. **Confirm KooCLI readiness** (optional, read-only) — the credential check in
Core Commands.
4. **Run the export script** with the desired output path, optional filters and
an optional `--sort-by` ordering.
5. **Read the result** — the printed path is the generated Markdown report; open
that file to see the instance-name table.
6. **Handle the empty / error cases** per the Error Handling table below (an
empty result still writes the file; credential / permission failures print an
explicit hint and write nothing).
## Core Commands
> **⚠️ Mandatory: every hcloud command in this skill MUST be wrapped with skill-quality-cli run --skill-name huawei-cloud-rds-export-markdown -- ** — bare hcloud calls are strictly forbidden.**
```bash
# Default: query the region's RDS instances and write rds_instances.md in cwd
python3 scripts/list_rds_export_markdown.py --region cn-north-4
# Explicit output path (recommended) + echo the Markdown to stdout as well
python3 scripts/list_rds_export_markdown.py --region cn-north-4 \
--output /tmp/rds_instances_cn-north-4.md --stdout
# Name filter (server-side) and status filter (applied client-side)
python3 scripts/list_rds_export_markdown.py --name rds-demo --status ACTIVE \
--output /tmp/rds_active.md
# Sort by instance name (case-insensitive, id fallback) — applied client-side
python3 scripts/list_rds_export_markdown.py --region cn-north-4 \
--sort-by name --output /tmp/rds_sorted_by_name.md
# Sort by creation time, oldest first (client-side; missing/unparseable last)
python3 scripts/list_rds_export_markdown.py --region cn-north-4 \
--sort-by created --output /tmp/rds_sorted_by_created.md
# Filter first, then sort (status filtering runs before the ordering)
python3 scripts/list_rds_export_markdown.py --status ACTIVE --sort-by name \
--output /tmp/rds_active_sorted.md
# Project-scoped (mapped to the KooCLI global --cli-project-id)
python3 scripts/list_rds_export_markdown.py --project_id <project_id> \
--output /tmp/rds_project.md
# Read-only credential readiness check (wrapped)
skill-quality-cli run --skill-name huawei-cloud-rds-export-markdown -- hcloud configure list
```
Equivalent direct KooCLI command (the script wraps exactly this call, with
paging; wrap with `skill-quality-cli run` in production):
```bash
skill-quality-cli run --skill-name huawei-cloud-rds-export-markdown -- hcloud RDS ListInstances --cli-region=cn-north-4 --limit=100 --offset=0
```
## Parameter Confirmation
| Parameter | Required | Description | Example |
| ----------- | ---------- | ------------- | --------- |
| `--region` / `-r` | No | Huawei Cloud region (default `cn-north-4`) | `cn-south-1` |
| `--project_id` / `--project-id` | No | Project id; mapped to the KooCLI global `--cli-project-id` (the SDK path resolves it from credentials + region) | `<project_id>` |
| `--output` / `-o` | No | Markdown output file path (default current dir `rds_instances.md`) | `/tmp/rds.md` |
| `--name` | No | Filter by instance name (server-side ListInstances parameter) | `rds-demo` |
| `--status` | No | Filter by instance state — **applied client-side** (RDS ListInstances has no status API parameter; the full list is fetched, then filtered locally, case-insensitive) | `ACTIVE`, `FAILED`, `BUILDING` |
| `--sort-by` | No | Order the rows — **applied client-side** after the filters (the API has no ordering parameter): `name` = instance name ascending, case-insensitive, `id` fallback; `created` = creation time ascending (oldest first), missing/unparseable values last. Omitted → API order preserved | `name`, `created` |
| `--page-size` | No | Page size for ListInstances (default 100) | `100` |
| `--operation` | No | hcloud RDS operation name; only `ListInstances` is allowed | `ListInstances` |
| `--stdout` | No | Also print the rendered Markdown to stdout (the file is still written) | `--stdout` |
## Markdown Output Format
The generated document has a title, a short metadata line, and a table with the
following columns (fields missing on an instance render as `-`):
| Column (header) | Source key | Notes |
| ---------- | ---------- | --------- |
| 实例名称 | `name` | Instance name (falls back to ID) |
| 实例ID | `id` | Instance ID |
| 状态 | `status` | `ACTIVE` / `FAILED` / `BUILDING` ... |
| 引擎 | `datastore.type` | Flattened engine type |
| 版本 | `datastore.version` | Flattened engine version |
| 规格 | `flavor_ref` | Flavor reference |
| 私网IP | `private_ips` | Comma-joined; `-` when absent |
| 可用区 | `nodes[0].availability_zone` | Primary node AZ |
| 创建时间 | `created` | Creation timestamp |
An empty instance list produces the header + separator followed by a note line
`_当前区域(<x>)无 RDS 实例。_` — the file is always created.
## Write-Intent Guard
- The guard validates the `--operation` value (only the read-only
`ListInstances` is accepted) and scans the remaining arguments for mutating
vocabulary (`create`, `delete`, `update`, `modify`, `resize`, `restart`,
`stop`, `start`, `shutdown`, `restore`, `destroy`, and RDS operation names such
as `CreateInstance`, `DeleteInstance`, `ResizeFlavor`).
- Values of read-only flags (the local `--output` path, `--region`,
`--project_id`, `--name`, `--status`, `--sort-by`, ...) are excluded from the
scan so a file path, a filter value or a sort option is never mistaken for an
RDS operation. `--sort-by` is in that whitelist on purpose: its value
`created` contains the mutating keyword `create`, and both `--sort-by created`
and `--sort-by=created` must pass the guard.
- Any hit raises `GuardError`, prints `[错误] 检测到写操作意图 ... 已被守卫拦截`,
and exits with code 1 **before** a single hcloud command is executed. No write
operation can be constructed by `build_command()`; there is no code path that
can issue a mutating RDS API call, so no user confirmation path exists — the
`Create` / `Delete` / `Update` intents are refused outright.
## KooCLI Command Format Standard
```text
hcloud <Service> <Operation> --cli-region=<region> [--key=value ...]
```
- **Service name** — exact KooCLI title-case service name: `RDS`.
- **Operation name** — PascalCase: `ListInstances`.
- **Region parameter** — `--cli-region=<value>`, e.g. `--cli-region=cn-north-4`
(required for every concrete RDS call).
- **Project parameter** — `--cli-project-id=<value>` (global KooCLI option,
emitted only when `--project_id` is supplied).
- **Simple parameter** — `--key=value`, e.g. `--limit=100`, `--offset=20`,
`--name=rds-01`, `--datastore_type=MySQL`, `--id=rds-01`. There is **no
`--status` parameter** on the real RDS ListInstances API (verified via
`hcloud RDS ListInstances --help`); status filtering is done client-side by
the script. There is likewise **no ordering parameter** — `--sort-by` is a
script-side option only and is never sent to hcloud / the SDK.
- **REST path (official SDK `_http_info`)** —
`GET /v3/{project_id}/instances` (`huaweicloudsdkrds` v3
`_list_instances_http_info`); project_id is resolved by KooCLI / the SDK from
the credential profile.
Concrete raw KooCLI command (the script wraps exactly this call; wrap with
`skill-quality-cli run --skill-name huawei-cloud-rds-export-markdown --` in
production):
| Command | Purpose |
| ------- | ------- |
| `hcloud RDS ListInstances --cli-region=cn-north-4 --limit=1` | Read-only instance list used as the Markdown source |
## Error Handling
| Scenario | Behavior |
| ---------- | --------- |
| Empty instance list / no filter match | Exit 0; the `.md` file is still written (header + `无 RDS 实例` note) |
| Missing / invalid AK-SK | Exit 1, `缺少 AK/SK 凭证` hint to configure hcloud credentials out-of-band (never in chat); no file written |
| 403 / insufficient IAM permission | Exit 1, `无权限访问 RDS,请检查 IAM 权限` hint; no file written |
| hcloud not installed | Falls back to the SDK; if the SDK is unavailable too, exit 1 with a clear hint |
| Bad `--output` path (permission / bad directory) | Exit 1, `Markdown 文件写出失败` hint; no partial file left |
| KooCLI usage error (`[USE_ERROR] ...`, even with exit code 0) | Exit 1, explicit `hcloud 参数错误` hint pointing to `hcloud RDS ListInstances --help` |
| Unknown `--sort-by` value | Exit 1, `--sort-by 仅支持 name 或 created(收到: xxx)`; fails before any API call, no file written |
| Network / API failure | Exit 1 with the hcloud stderr excerpt |
| Write intent detected | Exit 1, `GuardError` message, no command executed, no file written |
Exit codes: `0` success / empty list; `1` config, auth, guard, export, or API
error.
## Edge Cases
| Scenario | Handling |
| ---------- | --------- |
| Region has no instances | Header + empty-table note written, exit 0 |
| Many instances | Automatic `offset` paging (total_count aware, full-page fallback, max 100 pages) |
| Filter yields no instance | Empty message includes the active filters; the file is still written |
| Field missing on an instance | Rendered as `-` in the Markdown cell |
| `|` or newline inside a field | Escaped (`\|`) / collapsed to a space so the table stays well-formed |
| `--output` points to a non-existing directory | Directory is created automatically |
| `--output` path contains a guard keyword (e.g. `downloads/update-*.md`) | Path value is excluded from the guard (local path ≠ RDS operation) |
| `--status BUILDING` | Legitimate read filter — the status value is not sent to the server |
| `--sort-by` omitted | Original API order preserved (no reordering, output unchanged) |
| `--sort-by name` with duplicate / mixed-case names | Case-insensitive compare, stable sort keeps ties in the original relative order; records without `name` fall back to `id` |
| `--sort-by created` with missing / unparseable timestamp | Parse failure falls back to text comparison; a missing `created` is always placed last instead of aborting the export |
| hcloud absent, SDK installed | Transparent fallback to `huaweicloudsdkrds` list_instances, same Markdown output |
| User asks to create/delete/restart | Out of scope — blocked by the write-intent guard |
## Testing
Offline unit tests with a mocked hcloud subprocess (no credentials, no network):
```bash
cd skills/database/rds/huawei-cloud-rds-export-markdown
python3 -m unittest discover -s tests -v
```
Coverage: write-intent guard (read passes / create / delete / mutating verbs /
normalized separators / operation whitelist / read-only value scrubbing including
`--sort-by created` and `--flag=value` forms), command construction
(`--cli-region` / limit / offset / name / `--cli-project-id`, no write flags / no
status / no ordering), JSON extraction and instance extraction, failure
classification (missing AK/SK, 403, generic, `[USE_ERROR]` with rc=0), offset
paging (total / full page / empty), client-side status filtering, local sorting
(`--sort-by name` case-insensitive + id fallback + missing last, `--sort-by
created` mixed timestamp formats + text fallback + missing last + stable ties,
no-argument order preservation, unknown value), SDK fallback (env credentials /
missing credentials / unavailable), normalization, Markdown rendering (title /
header / rows / empty-result note / pipe & newline escaping), atomic file writing
(missing dirs / bad path), and all `main()` output modes (including the sorted
row order and the fail-fast exit 1 for an unknown `--sort-by`).
## Reference Documents
| Document | Content |
| ---------- | --------- |
| [references/iam-policies.md](references/iam-policies.md) | Least-privilege read-only IAM policy |
| [references/cli-installation-guide.md](references/cli-installation-guide.md) | KooCLI + SDK installation, credential configuration and skill-quality-cli |
| [references/verification-method.md](references/verification-method.md) | How the skill and its tests were verified |
| [references/dataflow-diagram.md](references/dataflow-diagram.md) | Mermaid data flow diagram |
| [references/acceptance-criteria.md](references/acceptance-criteria.md) | Acceptance criteria checklist |don't have the plugin yet? install it then click "run inline in claude" again.
extracted implicit decision logic into explicit if-else branches (guard, hcloud availability, error classification, filters, sorting, file write), formalized procedure steps with discrete inputs/outputs, documented all external connections and env-var dependencies, clarified edge cases and fallback paths, and added outcome signals for both success and failure.
export a huawei cloud rds instance inventory to a local markdown file. query the rds instance list from a specified region (default cn-north-4) via koolcli hcloud RDS ListInstances with automatic sdk fallback, then render instances into a well-formed markdown table with optional server-side name filtering, client-side status filtering, and client-side sorting by name or creation time. the skill writes only to the local filesystem and never modifies cloud resources. use this when you need a distributable markdown report of rds instances for inventory, patrol logs, or handover documentation.
environment & tools:
hcloud binary) installed and in $PATH (primary execution path)huaweicloudsdkcore and huaweicloudsdkrds v3 SDKs (install via python3 scripts/ensure_env.py)bash scripts/ensure_cli.sh, idempotent)credentials & permissions:
rds:instance:list or system policy RDS ReadOnlyAccess (least-privilege in references/iam-policies.md)rds.<region>.myhuaweicloud.comparameters:
--region / -r: target region (default cn-north-4, e.g. cn-south-1)--project_id / --project-id: project id (mapped to koolcli global --cli-project-id; sdk resolves from credentials + region if omitted)--output / -o: local markdown file path (default rds_instances.md in cwd; parent dirs auto-created)--name: server-side instance name filter (exact or substring match depends on api behavior)--status: client-side instance status filter, case-insensitive (e.g. ACTIVE, FAILED, BUILDING; api has no status parameter, so full list fetched then filtered locally)--sort-by: client-side row ordering after filters: name (case-insensitive, id fallback) or created (oldest first, missing/unparseable last); omit to preserve api order--page-size: items per hcloud call (default 100, max ~100 before pagination loop)--operation: hcloud rds operation name (only ListInstances allowed; guard rejects mutations)--stdout: also print rendered markdown to stdout (file still written)external connections:
GET /v3/{project_id}/instances (verified in huaweicloudsdkrds v3 _list_instances_http_info)HUAWEICLOUD_SDK_AK, HUAWEICLOUD_SDK_SK), or implicit mfaSKILL_QUALITY_REPORT=0)validate write-intent guard: scan --operation and remaining args for mutating keywords (create, delete, update, modify, resize, restart, stop, start, shutdown, restore, destroy, CreateInstance, DeleteInstance, ResizeFlavor, etc.). exclude read-only flag values (--output path, --region, --project_id, --name, --status, --sort-by values). if mutation detected, raise GuardError, print [错误] 检测到写操作意图 ... 已被守卫拦截, exit 1. proceed only if guard passes.
validate --sort-by parameter: if --sort-by supplied, confirm it is name or created. if unknown, print --sort-by 仅支持 name 或 created(收到: xxx), exit 1 before any api call.
--sort-by value (or None)attempt koolcli path: check if hcloud binary exists in $PATH. if yes, proceed to step 4. if no, skip to step 6 (sdk fallback).
$PATH env varbuild & execute koolcli command: construct hcloud RDS ListInstances --cli-region=<region> [--cli-project-id=<project_id>] [--name=<name>] --limit=<page_size> --offset=<offset> (no --status or --sort-by sent to api). wrap with skill-quality-cli run --skill-name huawei-cloud-rds-export-markdown --. execute and parse json response.
instances (list), total_count (int); on error, capture stderr/rc and proceed to step 5 (error handling)classify & handle api errors:
缺少 AK/SK 凭证 in stderr or rc=1 + auth-related message: print hint, exit 1, do not write file无权限 in stderr: print 无权限访问 RDS,请检查 IAM 权限, exit 1, do not write file[USE_ERROR] in stderr (koolcli usage error, even with rc=0): print hcloud 参数错误, point to hcloud RDS ListInstances --help, exit 1, do not write fileinstances and total_count) or exit 1 on failuresdk fallback (if hcloud absent): if step 3 found hcloud missing, use huaweicloudsdkrds.RdsClient.list_instances(). read credentials from env vars (HUAWEICLOUD_SDK_AK, HUAWEICLOUD_SDK_SK) or implicit profile. handle same errors as step 5. convert sdk response to same json dict structure.
paginate through results: initialize offset=0. loop: call api with current offset. append returned instances to accumulator. if len(returned) < page_size or offset + page_size >= total_count, break. else increment offset, repeat. safety limit: max 100 pages (fail-safe against infinite loops on api bugs). flatten all instances into single list.
apply client-side status filter: if --status supplied, filter instances to those where status field (case-insensitive) matches the filter value. skip instances with missing status field (treat as non-match).
--status value (or None)apply client-side sort: if --sort-by name, sort instances by name field (case-insensitive string compare); instances without name fall back to id field for comparison; instances with neither are placed last. if --sort-by created, sort by parsed created timestamp (iso-8601 format with Z or +0800 offset; space-separated also accepted); instances with missing/unparseable created are placed last (unparseable falls back to text compare). use stable sort to preserve original order on ties. if --sort-by omitted, skip this step and keep api order.
--sort-by value (or None)render markdown table: construct markdown string with:
# RDS 实例清单 (<region>)生成时间: <ISO8601>, 实例数: <count>, 过滤条件: <active_filters or '无'>| 实例名称 | 实例ID | 状态 | 引擎 | 版本 | 规格 | 私网IP | 可用区 | 创建时间 || --- | --- | --- | --- | --- | --- | --- | --- | --- |name (fallback to id), id, status, datastore.type, datastore.version, flavor_ref, private_ips (comma-joined), nodes[0].availability_zone, created. missing fields render as -. escape | and newlines in cell values so table stays well-formed._当前区域(<region>)无 RDS 实例。_write markdown to file (atomic): if --output omitted, use default rds_instances.md in cwd. create parent directory if missing. write markdown string to temp file (e.g. .tmp.<uuid>) in target directory. fsync temp file. rename temp to target atomically (platform-dependent: posix rename on linux, windows atomic replace on windows). if write fails, print Markdown 文件写出失败, exit 1 (no partial file left). on success, record final path.
--output path (or default)output result: print to stdout: 已生成 RDS 实例 Markdown 清单: <absolute_path>. if --stdout flag supplied, also print the markdown string itself to stdout (after the path line). exit 0.
--stdout flagwrite-intent guard check (step 1):
hcloud availability (step 3):
$PATH: execute step 4 (koolcli command)api error classification (step 5):
[USE_ERROR]): exit 1, no file writtenempty instance list after pagination and filters (after step 9):
--sort-by parameter (step 2 and step 9):
--sort-by supplied and not name or created: exit 1 before any api call, fail-fast--sort-by name: sort case-insensitive by name, fallback to id--sort-by created: sort by parsed timestamp (oldest first), unparseable/missing last--sort-by omitted: preserve api order, no reordering--status filter (step 8):
--status supplied: apply case-insensitive client-side match; api returns full list, we filter locally--stdout flag (step 12):
--stdout present: print markdown to stdout in addition to filesuccess (exit 0):
--output path (default rds_instances.md in cwd)| and newlines已生成 RDS 实例 Markdown 清单: <absolute_path> followed by optional markdown body if --stdout suppliedfailure (exit 1):
缺少 AK/SK 凭证, 无权限访问 RDS,请检查 IAM 权限, --sort-by 仅支持 name 或 created(收到: xxx), hcloud stderr excerpt, Markdown 文件写出失败)file contents (success, non-empty list):
# RDS 实例清单 (<region>)
生成时间: 2024-01-15T10:30:45Z, 实例数: 3, 过滤条件: 无
| 实例名称 | 实例ID | 状态 | 引擎 | 版本 | 规格 | 私网IP | 可用区 | 创建时间 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| rds-demo | <id> | ACTIVE | MySQL | 8.0 | rds.mysql.c3.large.2 | 10.0.1.5 | az01 | 2023-12-01T08:15:30Z |
| rds-backup | <id> | ACTIVE | PostgreSQL | 13 | rds.pg.c2.medium | 10.0.1.6 | az02 | 2023-11-20T14:22:10Z |
| rds-new | <id> | BUILDING | MySQL | 5.7 | rds.mysql.c3.medium | - | az01 | 2024-01-10T09:00:00Z |
file contents (success, empty list):
# RDS 实例清单 (cn-north-4)
生成时间: 2024-01-15T10:30:45Z, 实例数: 0, 过滤条件: 无
| 实例名称 | 实例ID | 状态 | 引擎 | 版本 | 规格 | 私网IP | 可用区 | 创建时间 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
_当前区域(cn-north-4)无 RDS 实例。_
user knows it worked when:
已生成 RDS 实例 Markdown 清单: /path/to/rds_instances.md to stdout--stdout was supplied, the same markdown table appears on stdout immediately after the path messageuser knows it failed when:
缺少 AK/SK 凭证, 无权限访问 RDS,请检查 IAM 权限, hcloud stderr, Markdown 文件写出失败, --sort-by 仅支持 name 或 created, guard rejection message, etc.)