|
| 1 | +import { |
| 2 | + defineCommand, |
| 3 | + buildCanonicalQuery, |
| 4 | + signRequest, |
| 5 | + modelStudioHost, |
| 6 | + detectOutputFormat, |
| 7 | + maskToken, |
| 8 | + trackingHeaders, |
| 9 | + type Config, |
| 10 | + type GlobalFlags, |
| 11 | + type GetSubscriptionSeatDetailsResponse, |
| 12 | + type TokenPlanSeatDetail, |
| 13 | + BailianError, |
| 14 | + ExitCode, |
| 15 | +} from "bailian-cli-core"; |
| 16 | +import { emitResult, emitBare } from "../../output/output.ts"; |
| 17 | +import { padEnd } from "../../output/cjk-width.ts"; |
| 18 | + |
| 19 | +const API_VERSION = "2026-02-10"; |
| 20 | +const API_ACTION = "GetSubscriptionSeatDetails"; |
| 21 | +const API_PATH = "/tokenplan/subscription/seat-detail"; |
| 22 | + |
| 23 | +export default defineCommand({ |
| 24 | + name: "tokenplan seats", |
| 25 | + description: "List Token Plan subscription seat details", |
| 26 | + usage: "bl tokenplan seats [flags]", |
| 27 | + options: [ |
| 28 | + { flag: "--page-no <n>", description: "Page number (default: 1)", type: "number" }, |
| 29 | + { flag: "--page-size <n>", description: "Page size (default: 10)", type: "number" }, |
| 30 | + { |
| 31 | + flag: "--caller-uac-account-id <id>", |
| 32 | + description: "Caller UAC account ID", |
| 33 | + }, |
| 34 | + { |
| 35 | + flag: "--namespace-id <id>", |
| 36 | + description: "Product namespace ID (Token Plan default: namespace-1)", |
| 37 | + }, |
| 38 | + { |
| 39 | + flag: "--status <status>", |
| 40 | + description: |
| 41 | + "Seat status filter (repeatable): CREATING, NORMAL, LIMIT, RELEASE, STOP, REFUNDED", |
| 42 | + type: "array", |
| 43 | + }, |
| 44 | + { |
| 45 | + flag: "--status-list-str <json>", |
| 46 | + description: "StatusList as JSON string, e.g. '[\"NORMAL\"]'", |
| 47 | + }, |
| 48 | + { flag: "--seat-id <id>", description: "Filter by seat ID" }, |
| 49 | + { |
| 50 | + flag: "--seat-type <type>", |
| 51 | + description: "Seat tier: standard, pro, or max", |
| 52 | + }, |
| 53 | + { |
| 54 | + flag: "--query-assigned <bool>", |
| 55 | + description: "Filter by assignment: true=assigned, false=unassigned", |
| 56 | + }, |
| 57 | + { flag: "--access-key-id <key>", description: "Alibaba Cloud Access Key ID (deprecated)" }, |
| 58 | + { |
| 59 | + flag: "--access-key-secret <key>", |
| 60 | + description: "Alibaba Cloud Access Key Secret (deprecated)", |
| 61 | + }, |
| 62 | + ], |
| 63 | + examples: [ |
| 64 | + "bl tokenplan seats", |
| 65 | + "bl tokenplan seats --page-size 20 --status NORMAL", |
| 66 | + "bl tokenplan seats --query-assigned true --seat-type standard", |
| 67 | + ], |
| 68 | + async run(config: Config, flags: GlobalFlags) { |
| 69 | + const format = detectOutputFormat(config.output); |
| 70 | + const accessKeyId = (flags.accessKeyId as string) || config.accessKeyId; |
| 71 | + const accessKeySecret = (flags.accessKeySecret as string) || config.accessKeySecret; |
| 72 | + |
| 73 | + if (!accessKeyId || !accessKeySecret) { |
| 74 | + throw new BailianError( |
| 75 | + "No credentials found.\n" + |
| 76 | + "Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.", |
| 77 | + ExitCode.AUTH, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + const queryParams = buildQueryParams(flags); |
| 82 | + const queryString = buildCanonicalQuery(queryParams); |
| 83 | + const host = modelStudioHost(config.region); |
| 84 | + const endpoint = `https://${host}${API_PATH}${queryString ? `?${queryString}` : ""}`; |
| 85 | + |
| 86 | + if (config.dryRun) { |
| 87 | + emitResult({ endpoint, query: queryParams }, format); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const headers = signRequest({ |
| 92 | + accessKeyId, |
| 93 | + accessKeySecret, |
| 94 | + action: API_ACTION, |
| 95 | + version: API_VERSION, |
| 96 | + body: "", |
| 97 | + host, |
| 98 | + pathname: API_PATH, |
| 99 | + method: "GET", |
| 100 | + queryString, |
| 101 | + }); |
| 102 | + |
| 103 | + if (config.verbose) { |
| 104 | + process.stderr.write(`> GET ${endpoint}\n`); |
| 105 | + process.stderr.write(`> AK: ${maskToken(accessKeyId)}\n`); |
| 106 | + } |
| 107 | + |
| 108 | + const timeoutMs = config.timeout * 1000; |
| 109 | + const res = await fetch(endpoint, { |
| 110 | + method: "GET", |
| 111 | + headers: { ...headers, ...trackingHeaders() }, |
| 112 | + signal: AbortSignal.timeout(timeoutMs), |
| 113 | + }); |
| 114 | + |
| 115 | + if (config.verbose) { |
| 116 | + process.stderr.write(`< ${res.status} ${res.statusText}\n`); |
| 117 | + } |
| 118 | + |
| 119 | + const data = (await res.json()) as GetSubscriptionSeatDetailsResponse; |
| 120 | + |
| 121 | + if (!res.ok || data.Success === false) { |
| 122 | + throw new BailianError( |
| 123 | + `${data.Code || res.status} - ${data.Message || res.statusText}`, |
| 124 | + ExitCode.GENERAL, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + const items = data.Data?.Items ?? []; |
| 129 | + if (config.quiet || format === "text") { |
| 130 | + emitTextSeats(items, data.Data?.Total, data.Data?.PageNo, data.Data?.PageSize); |
| 131 | + } else { |
| 132 | + emitResult(data, format); |
| 133 | + } |
| 134 | + }, |
| 135 | +}); |
| 136 | + |
| 137 | +function buildQueryParams(flags: GlobalFlags): Record<string, string | string[] | undefined> { |
| 138 | + const params: Record<string, string | string[] | undefined> = {}; |
| 139 | + |
| 140 | + if (flags.pageNo !== undefined) params.PageNo = String(flags.pageNo as number); |
| 141 | + if (flags.pageSize !== undefined) params.PageSize = String(flags.pageSize as number); |
| 142 | + if (flags.callerUacAccountId) params.CallerUacAccountId = flags.callerUacAccountId as string; |
| 143 | + if (flags.namespaceId) params.NamespaceId = flags.namespaceId as string; |
| 144 | + if (flags.statusListStr) params.StatusListStr = flags.statusListStr as string; |
| 145 | + |
| 146 | + const status = flags.status; |
| 147 | + if (Array.isArray(status) && status.length > 0) { |
| 148 | + params.StatusList = status as string[]; |
| 149 | + } else if (typeof status === "string" && status.length > 0) { |
| 150 | + params.StatusList = [status]; |
| 151 | + } |
| 152 | + |
| 153 | + if (flags.seatId) params.SeatId = flags.seatId as string; |
| 154 | + if (flags.seatType) params.SeatType = flags.seatType as string; |
| 155 | + |
| 156 | + if (typeof flags.queryAssigned === "string" && flags.queryAssigned.length > 0) { |
| 157 | + params.QueryAssigned = flags.queryAssigned; |
| 158 | + } |
| 159 | + |
| 160 | + return params; |
| 161 | +} |
| 162 | + |
| 163 | +function emitTextSeats( |
| 164 | + items: TokenPlanSeatDetail[], |
| 165 | + total?: number, |
| 166 | + pageNo?: number, |
| 167 | + pageSize?: number, |
| 168 | +): void { |
| 169 | + if (items.length === 0) { |
| 170 | + emitBare("No seats found."); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const header = [ |
| 175 | + padEnd("SeatId", 18), |
| 176 | + padEnd("Type", 10), |
| 177 | + padEnd("Status", 10), |
| 178 | + padEnd("Assigned", 12), |
| 179 | + padEnd("Account", 20), |
| 180 | + ].join(" "); |
| 181 | + emitBare(header); |
| 182 | + emitBare("-".repeat(header.length)); |
| 183 | + |
| 184 | + for (const item of items) { |
| 185 | + const row = [ |
| 186 | + padEnd(item.SeatId ?? "-", 18), |
| 187 | + padEnd(item.SpecType ?? "-", 10), |
| 188 | + padEnd(item.Status ?? "-", 10), |
| 189 | + padEnd(item.AssignedStatus ?? "-", 12), |
| 190 | + padEnd(item.AccountName ?? item.AccountId ?? "-", 20), |
| 191 | + ].join(" "); |
| 192 | + emitBare(row); |
| 193 | + } |
| 194 | + |
| 195 | + if (total !== undefined) { |
| 196 | + emitBare(""); |
| 197 | + emitBare( |
| 198 | + `Total: ${total}${pageNo !== undefined ? ` | Page: ${pageNo}` : ""}${pageSize !== undefined ? ` | PageSize: ${pageSize}` : ""}`, |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
0 commit comments