Skip to content

Commit

Permalink
Skip location prompt if backend is unambiguous for apphosting:secrets…
Browse files Browse the repository at this point in the history
…:grantaccess (#7060)

* delete

* pass message

* use backend location disambiguation lib
  • Loading branch information
tonyjhuang committed Apr 29, 2024
1 parent 54f6db3 commit f915865
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/commands/apphosting-secrets-grantaccess.ts
Expand Up @@ -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 <secretName>")
.description("grant service accounts permissions to the provided secret")
.option("-l, --location <location>", "backend location")
.option("-l, --location <location>", "backend location", "-")
.option("-b, --backend <backend>", "backend name")
.before(requireAuth)
.before(secretManager.ensureApi)
Expand All @@ -34,20 +34,24 @@ export const command = new Command("apphosting:secrets:grantaccess <secretName>"
);
}

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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/apphosting/index.spec.ts
Expand Up @@ -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");
});

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit f915865

Please sign in to comment.