@@ -17,7 +17,7 @@ import { Backend, BackendOutputOnlyFields, API_VERSION } from "../gcp/apphosting
17
17
import { addServiceAccountToRoles } from "../gcp/resourceManager" ;
18
18
import * as iam from "../gcp/iam" ;
19
19
import { FirebaseError , getErrStatus , getError } from "../error" ;
20
- import { promptOnce } from "../prompt" ;
20
+ import { promptOnce , confirm } from "../prompt" ;
21
21
import { DEFAULT_LOCATION } from "./constants" ;
22
22
import { ensure } from "../ensureApiEnabled" ;
23
23
import * as deploymentTool from "../deploymentTool" ;
@@ -474,3 +474,51 @@ export async function getBackendForAmbiguousLocation(
474
474
} ) ;
475
475
return backendsByLocation . get ( location ) ! ;
476
476
}
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