|
| 1 | +import { |
| 2 | + defineCommand, |
| 3 | + buildCanonicalQuery, |
| 4 | + signRequest, |
| 5 | + modelStudioHost, |
| 6 | + detectOutputFormat, |
| 7 | + maskToken, |
| 8 | + trackingHeaders, |
| 9 | + type Config, |
| 10 | + type GlobalFlags, |
| 11 | + type BatchAssignSeatsResponse, |
| 12 | + BailianError, |
| 13 | + ExitCode, |
| 14 | +} from "bailian-cli-core"; |
| 15 | +import { emitResult, emitBare } from "../../output/output.ts"; |
| 16 | + |
| 17 | +const API_VERSION = "2026-02-10"; |
| 18 | +const API_ACTION = "BatchAssignSeats"; |
| 19 | +const API_PATH = "/tokenplan/subscription/seat-assignments"; |
| 20 | + |
| 21 | +export default defineCommand({ |
| 22 | + name: "tokenplan assign-seats", |
| 23 | + description: "Batch assign Token Plan seats to members", |
| 24 | + usage: |
| 25 | + "bl tokenplan assign-seats --workspace-id <id> --seat-type <type> --account-id <id> [flags]", |
| 26 | + options: [ |
| 27 | + { |
| 28 | + flag: "--workspace-id <id>", |
| 29 | + description: "Workspace ID (env: BAILIAN_WORKSPACE_ID, config: workspace_id)", |
| 30 | + }, |
| 31 | + { |
| 32 | + flag: "--seat-type <type>", |
| 33 | + description: "Seat tier: standard, pro, or max", |
| 34 | + required: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + flag: "--account-id <id>", |
| 38 | + description: "Target member account ID (repeatable)", |
| 39 | + type: "array", |
| 40 | + }, |
| 41 | + { |
| 42 | + flag: "--caller-uac-account-id <id>", |
| 43 | + description: "Caller UAC account ID", |
| 44 | + }, |
| 45 | + { |
| 46 | + flag: "--namespace-id <id>", |
| 47 | + description: "Product namespace ID (Token Plan default: namespace-1)", |
| 48 | + }, |
| 49 | + { |
| 50 | + flag: "--locale <locale>", |
| 51 | + description: "Language: zh-CN or en-US", |
| 52 | + }, |
| 53 | + { flag: "--access-key-id <key>", description: "Alibaba Cloud Access Key ID (deprecated)" }, |
| 54 | + { |
| 55 | + flag: "--access-key-secret <key>", |
| 56 | + description: "Alibaba Cloud Access Key Secret (deprecated)", |
| 57 | + }, |
| 58 | + ], |
| 59 | + examples: [ |
| 60 | + "bl tokenplan assign-seats --workspace-id ws_456 --seat-type standard --account-id acc_123", |
| 61 | + "bl tokenplan assign-seats --workspace-id ws_456 --seat-type pro --account-id acc_1 --account-id acc_2", |
| 62 | + ], |
| 63 | + async run(config: Config, flags: GlobalFlags) { |
| 64 | + const format = detectOutputFormat(config.output); |
| 65 | + const accessKeyId = (flags.accessKeyId as string) || config.accessKeyId; |
| 66 | + const accessKeySecret = (flags.accessKeySecret as string) || config.accessKeySecret; |
| 67 | + |
| 68 | + if (!accessKeyId || !accessKeySecret) { |
| 69 | + throw new BailianError( |
| 70 | + "No credentials found.\n" + |
| 71 | + "Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.", |
| 72 | + ExitCode.AUTH, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const workspaceId = (flags.workspaceId as string) || config.workspaceId; |
| 77 | + const seatType = flags.seatType as string | undefined; |
| 78 | + if (!workspaceId) { |
| 79 | + throw new BailianError( |
| 80 | + "Missing workspace ID.\n" + |
| 81 | + "Set via: --workspace-id flag, env: BAILIAN_WORKSPACE_ID, or config: bl config set workspace_id <id>", |
| 82 | + ExitCode.USAGE, |
| 83 | + ); |
| 84 | + } |
| 85 | + if (!seatType) { |
| 86 | + throw new BailianError("Missing required argument --seat-type.", ExitCode.USAGE); |
| 87 | + } |
| 88 | + |
| 89 | + const accountIds = flags.accountId; |
| 90 | + const hasAccountIds = |
| 91 | + (Array.isArray(accountIds) && accountIds.length > 0) || |
| 92 | + (typeof accountIds === "string" && accountIds.length > 0); |
| 93 | + if (!hasAccountIds) { |
| 94 | + throw new BailianError("Missing required argument --account-id.", ExitCode.USAGE); |
| 95 | + } |
| 96 | + |
| 97 | + const queryParams = buildQueryParams(flags, workspaceId); |
| 98 | + const queryString = buildCanonicalQuery(queryParams); |
| 99 | + const host = modelStudioHost(config.region); |
| 100 | + const endpoint = `https://${host}${API_PATH}${queryString ? `?${queryString}` : ""}`; |
| 101 | + |
| 102 | + if (config.dryRun) { |
| 103 | + emitResult({ endpoint, query: queryParams }, format); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const headers = signRequest({ |
| 108 | + accessKeyId, |
| 109 | + accessKeySecret, |
| 110 | + action: API_ACTION, |
| 111 | + version: API_VERSION, |
| 112 | + body: "", |
| 113 | + host, |
| 114 | + pathname: API_PATH, |
| 115 | + method: "POST", |
| 116 | + queryString, |
| 117 | + }); |
| 118 | + |
| 119 | + if (config.verbose) { |
| 120 | + process.stderr.write(`> POST ${endpoint}\n`); |
| 121 | + process.stderr.write(`> AK: ${maskToken(accessKeyId)}\n`); |
| 122 | + } |
| 123 | + |
| 124 | + const timeoutMs = config.timeout * 1000; |
| 125 | + const res = await fetch(endpoint, { |
| 126 | + method: "POST", |
| 127 | + headers: { ...headers, ...trackingHeaders() }, |
| 128 | + signal: AbortSignal.timeout(timeoutMs), |
| 129 | + }); |
| 130 | + |
| 131 | + if (config.verbose) { |
| 132 | + process.stderr.write(`< ${res.status} ${res.statusText}\n`); |
| 133 | + } |
| 134 | + |
| 135 | + const data = (await res.json()) as BatchAssignSeatsResponse; |
| 136 | + |
| 137 | + if (!res.ok || data.Success === false) { |
| 138 | + throw new BailianError( |
| 139 | + `${data.Code || res.status} - ${data.Message || res.statusText}`, |
| 140 | + ExitCode.GENERAL, |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + if (config.quiet || format === "text") { |
| 145 | + emitBare("Seats assigned successfully."); |
| 146 | + } else { |
| 147 | + emitResult(data, format); |
| 148 | + } |
| 149 | + }, |
| 150 | +}); |
| 151 | + |
| 152 | +function buildQueryParams( |
| 153 | + flags: GlobalFlags, |
| 154 | + workspaceId: string, |
| 155 | +): Record<string, string | string[] | undefined> { |
| 156 | + const params: Record<string, string | string[] | undefined> = {}; |
| 157 | + |
| 158 | + params.WorkspaceId = workspaceId; |
| 159 | + if (flags.seatType) params.SeatType = flags.seatType as string; |
| 160 | + if (flags.callerUacAccountId) params.CallerUacAccountId = flags.callerUacAccountId as string; |
| 161 | + if (flags.namespaceId) params.NamespaceId = flags.namespaceId as string; |
| 162 | + if (flags.locale) params.Locale = flags.locale as string; |
| 163 | + |
| 164 | + const accountIds = flags.accountId as string | string[] | undefined; |
| 165 | + if (Array.isArray(accountIds) && accountIds.length > 0) { |
| 166 | + params.AccountIds = accountIds; |
| 167 | + } else if (typeof accountIds === "string" && accountIds.length > 0) { |
| 168 | + params.AccountIds = accountIds; |
| 169 | + } |
| 170 | + |
| 171 | + return params; |
| 172 | +} |
0 commit comments