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

Make apphosting:backends:delete prompt users on which backend to delete. #8214

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
53 changes: 53 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,59 @@
}
}

/**
* Fetches
*
*
*/
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}.\n` +

Check warning on line 446 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.",
);
}
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 chosenBackends = await promptOnce({
name: "backend",
type: "checkbox",
message: chooseBackendPrompt,
choices: Array.from(backendsByDisplay.keys(), (name) => {
return {
checked: false,
name: name,
value: name,
};
}),
});
return Array.from(chosenBackends, (name) => backendsByDisplay.get(name)!);

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

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
}

/**
* 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 +496,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 499 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 +519,11 @@
backends.forEach((backend) =>
backendsByLocation.set(apphosting.parseBackendName(backend.name).location, backend),
);
const location = await promptOnce({

Check warning on line 522 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 528 in src/apphosting/backend.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion

Check warning on line 528 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`
}
59 changes: 26 additions & 33 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,20 @@ 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),
});
}
const spinner = ora("Deleting backend(s)...").start();

backends.forEach(async (b) => {
const { location, id } = apphosting.parseBackendName(b.name);
try {
await deleteBackendAndPoll(projectId, location, id);
spinner.succeed(`Successfully deleted the backend: ${id}(${location})`);
} catch (err: unknown) {
throw new FirebaseError(
`Failed to delete backend: ${id}(${location}). Please retry to delete remaining backends.`,
{
original: getError(err),
},
);
}
});
});
Loading