From 60470e5d501f5a7eaed54c5bbe7dcbc87ed0a14c Mon Sep 17 00:00:00 2001 From: Tony Huang Date: Mon, 29 Apr 2024 15:44:46 -0400 Subject: [PATCH] Skip location prompt if backend is unambiguous for apphosting:secrets:grantaccess (#7060) * delete * pass message * use backend location disambiguation lib --- .../apphosting-secrets-grantaccess.ts | 24 +++++++++++-------- src/test/apphosting/index.spec.ts | 4 ++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/commands/apphosting-secrets-grantaccess.ts b/src/commands/apphosting-secrets-grantaccess.ts index 5645378c081..a99da896620 100644 --- a/src/commands/apphosting-secrets-grantaccess.ts +++ b/src/commands/apphosting-secrets-grantaccess.ts @@ -7,11 +7,11 @@ import * as secretManager from "../gcp/secretManager"; import { requirePermissions } from "../requirePermissions"; import * as apphosting from "../gcp/apphosting"; import * as secrets from "../apphosting/secrets"; -import { promptLocation } from "../apphosting"; +import { getBackendForAmbiguousLocation } from "../apphosting"; export const command = new Command("apphosting:secrets:grantaccess ") .description("grant service accounts permissions to the provided secret") - .option("-l, --location ", "backend location") + .option("-l, --location ", "backend location", "-") .option("-b, --backend ", "backend name") .before(requireAuth) .before(secretManager.ensureApi) @@ -34,20 +34,24 @@ export const command = new Command("apphosting:secrets:grantaccess " ); } - let location = options.location as string; - - location = - location || (await promptLocation(projectId, "Please select the location of your backend")); - - // TODO: consider showing dialog if --backend is missing - const exists = await secretManager.secretExists(projectId, secretName); if (!exists) { throw new FirebaseError(`Cannot find secret ${secretName}`); } const backendId = options.backend as string; - const backend = await apphosting.getBackend(projectId, location, backendId); + const location = options.location as string; + let backend: apphosting.Backend; + if (location === "" || location === "-") { + backend = await getBackendForAmbiguousLocation( + projectId, + backendId, + "Please select the location of your backend:", + ); + } else { + backend = await apphosting.getBackend(projectId, location, backendId); + } + const accounts = secrets.toMulti(secrets.serviceAccountsForBackend(projectNumber, backend)); await secrets.grantSecretAccess(projectId, projectNumber, secretName, accounts); diff --git a/src/test/apphosting/index.spec.ts b/src/test/apphosting/index.spec.ts index 8085396d60e..ff5a79968c2 100644 --- a/src/test/apphosting/index.spec.ts +++ b/src/test/apphosting/index.spec.ts @@ -227,7 +227,7 @@ describe("apphosting setup functions", () => { }); it("returns a location selection", async () => { - const location = await promptLocation(projectId); + const location = await promptLocation(projectId, /* prompt= */ ""); expect(location).to.be.eq("us-central1"); }); @@ -305,7 +305,7 @@ describe("apphosting setup functions", () => { getBackendForAmbiguousLocation( projectId, "foo", - /* prompt= */ "Please select the location of the backend you'd like to delete:", + "Please select the location of the backend you'd like to delete:", ), ).to.eventually.equal(backendFoo);