Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove --location from apphosting:backends:delete #8262

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/apphosting/backend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
promptLocation,
setDefaultTrafficPolicy,
ensureAppHostingComputeServiceAccount,
chooseBackends,
getBackendForAmbiguousLocation,
} from "./backend";
import * as deploymentTool from "../deploymentTool";
Expand Down Expand Up @@ -266,6 +267,85 @@ describe("apphosting setup functions", () => {
});
});

describe("chooseBackends", () => {
const backendChickenAsia = {
name: `projects/${projectId}/locations/asia-east1/backends/chicken`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};

const backendChickenEurope = {
name: `projects/${projectId}/locations/europe-west4/backends/chicken`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};

const backendChickenUS = {
name: `projects/${projectId}/locations/us-central1/backends/chicken`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};

const backendCow = {
name: `projects/${projectId}/locations/asia-east1/backends/cow`,
labels: {},
createTime: "0",
updateTime: "1",
uri: "https://placeholder.com",
};

const allBackends = [backendChickenAsia, backendChickenEurope, backendChickenUS, backendCow];

it("returns backend if only one is found", async () => {
listBackendsStub.resolves({
backends: allBackends,
});

await expect(chooseBackends(projectId, "cow", /* prompt= */ "")).to.eventually.deep.equal([
backendCow,
]);
});

it("throws if --force is used when multiple backends are found", async () => {
listBackendsStub.resolves({
backends: allBackends,
});

await expect(
chooseBackends(projectId, "chicken", /* prompt= */ "", /* force= */ true),
).to.be.rejectedWith(
"Force cannot be used because multiple backends were found with ID chicken.",
);
});

it("throws if no backend is found", async () => {
listBackendsStub.resolves({
backends: allBackends,
});

await expect(chooseBackends(projectId, "farmer", /* prompt= */ "")).to.be.rejectedWith(
'No backend named "farmer" found.',
);
});

it("lets user choose backends when more than one share a name", async () => {
listBackendsStub.resolves({
backends: allBackends,
});
promptOnceStub.resolves(["chicken(asia-east1)", "chicken(europe-west4)"]);

await expect(chooseBackends(projectId, "chicken", /* prompt= */ "")).to.eventually.deep.equal(
[backendChickenAsia, backendChickenEurope],
);
});
});

describe("getBackendForAmbiguousLocation", () => {
const backendFoo = {
name: `projects/${projectId}/locations/${location}/backends/foo`,
Expand Down
58 changes: 58 additions & 0 deletions src/apphosting/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
// SSL.
const maybeNodeError = err as { cause: { code: string }; code: string };
if (
/HANDSHAKE_FAILURE/.test(maybeNodeError?.cause?.code) ||

Check warning on line 51 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Use `String#includes()` method with a string instead
"EPROTO" === maybeNodeError?.code
) {
return false;
Expand Down Expand Up @@ -263,10 +263,10 @@
async function promptNewBackendId(
projectId: string,
location: string,
prompt: any,

Check warning on line 266 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
): Promise<string> {
while (true) {

Check warning on line 268 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected constant condition
const backendId = await promptOnce(prompt);

Check warning on line 269 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `QuestionsThatReturnAString<Answers>`
try {
await apphosting.getBackend(projectId, location, backendId);
} catch (err: unknown) {
Expand Down Expand Up @@ -429,6 +429,64 @@
}
}

/**
* Fetches backends of the given backendId and lets the user choose if more than one is found.
*/
export async function chooseBackends(
projectId: string,
backendId: string,
chooseBackendPrompt: string,
force?: boolean,
): Promise<apphosting.Backend[]> {
let { unreachable, backends } = await apphosting.listBackends(projectId, "-");
if (unreachable && unreachable.length !== 0) {
logWarning(
`The following locations are currently unreachable: ${unreachable.join(",")}.\n` +
"If your backend is in one of these regions, please try again later.",
);
}
backends = backends.filter(
(backend) => apphosting.parseBackendName(backend.name).id === backendId,
);
if (backends.length === 0) {
throw new FirebaseError(`No backend named "${backendId}" found.`);
}
if (backends.length === 1) {
return backends;
}

if (force) {
throw new FirebaseError(
`Force cannot be used because multiple backends were found with ID ${backendId}.`,
);
}
const backendsByDisplay = new Map<string, apphosting.Backend>();
backends.forEach((backend) => {
const { location, id } = apphosting.parseBackendName(backend.name);
backendsByDisplay.set(`${id}(${location})`, backend);
});
const chosenBackendDisplays = await promptOnce({
name: "backend",
type: "checkbox",
message: chooseBackendPrompt,
choices: Array.from(backendsByDisplay.keys(), (name) => {
return {
checked: false,
name: name,
value: name,
};
}),
});
const chosenBackends: apphosting.Backend[] = [];
chosenBackendDisplays.forEach((backendDisplay) => {
const backend = backendsByDisplay.get(backendDisplay);
if (backend !== undefined) {
chosenBackends.push(backend);
}
});
return chosenBackends;
}

/**
* Fetches a backend from the server. If there are multiple backends with that name (ie multi-regional backends),
* prompts the user to disambiguate. If the force option is specified and multiple backends have the same name,
Expand All @@ -443,7 +501,7 @@
let { unreachable, backends } = await apphosting.listBackends(projectId, "-");
if (unreachable && unreachable.length !== 0) {
logWarning(
`The following locations are currently unreachable: ${unreachable}.\n` +

Check warning on line 504 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid type "string[]" of template literal expression
"If your backend is in one of these regions, please try again later.",
);
}
Expand All @@ -466,11 +524,11 @@
backends.forEach((backend) =>
backendsByLocation.set(apphosting.parseBackendName(backend.name).location, backend),
);
const location = await promptOnce({

Check warning on line 527 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
name: "location",
type: "list",
message: locationDisambugationPrompt,
choices: [...backendsByLocation.keys()],
});
return backendsByLocation.get(location)!;

Check warning on line 533 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion

Check warning on line 533 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`
}
54 changes: 22 additions & 32 deletions src/commands/apphosting-backends-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,25 @@ import { promptOnce } from "../prompt";
import * as utils from "../utils";
import * as apphosting from "../gcp/apphosting";
import { printBackendsTable } from "./apphosting-backends-list";
import {
deleteBackendAndPoll,
getBackendForAmbiguousLocation,
getBackendForLocation,
} from "../apphosting/backend";
import { deleteBackendAndPoll, chooseBackends } from "../apphosting/backend";
import * as ora from "ora";

export const command = new Command("apphosting:backends:delete <backend>")
.description("delete a Firebase App Hosting backend")
.option("-l, --location <location>", "specify the location of the backend")
.withForce()
.before(apphosting.ensureApiEnabled)
.action(async (backendId: string, options: Options) => {
const projectId = needProjectId(options);
if (options.location !== undefined) {
utils.logWarning("--location is being removed in the next major release.");
}
let location = (options.location as string) ?? "-";
let backend: apphosting.Backend;
if (location === "-" || location === "") {
backend = await getBackendForAmbiguousLocation(
projectId,
backendId,
"Please select the location of the backend you'd like to delete:",
);
location = apphosting.parseBackendName(backend.name).location;
} else {
backend = await getBackendForLocation(projectId, location, backendId);
}

utils.logWarning("You are about to permanently delete this backend:");
printBackendsTable([backend]);
const backends = await chooseBackends(
projectId,
backendId,
"Please select the backends you'd like to delete:",
options.force,
);

utils.logWarning("You are about to permanently delete these backend(s):");
printBackendsTable(backends);

const confirmDeletion = await promptOnce(
{
Expand All @@ -52,14 +39,17 @@ export const command = new Command("apphosting:backends:delete <backend>")
return;
}

const spinner = ora("Deleting backend...").start();
try {
await deleteBackendAndPoll(projectId, location, backendId);
spinner.succeed(`Successfully deleted the backend: ${backendId}`);
} catch (err: unknown) {
spinner.stop();
throw new FirebaseError(`Failed to delete backend: ${backendId}.`, {
original: getError(err),
});
for (const b of backends) {
const { location, id } = apphosting.parseBackendName(b.name);
const spinner = ora(`Deleting backend ${id}(${location})...`).start();
try {
await deleteBackendAndPoll(projectId, location, id);
spinner.succeed(`Successfully deleted the backend: ${id}(${location})`);
} catch (err: unknown) {
spinner.stop();
throw new FirebaseError(`Failed to delete backend: ${id}(${location}). Please retry.`, {
original: getError(err),
});
}
}
});
Loading