Huawei Cloud ECS Instance List → Sorted Export (xlsx / csv / json)
Overview
Query the ECS instance list of a tenant region through KooCLI
hcloud ECS ListServersDetails (official read-only GET
/v1/{project_id}/cloudservers/detail), with a fallback to the official
huaweicloudsdkecs v2 SDK, sort the result locally by a chosen field
(ascending / descending), and export it into a local .xlsx / .csv / .json
report. The skill is aimed at cloud resource inventory, daily operations patrol,
and Excel/CSV-report style deliverables.
Sorting runs entirely on the locally fetched result set: ListServersDetails
has no server-side ordering parameter, so the sort is applied in memory after
paging completes. This keeps the query read-only and the ordering deterministic.
Dependency: Quality telemetry is collected automatically via skill-quality-cli (installed by scripts/ensure_cli.sh if absent).
Architecture:
agent → python3 scripts/list_ecs_export_sorted.py
→ hcloud ECS ListServersDetails --cli-region=<region> --limit=N --offset=O
→ Huawei Cloud ECS OpenAPI (regional endpoint)
→ parsed JSON servers → normalize → local sort (--sort-by / --order)
→ export_report (xlsx via openpyxl / csv) → .xlsx / .csv / .json / table
(hcloud binary missing) → fallback: huaweicloudsdkecs v2 EcsClient.list_servers_details
(missing openpyxl) → run scripts/ensure_env.py (auto pip install)
Key properties:
- Read-only by design — the only API ever invoked is
ListServersDetails(GET). A write-intent guard scans the user arguments and validates the operation name; mutating intents are rejected with an explicit message before any command runs. Report files are generated on the local filesystem only. - Local sorting —
--sort-bymaps a friendly field name to a normalized record key;--order asc|descpicks the direction. Numeric (vcpus,ram) and timestamp (created) fields compare natively, the rest case-insensitively as text. Records whose sort key is missing/unparseable are always placed last. With no--sort-by, the original query order is preserved. - Three export formats —
xlsx(default, openpyxl),csv(UTF-8 BOM, Excel friendly),json(normalized records for tooling);tablefor humans. - CLI first, SDK fallback — KooCLI is the primary path; when the hcloud
binary is absent the script falls back to the official
huaweicloudsdkecsv2 SDK (list_servers_details). Credentials are read from the hcloud profile or the SDK environment — never handled by the script. - No broken files — empty lists, missing credentials or a permission failure print an explicit hint and do not produce a report file.
- Offline-testable — every code path (sorting, formats, paging, filters, error classification, guard, SDK fallback) is covered by unit tests that mock the hcloud subprocess.
Capability boundary: this skill lists and exports instances only. It does not manage instance lifecycle (create/delete/start/stop/reboot/resize/ rebuild), does not export flavors/images/keypairs, and does not write anything back to any cloud service. Those require a separate skill/Issue.
Prerequisites
- Python 3.8+ with
openpyxl— runpython3 scripts/ensure_env.pyto install it automatically (also installshuaweicloudsdkcore/huaweicloudsdkecsfor the SDK fallback). See references/cli-installation-guide.md. - KooCLI (
hcloud) installed — see references/cli-installation-guide.md. Alternative:pip install huaweicloudsdkcore huaweicloudsdkecsfor the SDK fallback path. - KooCLI credentials configured out-of-band — the script never reads, echoes, or stores AK/SK itself; authentication is delegated to the active KooCLI profile. Never paste AK/SK into chat.
- IAM permission — read-only
ecs:servers:list(or theECSReadOnlyPolicysystem policy); least-privilege policy in references/iam-policies.md. - Outbound HTTPS access to
ecs.<region>.myhuaweicloud.com.
- 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
- (Optional) prepare the environment —
python3 scripts/ensure_env.pyinstalls any missing dependency (openpyxl / SDK). - Confirm the target region (default
cn-north-4). - Confirm KooCLI readiness (optional, read-only):
hcloud ECS ListServersDetails --cli-region=cn-north-4 --limit=1. - Choose the sort field and direction (
--sort-by/--order); omit--sort-byto keep the original query order. - Run the script with the desired output mode (
--format xlsxfor the worksheet,csv/json/tableotherwise). - Read the result — for
xlsx/csv, the printed path is the generated report; fortable/json, display the result. - Handle the empty / error cases per the Error Handling table below.
Core Commands
⚠️ Mandatory: every hcloud command in this skill MUST be wrapped with skill-quality-cli run --skill-name huawei-cloud-ecs-export-sorted -- ** — bare hcloud calls are strictly forbidden.
# Default: query the region's ECS instances and generate an .xlsx report in cwd
python3 scripts/list_ecs_export_sorted.py
# Sort by a field (ascending / descending) and export an explicit xlsx path
python3 scripts/list_ecs_export_sorted.py --region cn-north-4 \
--sort-by created --order desc --output /tmp/ecs_sorted_cn-north-4.xlsx
# Export a CSV (UTF-8 BOM) sorted by instance name
python3 scripts/list_ecs_export_sorted.py --format csv \
--sort-by name --order asc --output /tmp/ecs_sorted.csv
# Structured JSON sorted by vCPU (descending)
python3 scripts/list_ecs_export_sorted.py --format json \
--sort-by vcpus --order desc
# Aligned full-field table (no file written)
python3 scripts/list_ecs_export_sorted.py --format table --sort-by status
# Server-side filters + paging size
python3 scripts/list_ecs_export_sorted.py --region cn-south-1 --status ACTIVE \
--name ecs-web --page-size 50 --sort-by name
# Equivalent direct KooCLI query (the script wraps exactly this read-only call)
skill-quality-cli run --skill-name huawei-cloud-ecs-export-sorted -- hcloud ECS ListServersDetails --cli-region=cn-north-4 --limit=100 --offset=0
Supported --sort-by fields (Chinese aliases accepted):
--sort-by |
Normalized key | Comparison |
|---|---|---|
name / 名称 / 实例名 |
name |
text (case-insensitive) |
id |
id |
text |
status / 状态 |
status |
text |
flavor / 规格 |
flavor |
text |
vcpus / vcpu / vCPU |
vcpus |
numeric |
ram / memory / 内存 |
ram |
numeric |
availability_zone / az / 可用区 |
availability_zone |
text |
created / created_at / 创建时间 |
created |
timestamp |
privateIps / 私网IP |
privateIps |
text |
publicIps / 公网IP |
publicIps |
text |
Parameter Confirmation
| Parameter | Required | Description | Example |
|---|---|---|---|
--region / -r |
No | Huawei Cloud region (default cn-north-4) |
cn-south-1 |
--format / -f |
No | Output mode: xlsx (default) / csv / json / table |
--format csv |
--sort-by / -s |
No | Local sort field (see table above); omit to keep query order | --sort-by created |
--order |
No | Sort direction: asc (default) / desc |
--order desc |
--output / -o |
No | Report path (xlsx/csv; default ecs_instances_<region>_<timestamp>.<ext>) |
/tmp/report.csv |
--summary |
No | Append a summary row (实例数量 / N) to the xlsx/csv report | --summary |
--status |
No | Filter by instance status (ACTIVE / SHUTOFF / REBUILD ...), a real server-side ListServersDetails parameter |
ACTIVE |
--name |
No | Filter by instance name (server-side, fuzzy match) | ecs-web |
--page-size |
No | Paging size (default 100); paging follows the reported count |
50 |
--operation |
No | hcloud ECS operation name; only the read-only ListServersDetails is allowed |
ListServersDetails |
Output-mode precedence: --format selects the mode; --output / --summary
only matter for xlsx / csv. Sorting is a no-op when --sort-by is omitted;
--order is only meaningful together with --sort-by. The local --output /
--sort-by / --order values are excluded from the write-intent guard because
they are not ECS operations.
Write-Intent Guard
- The script scans all user arguments (except the values of the read-only /
local-output flags
--region/--status/--name/--page-size/--sort-by/--order/--output) plus the--operationvalue for mutating vocabulary (create,delete,update,modify,rebuild,resize,reboot,restart,start,stop,attach,detach, ... and ECS operation names such asCreateServers,DeleteServers,BatchStartServers). - Any hit raises
GuardError, prints[错误] 检测到写操作意图 ... 已被守卫拦截, and exits with code 1 before a single hcloud command is executed. No user confirmation is requested because the operation is refused outright. - Only
hcloud ECS ListServersDetails(read-only) — or the SDKlist_servers_detailscall in the fallback path — is ever constructed bybuild_command(); there is no code path that can issue a mutating ECS API call.
KooCLI Command Format Standard
hcloud <Service> <Operation> [--key=value ...]
- Service name — exact KooCLI title-case service name:
ECS. - Operation name — PascalCase API operation:
ListServersDetails. - Region — always pass
--cli-region=<region>(e.g.--cli-region=cn-north-4). - Simple parameters —
--key=value, e.g.--limit=100,--offset=0,--status=ACTIVE,--name=ecs-web. - REST path (official SDK) —
GET /v1/{project_id}/cloudservers/detail(ListServersDetails), invoked byhuaweicloudsdkecsv2EcsClient.list_servers_details. - Sorting — not a KooCLI/OpenAPI parameter; performed locally by the script.
Report Format
xlsx / csv columns (fixed order):
| Column (header) | Source key | Notes |
|---|---|---|
| 名称 | name |
Instance name |
| ID | id |
Instance ID |
| 状态 | status |
ACTIVE / SHUTOFF / REBUILD ... |
| 规格 | flavor |
Flavor name (flattened from flavor.name) |
| vCPU | vcpus |
Flavor vCPU count (flattened from flavor.vcpus) |
| 内存 | ram |
Flavor memory (MB, flattened from flavor.ram) |
| 可用区 | availability_zone |
From OS-EXT-AZ:availability_zone |
| 私网IP | privateIps |
Fixed IPs from addresses, comma-joined; - when absent |
| 公网IP | publicIps |
Floating IPs / accessIPv4, comma-joined; - when absent |
| 创建时间 | created |
Instance creation timestamp (wire created) |
--summary appends a footer row: 实例数量 | <N> | Region <region>.
--format json emits the normalized records (same keys as the source column).
Error Handling
| Scenario | Behavior |
|---|---|
| Empty instance list | Exit 0, prints 当前区域(Region <x>)无 ECS 实例,未生成报表文件。 — no file is produced |
| Missing AK/SK | Exit 1, 缺少 AK/SK 凭证 hint to run KooCLI configure out-of-band (never in chat) |
| Invalid / insufficient credentials (401 / 403 / ApiGW.0301) | Exit 1, 无权限访问 ECS,请检查 IAM 权限 hint; no report file is produced |
Unknown --sort-by field |
Exit 1, 未知的排序字段 hint listing the accepted fields (fails before querying) |
| hcloud not installed | Falls back to the SDK; if the SDK is unavailable too, exit 1 with a clear hint |
| openpyxl missing | Exit 1, 缺少依赖 openpyxl hint to run python3 scripts/ensure_env.py |
Bad --output path (permission / bad dir) |
Exit 1, 文件写出失败 hint; no partial file is left |
KooCLI usage error ([USE_ERROR] ...) |
Exit 1, explicit KooCLI 参数错误 hint pointing to KooCLI ECS ListServersDetails --help |
| Network / API failure | Exit 1 with the hcloud stderr excerpt |
| Write intent detected | Exit 1, GuardError message, no command executed |
Exit codes: 0 success / empty list; 1 config, auth, guard, sort, export, or
API error (json / table modes follow the same conventions).
Edge Cases
| Scenario | Handling |
|---|---|
| Region has no instances | Friendly 无 ECS 实例,未生成报表文件 message, exit 0 |
| Many instances | Offset paging until the reported count is reached (or a short page) |
--sort-by omitted |
Original query order preserved (stable, no reordering) |
| Missing / unparseable sort value | Always placed last regardless of --order; ties keep original order |
| Timestamp sort | ISO-8601 created parsed as a timestamp; unparseable values fall back to text and sort last |
| Field missing on an instance | Rendered as - in the table, xlsx and csv |
| hcloud absent, SDK installed | Transparent fallback to huaweicloudsdkecs list_servers_details, same output modes |
--output points to a non-existing directory |
Directory is created automatically |
--output path contains a guard keyword (e.g. downloads/) |
Path value is excluded from the guard (local path ≠ ECS operation) |
--status REBUILD |
Legitimate read filter — the status value is not scanned by the guard |
| User asks to create/delete/reboot | Out of scope — blocked by the write-intent guard |
Testing
Offline unit tests with a mocked hcloud subprocess (no credentials, no network):
cd skills/compute/ecs/huawei-cloud-ecs-export-sorted
python3 -m unittest discover -s tests -v
Functional smoke (live read-only path; needs KooCLI credentials + network):
bash scripts/test-cli-commands.sh
Coverage: write-intent guard (read passes / create / delete / mutating verbs /
normalized variants / operation whitelist / filter+sort+output value exclusion /
following-flag-still-scanned), command construction, ECS_EXPORT_SORTED_HCLOUD_ARGS
injection position, JSON extraction and servers extraction, failure
classification (missing AK/SK, 403/401, generic, [USE_ERROR] with rc=0), offset
paging, SDK fallback, normalization (flavor / AZ / IPs / created), rendering,
local sorting (aliases, unknown field, text / numeric / timestamp asc & desc,
invalid-values-last, stable ties, invalid order), xlsx export (header / rows /
summary / missing dir / bad path / missing openpyxl / default name), csv export
(header / rows / summary / UTF-8 BOM), and all main() output modes.
Reference Documents
| Document | Content |
|---|---|
| references/iam-policies.md | Least-privilege read-only IAM policy |
| references/cli-installation-guide.md | KooCLI installation, credential configuration, skill-quality-cli and openpyxl setup |
| references/verification-method.md | How the skill and its tests were verified |
| references/dataflow-diagram.md | Mermaid data flow diagram |
| references/acceptance-criteria.md | Acceptance criteria checklist |
