@caelo-cms/provisioning 0.12.5-rc.1 → 0.12.6-rc.1
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.
- package/dist/admin-internal.d.ts +1 -1
- package/dist/agent-db.js +192 -130
- package/dist/cli-routing.d.ts.map +1 -1
- package/dist/cli-routing.js +2 -1
- package/dist/cli-routing.js.map +1 -1
- package/dist/cli.js +15 -1
- package/dist/cli.js.map +1 -1
- package/dist/gcloud.d.ts +32 -1
- package/dist/gcloud.d.ts.map +1 -1
- package/dist/gcloud.js +59 -7
- package/dist/gcloud.js.map +1 -1
- package/dist/infra-contract.d.ts +1 -1
- package/dist/infra-contract.d.ts.map +1 -1
- package/dist/infra-contract.js +9 -1
- package/dist/infra-contract.js.map +1 -1
- package/dist/upgrader-iam-preflight.d.ts.map +1 -1
- package/dist/upgrader-iam-preflight.js +8 -1
- package/dist/upgrader-iam-preflight.js.map +1 -1
- package/dist/upgrader-setup.d.ts +19 -0
- package/dist/upgrader-setup.d.ts.map +1 -1
- package/dist/upgrader-setup.js +78 -56
- package/dist/upgrader-setup.js.map +1 -1
- package/dist/wizard.d.ts +6 -0
- package/dist/wizard.d.ts.map +1 -1
- package/dist/wizard.js +4 -0
- package/dist/wizard.js.map +1 -1
- package/dist/wizards/gcp-billing.d.ts +81 -0
- package/dist/wizards/gcp-billing.d.ts.map +1 -0
- package/dist/wizards/gcp-billing.js +117 -0
- package/dist/wizards/gcp-billing.js.map +1 -0
- package/dist/wizards/gcp-firebase.d.ts +2 -0
- package/dist/wizards/gcp-firebase.d.ts.map +1 -1
- package/dist/wizards/gcp-firebase.js +1 -0
- package/dist/wizards/gcp-firebase.js.map +1 -1
- package/dist/wizards/gcp-install-job.d.ts +3 -0
- package/dist/wizards/gcp-install-job.d.ts.map +1 -1
- package/dist/wizards/gcp-install-job.js +5 -1
- package/dist/wizards/gcp-install-job.js.map +1 -1
- package/dist/wizards/gcp.d.ts +6 -0
- package/dist/wizards/gcp.d.ts.map +1 -1
- package/dist/wizards/gcp.js +68 -36
- package/dist/wizards/gcp.js.map +1 -1
- package/package.json +2 -2
- package/src/admin-internal.ts +1 -1
- package/src/cli-routing.test.ts +1 -1
- package/src/cli-routing.ts +2 -1
- package/src/cli.ts +16 -1
- package/src/gcloud.test.ts +67 -0
- package/src/gcloud.ts +78 -11
- package/src/infra-contract.test.ts +4 -3
- package/src/infra-contract.ts +9 -1
- package/src/upgrader-iam-preflight.test.ts +49 -42
- package/src/upgrader-iam-preflight.ts +8 -0
- package/src/upgrader-job.test.ts +1 -0
- package/src/upgrader-setup.test.ts +2 -0
- package/src/upgrader-setup.ts +86 -55
- package/src/wizard.ts +10 -0
- package/src/wizards/gcp-billing.test.ts +260 -0
- package/src/wizards/gcp-billing.ts +150 -0
- package/src/wizards/gcp-firebase.ts +3 -0
- package/src/wizards/gcp-install-job.ts +7 -1
- package/src/wizards/gcp.ts +86 -43
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
3
|
+
import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
|
+
import type { BillingAccount } from "../gcloud.js";
|
|
7
|
+
import {
|
|
8
|
+
chooseBillingAccount,
|
|
9
|
+
linkedBillingProblem,
|
|
10
|
+
parseBillingAccountId,
|
|
11
|
+
requestedBillingProblem,
|
|
12
|
+
sameBillingAccount,
|
|
13
|
+
} from "./gcp-billing.js";
|
|
14
|
+
|
|
15
|
+
const AGENCY: BillingAccount = { id: "0A1B2C-3D4E5F-60718A", displayName: "Agency", open: true };
|
|
16
|
+
const CLIENT: BillingAccount = { id: "111111-222222-333333", displayName: "Client", open: true };
|
|
17
|
+
const CLOSED: BillingAccount = { id: "999999-888888-777777", displayName: "Old card", open: false };
|
|
18
|
+
|
|
19
|
+
describe("parseBillingAccountId", () => {
|
|
20
|
+
it("takes the id gcloud prints, its resource name and either letter case", () => {
|
|
21
|
+
expect(parseBillingAccountId("0A1B2C-3D4E5F-60718A")).toBe("0A1B2C-3D4E5F-60718A");
|
|
22
|
+
expect(parseBillingAccountId("billingAccounts/0a1b2c-3d4e5f-60718a")).toBe(
|
|
23
|
+
"0A1B2C-3D4E5F-60718A",
|
|
24
|
+
);
|
|
25
|
+
expect(parseBillingAccountId(" 111111-222222-333333 ")).toBe("111111-222222-333333");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
for (const bad of [
|
|
29
|
+
"",
|
|
30
|
+
"--non-interactive",
|
|
31
|
+
"0A1B2C3D4E5F60718A",
|
|
32
|
+
"0A1B2C-3D4E5F",
|
|
33
|
+
"XYZXYZ-3D4E5F-60718A",
|
|
34
|
+
]) {
|
|
35
|
+
it(`refuses ${JSON.stringify(bad)} and names the flag`, () => {
|
|
36
|
+
expect(() => parseBillingAccountId(bad)).toThrow("--billing-account");
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("chooseBillingAccount", () => {
|
|
42
|
+
it("links the account the flag names among several open ones, also without a terminal", () => {
|
|
43
|
+
const choice = chooseBillingAccount({
|
|
44
|
+
accounts: [AGENCY, CLIENT, CLOSED],
|
|
45
|
+
requested: "111111-222222-333333",
|
|
46
|
+
nonInteractive: true,
|
|
47
|
+
});
|
|
48
|
+
expect(choice).toEqual({ kind: "use", account: CLIENT });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("matches the flag whatever its letter case", () => {
|
|
52
|
+
const choice = chooseBillingAccount({
|
|
53
|
+
accounts: [AGENCY, CLIENT],
|
|
54
|
+
requested: "0a1b2c-3d4e5f-60718a",
|
|
55
|
+
nonInteractive: true,
|
|
56
|
+
});
|
|
57
|
+
expect(choice).toEqual({ kind: "use", account: AGENCY });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("refuses a closed account and names the open ones", () => {
|
|
61
|
+
const choice = chooseBillingAccount({
|
|
62
|
+
accounts: [AGENCY, CLIENT, CLOSED],
|
|
63
|
+
requested: CLOSED.id,
|
|
64
|
+
nonInteractive: true,
|
|
65
|
+
});
|
|
66
|
+
expect(choice.kind).toBe("refuse");
|
|
67
|
+
const message = choice.kind === "refuse" ? choice.message : "";
|
|
68
|
+
expect(message).toContain(`${CLOSED.id} is closed`);
|
|
69
|
+
expect(message).toContain("0A1B2C-3D4E5F-60718A (Agency)");
|
|
70
|
+
expect(message).toContain("111111-222222-333333 (Client)");
|
|
71
|
+
expect(message).not.toContain("Old card");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("refuses an account the signed-in account cannot see, also when it is the only open one", () => {
|
|
75
|
+
const choice = chooseBillingAccount({
|
|
76
|
+
accounts: [AGENCY],
|
|
77
|
+
requested: "123456-123456-123456",
|
|
78
|
+
nonInteractive: false,
|
|
79
|
+
});
|
|
80
|
+
expect(choice.kind).toBe("refuse");
|
|
81
|
+
const message = choice.kind === "refuse" ? choice.message : "";
|
|
82
|
+
expect(message).toContain("123456-123456-123456 is not among");
|
|
83
|
+
expect(message).toContain("0A1B2C-3D4E5F-60718A (Agency)");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("without the flag uses the only open account", () => {
|
|
87
|
+
expect(
|
|
88
|
+
chooseBillingAccount({
|
|
89
|
+
accounts: [CLOSED, AGENCY],
|
|
90
|
+
requested: undefined,
|
|
91
|
+
nonInteractive: true,
|
|
92
|
+
}),
|
|
93
|
+
).toEqual({ kind: "use", account: AGENCY });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("without the flag asks between several open accounts in a terminal", () => {
|
|
97
|
+
expect(
|
|
98
|
+
chooseBillingAccount({
|
|
99
|
+
accounts: [AGENCY, CLIENT, CLOSED],
|
|
100
|
+
requested: undefined,
|
|
101
|
+
nonInteractive: false,
|
|
102
|
+
}),
|
|
103
|
+
).toEqual({ kind: "ask", open: [AGENCY, CLIENT] });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("without the flag stops a --non-interactive run with several open accounts and names the flag as it parses", () => {
|
|
107
|
+
const choice = chooseBillingAccount({
|
|
108
|
+
accounts: [AGENCY, CLIENT],
|
|
109
|
+
requested: undefined,
|
|
110
|
+
nonInteractive: true,
|
|
111
|
+
});
|
|
112
|
+
expect(choice.kind).toBe("refuse");
|
|
113
|
+
const message = choice.kind === "refuse" ? choice.message : "";
|
|
114
|
+
// `--billing-account=<id>` (the old hint) is not a form the CLI reads.
|
|
115
|
+
expect(message).toContain("--billing-account <id>");
|
|
116
|
+
expect(message).not.toContain("--billing-account=");
|
|
117
|
+
expect(message).toContain("0A1B2C-3D4E5F-60718A (Agency)");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("refuses when no account is open, flag or not", () => {
|
|
121
|
+
for (const requested of [undefined, CLOSED.id]) {
|
|
122
|
+
const choice = chooseBillingAccount({ accounts: [CLOSED], requested, nonInteractive: true });
|
|
123
|
+
expect(choice.kind).toBe("refuse");
|
|
124
|
+
expect(choice.kind === "refuse" ? choice.message : "").toContain("No open billing account");
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("sameBillingAccount", () => {
|
|
130
|
+
it("ignores letter case and the resource-name prefix", () => {
|
|
131
|
+
expect(sameBillingAccount("billingAccounts/0a1b2c-3d4e5f-60718a", AGENCY.id)).toBe(true);
|
|
132
|
+
expect(sameBillingAccount(AGENCY.id, CLIENT.id)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("linkedBillingProblem", () => {
|
|
137
|
+
const base = { projectId: "caelo-site", linked: AGENCY.id };
|
|
138
|
+
|
|
139
|
+
it("lets the run go on without the flag, without a link yet, or with the linked account", () => {
|
|
140
|
+
expect(linkedBillingProblem({ ...base, requested: undefined })).toBeNull();
|
|
141
|
+
expect(linkedBillingProblem({ ...base, linked: null, requested: CLIENT.id })).toBeNull();
|
|
142
|
+
expect(linkedBillingProblem({ ...base, requested: "0a1b2c-3d4e5f-60718a" })).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("refuses another account and says how to change the link", () => {
|
|
146
|
+
const message = linkedBillingProblem({ ...base, requested: CLIENT.id }) ?? "";
|
|
147
|
+
expect(message).toContain(`caelo-site is already linked to billing account ${AGENCY.id}`);
|
|
148
|
+
expect(message).toContain(`--billing-account ${CLIENT.id} would not change that`);
|
|
149
|
+
expect(message).toContain("console.cloud.google.com/billing/linkedaccount?project=caelo-site");
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("requestedBillingProblem", () => {
|
|
154
|
+
it("asks Google Cloud nothing without the flag", async () => {
|
|
155
|
+
let asked = 0;
|
|
156
|
+
const describe = async () => {
|
|
157
|
+
asked += 1;
|
|
158
|
+
return { ok: true as const, accountId: AGENCY.id };
|
|
159
|
+
};
|
|
160
|
+
expect(await requestedBillingProblem("caelo-site", undefined, describe)).toBeNull();
|
|
161
|
+
expect(asked).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("compares the flag with the link Google Cloud records", async () => {
|
|
165
|
+
const describe = async () => ({ ok: true as const, accountId: AGENCY.id });
|
|
166
|
+
expect(await requestedBillingProblem("caelo-site", AGENCY.id, describe)).toBeNull();
|
|
167
|
+
expect(await requestedBillingProblem("caelo-site", CLIENT.id, describe)).toContain(
|
|
168
|
+
"is already linked to billing account",
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("stops with gcloud's error when the link cannot be read", async () => {
|
|
173
|
+
const error = "gcloud billing projects describe caelo-site: PERMISSION_DENIED";
|
|
174
|
+
const describe = async () => ({ ok: false as const, error });
|
|
175
|
+
expect(await requestedBillingProblem("caelo-site", CLIENT.id, describe)).toBe(error);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("job mode (#1580 review)", () => {
|
|
180
|
+
it("checks the flag before it follows or shows an install job", () => {
|
|
181
|
+
// Following a running job and showing a finished one return before the
|
|
182
|
+
// wizard's billing step, so the check has to come first there.
|
|
183
|
+
const source = readFileSync(join(import.meta.dir, "gcp-install-job.ts"), "utf8");
|
|
184
|
+
const prep = source.slice(source.indexOf("export async function prepareJobModeInstall("));
|
|
185
|
+
const check = prep.indexOf("requestedBillingProblem(projectId, opts.billingAccountId)");
|
|
186
|
+
expect(check).toBeGreaterThan(-1);
|
|
187
|
+
expect(check).toBeLessThan(prep.indexOf("await followToTheEnd("));
|
|
188
|
+
expect(check).toBeLessThan(prep.indexOf("await showResult("));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("hands the flag to the job-mode preparation", () => {
|
|
192
|
+
const wizard = readFileSync(join(import.meta.dir, "..", "wizard.ts"), "utf8");
|
|
193
|
+
const call = wizard.slice(wizard.indexOf("await prepareJobModeInstall({"));
|
|
194
|
+
expect(call.slice(0, call.indexOf("});"))).toContain("billingAccountId: opts.billingAccountId");
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe("the GCP wizard", () => {
|
|
199
|
+
it("settles the billing account before it creates the project", () => {
|
|
200
|
+
// A wrong --billing-account must stop the run with nothing created.
|
|
201
|
+
const source = readFileSync(join(import.meta.dir, "gcp.ts"), "utf8");
|
|
202
|
+
const flow = source.slice(source.indexOf("export async function runGcpWizard("));
|
|
203
|
+
const choice = flow.indexOf("await stepBillingChoice(");
|
|
204
|
+
const create = flow.indexOf("await stepProjectCreate(");
|
|
205
|
+
const link = flow.indexOf("await stepBillingLink(");
|
|
206
|
+
expect(choice).toBeGreaterThan(-1);
|
|
207
|
+
expect(choice).toBeLessThan(create);
|
|
208
|
+
expect(create).toBeLessThan(link);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("cms-provision --billing-account", () => {
|
|
213
|
+
const CLI = resolve(import.meta.dir, "..", "cli.ts");
|
|
214
|
+
let home: string;
|
|
215
|
+
beforeEach(() => {
|
|
216
|
+
home = mkdtempSync(join(tmpdir(), "caelo-billing-flag-"));
|
|
217
|
+
});
|
|
218
|
+
afterEach(() => rmSync(home, { recursive: true, force: true }));
|
|
219
|
+
|
|
220
|
+
/** The `~/.caelo-<install-id>` folders a started wizard would create (bun itself adds `Library/`). */
|
|
221
|
+
const installFolders = () => readdirSync(home).filter((name) => name.startsWith(".caelo"));
|
|
222
|
+
|
|
223
|
+
function wizard(...args: string[]) {
|
|
224
|
+
return Bun.spawnSync(
|
|
225
|
+
[
|
|
226
|
+
process.execPath,
|
|
227
|
+
CLI,
|
|
228
|
+
"--provider",
|
|
229
|
+
"gcp",
|
|
230
|
+
"--domain",
|
|
231
|
+
"example.com",
|
|
232
|
+
"--owner-email",
|
|
233
|
+
"owner@example.com",
|
|
234
|
+
"--region",
|
|
235
|
+
"europe-west1",
|
|
236
|
+
"--non-interactive",
|
|
237
|
+
"--local",
|
|
238
|
+
...args,
|
|
239
|
+
],
|
|
240
|
+
// No gcloud on PATH and a scratch HOME: a regression that let the run
|
|
241
|
+
// past the flag check could reach neither a cloud account nor an
|
|
242
|
+
// install folder.
|
|
243
|
+
{ env: { HOME: home, PATH: "" }, stdout: "pipe", stderr: "pipe" },
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
it("refuses a malformed id before the wizard starts", () => {
|
|
248
|
+
const r = wizard("--billing-account", "my-agency-account");
|
|
249
|
+
expect(r.exitCode).toBe(2);
|
|
250
|
+
expect(r.stderr.toString()).toContain("--billing-account needs a billing account id like");
|
|
251
|
+
expect(installFolders()).toEqual([]);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("refuses the flag without a value", () => {
|
|
255
|
+
const r = wizard("--billing-account");
|
|
256
|
+
expect(r.exitCode).toBe(2);
|
|
257
|
+
expect(r.stderr.toString()).toContain("--billing-account needs a billing account id");
|
|
258
|
+
expect(installFolders()).toEqual([]);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Which billing account a new install's Google Cloud project is linked to
|
|
5
|
+
* (#1580).
|
|
6
|
+
*
|
|
7
|
+
* The wizard settles the account before it creates the project, so a
|
|
8
|
+
* mistyped or closed `--billing-account` stops the run with nothing
|
|
9
|
+
* created; the link itself follows the project create. Without the flag the
|
|
10
|
+
* one open account is used, several are offered in a picker, and a
|
|
11
|
+
* `--non-interactive` run with several stops and names the flag, because a
|
|
12
|
+
* script has nobody to pick for it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type BillingAccount,
|
|
17
|
+
type ProjectBillingResult,
|
|
18
|
+
projectBillingAccount,
|
|
19
|
+
} from "../gcloud.js";
|
|
20
|
+
|
|
21
|
+
/** The installer flag that names the billing account. */
|
|
22
|
+
export const BILLING_ACCOUNT_FLAG = "--billing-account";
|
|
23
|
+
|
|
24
|
+
/** A billing account id as gcloud prints it: three groups of six hex digits. */
|
|
25
|
+
const BILLING_ACCOUNT_ID = /^[0-9A-F]{6}-[0-9A-F]{6}-[0-9A-F]{6}$/;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Parse `--billing-account`: the id `gcloud billing accounts list` prints
|
|
29
|
+
* (`012345-6789AB-CDEF01`), also as the API's resource name
|
|
30
|
+
* (`billingAccounts/012345-6789AB-CDEF01`), in either letter case.
|
|
31
|
+
*
|
|
32
|
+
* @returns the id in gcloud's spelling (upper case, no prefix)
|
|
33
|
+
* @throws Error naming the flag and the expected shape
|
|
34
|
+
*/
|
|
35
|
+
export function parseBillingAccountId(raw: string): string {
|
|
36
|
+
const id = raw
|
|
37
|
+
.trim()
|
|
38
|
+
.replace(/^billingAccounts\//, "")
|
|
39
|
+
.toUpperCase();
|
|
40
|
+
if (!BILLING_ACCOUNT_ID.test(id)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`${BILLING_ACCOUNT_FLAG} needs a billing account id like 012345-6789AB-CDEF01, as \`gcloud billing accounts list\` prints it; got "${raw}".`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return id;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What the billing step does with the accounts gcloud listed. */
|
|
49
|
+
export type BillingChoice =
|
|
50
|
+
/** Link this account: the one asked for, or the only open one. */
|
|
51
|
+
| { readonly kind: "use"; readonly account: BillingAccount }
|
|
52
|
+
/** Several open accounts and a terminal to ask in: offer these. */
|
|
53
|
+
| { readonly kind: "ask"; readonly open: readonly BillingAccount[] }
|
|
54
|
+
/** Stop before anything is created; the message says what to do. */
|
|
55
|
+
| { readonly kind: "refuse"; readonly message: string };
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Decide the billing account from the listed accounts and the flag.
|
|
59
|
+
*
|
|
60
|
+
* @param input.requested the parsed `--billing-account`, if given
|
|
61
|
+
* @param input.nonInteractive whether `--non-interactive` was passed
|
|
62
|
+
*/
|
|
63
|
+
export function chooseBillingAccount(input: {
|
|
64
|
+
readonly accounts: readonly BillingAccount[];
|
|
65
|
+
readonly requested: string | undefined;
|
|
66
|
+
readonly nonInteractive: boolean;
|
|
67
|
+
}): BillingChoice {
|
|
68
|
+
const open = input.accounts.filter((a) => a.open);
|
|
69
|
+
if (open.length === 0) {
|
|
70
|
+
return {
|
|
71
|
+
kind: "refuse",
|
|
72
|
+
message:
|
|
73
|
+
"No open billing account found for the signed-in account. Open one at https://console.cloud.google.com/billing, then re-run.",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const { requested } = input;
|
|
77
|
+
if (requested !== undefined) {
|
|
78
|
+
const named = input.accounts.find((a) => sameBillingAccount(a.id, requested));
|
|
79
|
+
if (named?.open) return { kind: "use", account: named };
|
|
80
|
+
const why = named
|
|
81
|
+
? "is closed"
|
|
82
|
+
: "is not among the billing accounts the signed-in account can use";
|
|
83
|
+
return {
|
|
84
|
+
kind: "refuse",
|
|
85
|
+
message: `Billing account ${requested} ${why}. Re-run with ${BILLING_ACCOUNT_FLAG} set to an open one: ${describeAccounts(open)}.`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (open.length === 1) return { kind: "use", account: open[0] as BillingAccount };
|
|
89
|
+
if (input.nonInteractive) {
|
|
90
|
+
return {
|
|
91
|
+
kind: "refuse",
|
|
92
|
+
message: `The signed-in account has several open billing accounts, and --non-interactive leaves nobody to pick one. Re-run with ${BILLING_ACCOUNT_FLAG} <id>, one of: ${describeAccounts(open)}.`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return { kind: "ask", open };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether two billing account ids name the same account, whatever their
|
|
100
|
+
* letter case or `billingAccounts/` prefix.
|
|
101
|
+
*/
|
|
102
|
+
export function sameBillingAccount(a: string, b: string): boolean {
|
|
103
|
+
const norm = (id: string) => id.replace(/^billingAccounts\//, "").toUpperCase();
|
|
104
|
+
return norm(a) === norm(b);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Refuse a `--billing-account` that names another account than the one the
|
|
109
|
+
* project is linked to. A re-run never relinks: the project has billed to
|
|
110
|
+
* that account since the first run, and resuming, following or showing an
|
|
111
|
+
* install is no place to switch it silently.
|
|
112
|
+
*
|
|
113
|
+
* @param input.linked the project's account ({@link projectBillingAccount}),
|
|
114
|
+
* null when it has none yet (the billing step links it)
|
|
115
|
+
* @returns the message to stop with, or null when the flag fits
|
|
116
|
+
*/
|
|
117
|
+
export function linkedBillingProblem(input: {
|
|
118
|
+
readonly projectId: string;
|
|
119
|
+
readonly linked: string | null;
|
|
120
|
+
readonly requested: string | undefined;
|
|
121
|
+
}): string | null {
|
|
122
|
+
const { projectId, linked, requested } = input;
|
|
123
|
+
if (requested === undefined || linked === null || sameBillingAccount(linked, requested)) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return `${projectId} is already linked to billing account ${linked}; ${BILLING_ACCOUNT_FLAG} ${requested} would not change that. Drop the flag, or link the other account at https://console.cloud.google.com/billing/linkedaccount?project=${projectId} and re-run.`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check `--billing-account` against the project's billing link as Google
|
|
131
|
+
* Cloud records it, for every path that does not link billing itself: a
|
|
132
|
+
* resumed bootstrap whose link is done, and job mode following or showing
|
|
133
|
+
* an install job (#1580 review).
|
|
134
|
+
*
|
|
135
|
+
* @returns the message to stop with, or null; no gcloud call without the flag
|
|
136
|
+
*/
|
|
137
|
+
export async function requestedBillingProblem(
|
|
138
|
+
projectId: string,
|
|
139
|
+
requested: string | undefined,
|
|
140
|
+
describe: (projectId: string) => Promise<ProjectBillingResult> = projectBillingAccount,
|
|
141
|
+
): Promise<string | null> {
|
|
142
|
+
if (requested === undefined) return null;
|
|
143
|
+
const linked = await describe(projectId);
|
|
144
|
+
if (!linked.ok) return linked.error;
|
|
145
|
+
return linkedBillingProblem({ projectId, linked: linked.accountId, requested });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function describeAccounts(accounts: readonly BillingAccount[]): string {
|
|
149
|
+
return accounts.map((a) => `${a.id} (${a.displayName})`).join(", ");
|
|
150
|
+
}
|
|
@@ -40,6 +40,8 @@ export interface GcpFirebaseWizardOpts {
|
|
|
40
40
|
version?: string;
|
|
41
41
|
/** #1558 — see {@link GcpWizardOpts.acceptReleaseCandidate}. */
|
|
42
42
|
acceptReleaseCandidate?: boolean;
|
|
43
|
+
/** #1580 — see {@link GcpWizardOpts.billingAccountId}. */
|
|
44
|
+
billingAccountId?: string;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
export async function runGcpFirebaseWizard(opts: GcpFirebaseWizardOpts): Promise<void> {
|
|
@@ -73,6 +75,7 @@ export async function runGcpFirebaseWizard(opts: GcpFirebaseWizardOpts): Promise
|
|
|
73
75
|
...(opts.runMode ? { runMode: opts.runMode } : {}),
|
|
74
76
|
...(opts.version ? { version: opts.version } : {}),
|
|
75
77
|
...(opts.acceptReleaseCandidate ? { acceptReleaseCandidate: true } : {}),
|
|
78
|
+
...(opts.billingAccountId ? { billingAccountId: opts.billingAccountId } : {}),
|
|
76
79
|
provider: "gcp-firebase",
|
|
77
80
|
});
|
|
78
81
|
}
|
|
@@ -54,6 +54,7 @@ import { selfCommand } from "../self-command.js";
|
|
|
54
54
|
import { attachInstall } from "../state-attach.js";
|
|
55
55
|
import { hasLocalPulumiState, opsBucketName } from "../state-backend.js";
|
|
56
56
|
import { GcsStateStore, realSyncGcloud, type SyncGcloud } from "../state-store.js";
|
|
57
|
+
import { requestedBillingProblem } from "./gcp-billing.js";
|
|
57
58
|
import { promptProjectId } from "./gcp-prompts.js";
|
|
58
59
|
|
|
59
60
|
/** Side effects of the tail loop, injectable for tests. */
|
|
@@ -463,10 +464,13 @@ export type JobModePreparation =
|
|
|
463
464
|
* @param opts.version - `--version` (#1558): refused before following or
|
|
464
465
|
* showing a job that installs another release, since neither path reaches
|
|
465
466
|
* the wizard's own check.
|
|
467
|
+
* @param opts.billingAccountId - `--billing-account` (#1580): refused the
|
|
468
|
+
* same way when the project is linked to another billing account.
|
|
466
469
|
*/
|
|
467
470
|
export async function prepareJobModeInstall(opts: {
|
|
468
471
|
projectId?: string;
|
|
469
472
|
version?: string;
|
|
473
|
+
billingAccountId?: string;
|
|
470
474
|
}): Promise<JobModePreparation> {
|
|
471
475
|
const account = await activeAccount();
|
|
472
476
|
if (!account) {
|
|
@@ -503,7 +507,9 @@ export async function prepareJobModeInstall(opts: {
|
|
|
503
507
|
if (!meta.region) return { kind: "existing", meta };
|
|
504
508
|
const decision = await decide(gcloud, realSyncGcloud, { projectId, region: meta.region });
|
|
505
509
|
if (decision.kind === "attach" || decision.kind === "done") {
|
|
506
|
-
const problem =
|
|
510
|
+
const problem =
|
|
511
|
+
attachedInstallVersionProblem(opts.version, meta.releaseVersion) ??
|
|
512
|
+
(await requestedBillingProblem(projectId, opts.billingAccountId));
|
|
507
513
|
if (problem) {
|
|
508
514
|
cancel(problem);
|
|
509
515
|
process.exit(1);
|
package/src/wizards/gcp.ts
CHANGED
|
@@ -175,6 +175,7 @@ import {
|
|
|
175
175
|
} from "../upgrade/release-images.js";
|
|
176
176
|
import { ensureUpgraderJob, resolveUpgraderTarget } from "../upgrader-setup.js";
|
|
177
177
|
import { ensureUrlMapRedirectsAccess } from "../url-map-redirects-access.js";
|
|
178
|
+
import { chooseBillingAccount, requestedBillingProblem } from "./gcp-billing.js";
|
|
178
179
|
import { estimateGcpCost } from "./gcp-cost.js";
|
|
179
180
|
import { promptProjectId } from "./gcp-prompts.js";
|
|
180
181
|
import { GCP_STACK_KNOB_DEFAULTS, PulumiUpDeclinedError, pulumiUpGcp } from "./gcp-pulumi.js";
|
|
@@ -206,6 +207,12 @@ export interface GcpWizardOpts {
|
|
|
206
207
|
version?: string;
|
|
207
208
|
/** #1558 — `--accept-release-candidate`: confirms a release candidate without a prompt. */
|
|
208
209
|
acceptReleaseCandidate?: boolean;
|
|
210
|
+
/**
|
|
211
|
+
* #1580 — `--billing-account` (parsed, gcloud's spelling): the billing
|
|
212
|
+
* account the new project is linked to. Needed with `nonInteractive` when
|
|
213
|
+
* the signed-in account has several open ones; must be one of them.
|
|
214
|
+
*/
|
|
215
|
+
billingAccountId?: string;
|
|
209
216
|
}
|
|
210
217
|
|
|
211
218
|
export async function runGcpWizard(opts: GcpWizardOpts): Promise<void> {
|
|
@@ -241,11 +248,16 @@ export async function runGcpWizard(opts: GcpWizardOpts): Promise<void> {
|
|
|
241
248
|
// manifest instead.
|
|
242
249
|
const version = await stepRequestedRelease(installId, projectId, provider, runMode, opts);
|
|
243
250
|
|
|
251
|
+
// === 2.8. Billing account (#1580) ===
|
|
252
|
+
// Chosen before the project exists, so a wrong --billing-account stops the
|
|
253
|
+
// run with nothing created.
|
|
254
|
+
const billing = await stepBillingChoice(installId, projectId, opts);
|
|
255
|
+
|
|
244
256
|
// === 3. Project create ===
|
|
245
257
|
await stepProjectCreate(installId, projectId, opts.domain);
|
|
246
258
|
|
|
247
259
|
// === 4. Billing link ===
|
|
248
|
-
await stepBillingLink(installId, projectId,
|
|
260
|
+
await stepBillingLink(installId, projectId, billing);
|
|
249
261
|
|
|
250
262
|
// === 5. Enable APIs ===
|
|
251
263
|
await stepEnableApis(installId, projectId, provider);
|
|
@@ -1073,57 +1085,88 @@ async function stepProjectCreate(
|
|
|
1073
1085
|
markStepDone(installId, stepName, { created: true });
|
|
1074
1086
|
}
|
|
1075
1087
|
|
|
1076
|
-
|
|
1088
|
+
/** The checkpoint of the billing link; its payload records the linked account. */
|
|
1089
|
+
function billingStepName(projectId: string): string {
|
|
1090
|
+
return `billing-link-${projectId}`;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* Settle the billing account before the project is created (#1580), so a
|
|
1095
|
+
* wrong `--billing-account` stops the run with nothing changed.
|
|
1096
|
+
*
|
|
1097
|
+
* @returns the account to link, or null when the link is already done
|
|
1098
|
+
*/
|
|
1099
|
+
async function stepBillingChoice(
|
|
1077
1100
|
installId: string,
|
|
1078
1101
|
projectId: string,
|
|
1079
|
-
nonInteractive: boolean,
|
|
1080
|
-
): Promise<
|
|
1081
|
-
const stepName =
|
|
1102
|
+
opts: { readonly billingAccountId?: string; readonly nonInteractive: boolean },
|
|
1103
|
+
): Promise<BillingAccount | null> {
|
|
1104
|
+
const stepName = billingStepName(projectId);
|
|
1082
1105
|
if (isStepDone(installId, stepName)) {
|
|
1083
|
-
|
|
1084
|
-
|
|
1106
|
+
const problem = await requestedBillingProblem(projectId, opts.billingAccountId);
|
|
1107
|
+
if (problem) {
|
|
1108
|
+
log.error(red(problem));
|
|
1109
|
+
cancel("Aborted.");
|
|
1110
|
+
process.exit(1);
|
|
1111
|
+
}
|
|
1112
|
+
return null;
|
|
1085
1113
|
}
|
|
1086
|
-
const
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
log.
|
|
1090
|
-
log.warn(
|
|
1091
|
-
`Open one at https://console.cloud.google.com/billing then re-run ${bold(selfCommand())}.`,
|
|
1092
|
-
);
|
|
1114
|
+
const listed = await listBillingAccounts();
|
|
1115
|
+
if (!listed.ok) {
|
|
1116
|
+
log.error(red(listed.error));
|
|
1117
|
+
log.warn(`Check the gcloud sign-in (gcloud auth list), then re-run ${bold(selfCommand())}.`);
|
|
1093
1118
|
cancel("Aborted.");
|
|
1094
1119
|
process.exit(1);
|
|
1095
1120
|
}
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
`Multiple billing accounts but --non-interactive passed. Re-run with --billing-account=<id> (one of: ${open.map((a) => a.id).join(", ")}).`,
|
|
1104
|
-
),
|
|
1105
|
-
);
|
|
1121
|
+
const choice = chooseBillingAccount({
|
|
1122
|
+
accounts: listed.accounts,
|
|
1123
|
+
requested: opts.billingAccountId,
|
|
1124
|
+
nonInteractive: opts.nonInteractive,
|
|
1125
|
+
});
|
|
1126
|
+
if (choice.kind === "refuse") {
|
|
1127
|
+
log.error(red(choice.message));
|
|
1106
1128
|
cancel("Aborted.");
|
|
1107
1129
|
process.exit(1);
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1130
|
+
}
|
|
1131
|
+
if (choice.kind === "use") {
|
|
1132
|
+
log.info(
|
|
1133
|
+
`Billing account: ${bold(choice.account.displayName)} ${dim(`(${choice.account.id})`)}`,
|
|
1134
|
+
);
|
|
1135
|
+
return choice.account;
|
|
1136
|
+
}
|
|
1137
|
+
const picked = await select<string>({
|
|
1138
|
+
message: "Pick a billing account",
|
|
1139
|
+
options: choice.open.map((a) => ({
|
|
1140
|
+
value: a.id,
|
|
1141
|
+
label: a.displayName,
|
|
1142
|
+
hint: a.id,
|
|
1143
|
+
})),
|
|
1144
|
+
});
|
|
1145
|
+
if (isCancel(picked)) {
|
|
1146
|
+
cancel("Cancelled.");
|
|
1147
|
+
process.exit(0);
|
|
1148
|
+
}
|
|
1149
|
+
const found = choice.open.find((a) => a.id === picked);
|
|
1150
|
+
if (!found) {
|
|
1151
|
+
cancel("Picked account not found in list — internal error.");
|
|
1152
|
+
process.exit(1);
|
|
1153
|
+
}
|
|
1154
|
+
return found;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
* Link the project to the account {@link stepBillingChoice} settled; null
|
|
1159
|
+
* means the checkpoint records the link already.
|
|
1160
|
+
*/
|
|
1161
|
+
async function stepBillingLink(
|
|
1162
|
+
installId: string,
|
|
1163
|
+
projectId: string,
|
|
1164
|
+
chosen: BillingAccount | null,
|
|
1165
|
+
): Promise<void> {
|
|
1166
|
+
const stepName = billingStepName(projectId);
|
|
1167
|
+
if (chosen === null) {
|
|
1168
|
+
log.success(`Billing already linked ${dim("(checkpoint)")}`);
|
|
1169
|
+
return;
|
|
1127
1170
|
}
|
|
1128
1171
|
const s = spinner();
|
|
1129
1172
|
s.start(`Linking billing ${chosen.id}...`);
|