Skip to content

Commit

Permalink
use backend location disambiguation lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyjhuang committed Apr 27, 2024
1 parent 95d459c commit 3df4012
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/apphosting/index.ts
Expand Up @@ -408,7 +408,10 @@ export async function deleteBackendAndPoll(
/**
* Prompts the user for a location.
*/
export async function promptLocation(projectId: string, prompt: string): Promise<string> {
export async function promptLocation(
projectId: string,
prompt: string = "Please select a location:",

Check warning on line 413 in src/apphosting/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Type string trivially inferred from a string literal, remove type annotation
): Promise<string> {
const allowedLocations = (await apphosting.listLocations(projectId)).map((loc) => loc.locationId);

return (await promptOnce({
Expand Down
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
8 changes: 6 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 @@ -302,7 +302,11 @@ describe("apphosting setup functions", () => {
promptOnceStub.resolves(location);

await expect(
getBackendForAmbiguousLocation(projectId, "foo", /* prompt= */ ""),
getBackendForAmbiguousLocation(
projectId,
"foo",
"Please select the location of the backend you'd like to delete:",
),
).to.eventually.equal(backendFoo);

expect(promptOnceStub).to.be.calledWith({
Expand Down

0 comments on commit 3df4012

Please sign in to comment.