Skip to content

Commit 86a3138

Browse files
committed
Implement getBackend.
1 parent 2bfb61b commit 86a3138

File tree

2 files changed

+525
-1
lines changed

2 files changed

+525
-1
lines changed

Diff for: src/apphosting/backend.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Backend, BackendOutputOnlyFields, API_VERSION } from "../gcp/apphosting
1717
import { addServiceAccountToRoles } from "../gcp/resourceManager";
1818
import * as iam from "../gcp/iam";
1919
import { FirebaseError, getErrStatus, getError } from "../error";
20-
import { promptOnce } from "../prompt";
20+
import { promptOnce, confirm } from "../prompt";
2121
import { DEFAULT_LOCATION } from "./constants";
2222
import { ensure } from "../ensureApiEnabled";
2323
import * as deploymentTool from "../deploymentTool";
@@ -474,3 +474,51 @@ export async function getBackendForAmbiguousLocation(
474474
});
475475
return backendsByLocation.get(location)!;
476476
}
477+
478+
/**
479+
* Fetches a backend from the server. If there are multiple backends with the name, it will fetch the first
480+
* in the list and warn the user that there are other backends with the same name that need to be deleted. If
481+
* the force option is specified nad multiple backends have the same name, it throws an error.
482+
*/
483+
export async function getBackend(
484+
projectId: string,
485+
backendId: string,
486+
force?: boolean,
487+
): Promise<apphosting.Backend> {
488+
// TODO: call apphosting.getBackend() once all duplicate named backends have been deleted.
489+
let { unreachable, backends } = await apphosting.listBackends(projectId, "-");
490+
backends = backends.filter(
491+
(backend) => apphosting.parseBackendName(backend.name).id === backendId,
492+
);
493+
if (backends.length > 0) {
494+
if (backends.length > 1) {
495+
logWarning(
496+
`You have multiple backends with the same ${backendId} ID. This is no longer supported. ` +
497+
"Please delete and recreate any backends that share an ID with another backend."
498+
)
499+
if (force) {
500+
throw new FirebaseError(
501+
`Force cannot be used when multiple backends share the same ID`,
502+
);
503+
};
504+
const options = { force: force, nonInteractive: false };
505+
const confirmed = await confirm({
506+
...options,
507+
message: `Using backend ${backends[0].name} continue?`,
508+
});
509+
if (!confirmed) {
510+
throw new FirebaseError("Command aborted.");
511+
}
512+
}
513+
return backends[0];
514+
}
515+
if (unreachable && unreachable.length !== 0) {
516+
logWarning(
517+
`Backends with the following primary regions are unreachable: ${unreachable}.\n` +
518+
"If your backend is in one of these regions, please try again later."
519+
);
520+
};
521+
throw new FirebaseError(
522+
`No backend named ${backendId} found.`,
523+
);
524+
}

0 commit comments

Comments
 (0)