Skip to content

Commit 8228367

Browse files
authored
Switching lots of API name magic strings to <product>Origin (#6805)
1 parent 41922fe commit 8228367

File tree

15 files changed

+149
-144
lines changed

15 files changed

+149
-144
lines changed

src/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export const secretManagerOrigin = utils.envOverride(
189189
"CLOUD_SECRET_MANAGER_URL",
190190
"https://secretmanager.googleapis.com",
191191
);
192+
export const computeOrigin = utils.envOverride("COMPUTE_URL", "https://compute.googleapis.com");
192193
export const githubClientId = utils.envOverride("GITHUB_CLIENT_ID", "89cf50f02ac6aaed3484");
193194
export const githubClientSecret = utils.envOverride(
194195
"GITHUB_CLIENT_SECRET",

src/deploy/extensions/v2FunctionHelper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FirebaseError } from "../../error";
55
import { ensure } from "../../ensureApiEnabled";
66
import * as planner from "./planner";
77
import { needProjectId } from "../../projectUtils";
8+
import { computeOrigin } from "../../api";
89

910
const SERVICE_AGENT_ROLE = "roles/eventarc.eventReceiver";
1011

@@ -21,7 +22,7 @@ export async function checkSpecForV2Functions(i: planner.InstanceSpec): Promise<
2122
*/
2223
export async function ensureNecessaryV2ApisAndRoles(options: any) {
2324
const projectId = needProjectId(options);
24-
await ensure(projectId, "compute.googleapis.com", "extensions", options.markdown);
25+
await ensure(projectId, computeOrigin, "extensions", options.markdown);
2526
await ensureComputeP4SARole(projectId);
2627
}
2728

src/deploy/functions/ensure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { logLabeledBullet, logLabeledSuccess } from "../../utils";
88
import { ensureServiceAgentRole } from "../../gcp/secretManager";
99
import { getFirebaseProject } from "../../management/projects";
1010
import { assertExhaustive } from "../../functional";
11+
import { cloudbuildOrigin } from "../../api";
1112
import * as backend from "./backend";
1213

1314
const FAQ_URL = "https://firebase.google.com/support/faq#functions-runtime";
14-
const CLOUD_BUILD_API = "cloudbuild.googleapis.com";
1515

1616
const metadataCallCache: Map<string, Promise<FirebaseProjectMetadata>> = new Map();
1717

@@ -74,7 +74,7 @@ function isPermissionError(e: { context?: { body?: { error?: { status?: string }
7474
*/
7575
export async function cloudBuildEnabled(projectId: string): Promise<void> {
7676
try {
77-
await ensure(projectId, CLOUD_BUILD_API, "functions");
77+
await ensure(projectId, cloudbuildOrigin, "functions");
7878
} catch (e: any) {
7979
if (isBillingError(e)) {
8080
throw nodeBillingError(projectId);

src/deploy/functions/prepare.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import * as functionsEnv from "../../functions/env";
99
import * as runtimes from "./runtimes";
1010
import * as validate from "./validate";
1111
import * as ensure from "./ensure";
12+
import {
13+
functionsOrigin,
14+
artifactRegistryDomain,
15+
runtimeconfigOrigin,
16+
cloudRunApiOrigin,
17+
eventarcOrigin,
18+
pubsubOrigin,
19+
storageOrigin,
20+
} from "../../api";
1221
import { Options } from "../../options";
1322
import {
1423
EndpointFilter,
@@ -62,15 +71,10 @@ export async function prepare(
6271

6372
// ===Phase 0. Check that minimum APIs required for function deploys are enabled.
6473
const checkAPIsEnabled = await Promise.all([
65-
ensureApiEnabled.ensure(projectId, "cloudfunctions.googleapis.com", "functions"),
66-
ensureApiEnabled.check(
67-
projectId,
68-
"runtimeconfig.googleapis.com",
69-
"runtimeconfig",
70-
/* silent=*/ true,
71-
),
74+
ensureApiEnabled.ensure(projectId, functionsOrigin, "functions"),
75+
ensureApiEnabled.check(projectId, runtimeconfigOrigin, "runtimeconfig", /* silent=*/ true),
7276
ensure.cloudBuildEnabled(projectId),
73-
ensureApiEnabled.ensure(projectId, "artifactregistry.googleapis.com", "artifactregistry"),
77+
ensureApiEnabled.ensure(projectId, artifactRegistryDomain, "artifactregistry"),
7478
]);
7579

7680
// Get the Firebase Config, and set it on each function in the deployment.
@@ -234,12 +238,7 @@ export async function prepare(
234238
// Note: Some of these are premium APIs that require billing to be enabled.
235239
// We'd eventually have to add special error handling for billing APIs, but
236240
// enableCloudBuild is called above and has this special casing already.
237-
const V2_APIS = [
238-
"run.googleapis.com",
239-
"eventarc.googleapis.com",
240-
"pubsub.googleapis.com",
241-
"storage.googleapis.com",
242-
];
241+
const V2_APIS = [cloudRunApiOrigin, eventarcOrigin, pubsubOrigin, storageOrigin];
243242
const enablements = V2_APIS.map((api) => {
244243
return ensureApiEnabled.ensure(context.projectId, api, "functions");
245244
});

src/ensureApiEnabled.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ const apiClient = new Client({
1919
/**
2020
* Check if the specified API is enabled.
2121
* @param projectId The project on which to check enablement.
22-
* @param apiName The name of the API e.g. `someapi.googleapis.com`.
22+
* @param apiUri The name of the API e.g. `someapi.googleapis.com`.
2323
* @param prefix The logging prefix to use when printing messages about enablement.
2424
* @param silent Whether or not to print log messages.
2525
*/
2626
export async function check(
2727
projectId: string,
28-
apiName: string,
28+
apiUri: string,
2929
prefix: string,
3030
silent = false,
3131
): Promise<boolean> {
32+
const apiName = apiUri.startsWith("http") ? new URL(apiUri).hostname : apiUri;
3233
const res = await apiClient.get<{ state: string }>(`/projects/${projectId}/services/${apiName}`, {
3334
headers: { "x-goog-quota-user": `projects/${projectId}` },
3435
skipLog: { resBody: true },
@@ -148,27 +149,28 @@ async function enableApiWithRetries(
148149
* Check if an API is enabled on a project, try to enable it if not with polling and retries.
149150
*
150151
* @param projectId The project on which to check enablement.
151-
* @param apiName The name of the API e.g. `someapi.googleapis.com`.
152+
* @param apiUri The name of the API e.g. `someapi.googleapis.com`.
152153
* @param prefix The logging prefix to use when printing messages about enablement.
153154
* @param silent Whether or not to print log messages.
154155
*/
155156
export async function ensure(
156157
projectId: string,
157-
apiName: string,
158+
apiUri: string,
158159
prefix: string,
159160
silent = false,
160161
): Promise<void> {
162+
const hostname = apiUri.startsWith("http") ? new URL(apiUri).hostname : apiUri;
161163
if (!silent) {
162-
utils.logLabeledBullet(prefix, `ensuring required API ${bold(apiName)} is enabled...`);
164+
utils.logLabeledBullet(prefix, `ensuring required API ${bold(hostname)} is enabled...`);
163165
}
164-
const isEnabled = await check(projectId, apiName, prefix, silent);
166+
const isEnabled = await check(projectId, hostname, prefix, silent);
165167
if (isEnabled) {
166168
return;
167169
}
168170
if (!silent) {
169-
utils.logLabeledWarning(prefix, `missing required API ${bold(apiName)}. Enabling now...`);
171+
utils.logLabeledWarning(prefix, `missing required API ${bold(hostname)}. Enabling now...`);
170172
}
171-
return enableApiWithRetries(projectId, apiName, prefix, silent);
173+
return enableApiWithRetries(projectId, hostname, prefix, silent);
172174
}
173175

174176
/**

src/extensions/extensionsHelper.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ marked.setOptions({
1313
renderer: new TerminalRenderer(),
1414
});
1515

16-
import { storageOrigin } from "../api";
16+
import { extensionsOrigin, extensionsPublisherOrigin, storageOrigin } from "../api";
1717
import { archiveDirectory } from "../archiveDirectory";
1818
import { convertOfficialExtensionsToList } from "./utils";
1919
import { getFirebaseConfig } from "../functionsConfig";
@@ -488,25 +488,15 @@ export async function ensureExtensionsApiEnabled(options: any): Promise<void> {
488488
if (!projectId) {
489489
return;
490490
}
491-
return await ensure(
492-
projectId,
493-
"firebaseextensions.googleapis.com",
494-
"extensions",
495-
options.markdown,
496-
);
491+
return await ensure(projectId, extensionsOrigin, "extensions", options.markdown);
497492
}
498493

499494
export async function ensureExtensionsPublisherApiEnabled(options: any): Promise<void> {
500495
const projectId = getProjectId(options);
501496
if (!projectId) {
502497
return;
503498
}
504-
return await ensure(
505-
projectId,
506-
"firebaseextensionspublisher.googleapis.com",
507-
"extensions",
508-
options.markdown,
509-
);
499+
return await ensure(projectId, extensionsPublisherOrigin, "extensions", options.markdown);
510500
}
511501

512502
/**

src/extensions/secretsUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { needProjectId } from "../projectUtils";
55
import { ExtensionInstance, ExtensionSpec, ParamType } from "./types";
66
import * as secretManagerApi from "../gcp/secretManager";
77
import { logger } from "../logger";
8+
import { secretManagerOrigin } from "../api";
89

910
export const SECRET_LABEL = "firebase-extensions-managed";
1011
export const SECRET_ROLE = "secretmanager.secretAccessor";
1112

1213
export async function ensureSecretManagerApiEnabled(options: any): Promise<void> {
1314
const projectId = needProjectId(options);
14-
return await ensure(projectId, "secretmanager.googleapis.com", "extensions", options.markdown);
15+
return await ensure(projectId, secretManagerOrigin, "extensions", options.markdown);
1516
}
1617

1718
export function usesSecrets(spec: ExtensionSpec): boolean {

src/functions/secrets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as gcfV1 from "../gcp/cloudfunctions";
44
import * as gcfV2 from "../gcp/cloudfunctionsv2";
55
import * as backend from "../deploy/functions/backend";
66
import * as ensureApiEnabled from "../ensureApiEnabled";
7-
import { functionsOrigin, functionsV2Origin } from "../api";
7+
import { functionsOrigin, functionsV2Origin, secretManagerOrigin } from "../api";
88
import {
99
createSecret,
1010
destroySecretVersion,
@@ -78,7 +78,7 @@ function toUpperSnakeCase(key: string): string {
7878
*/
7979
export function ensureApi(options: any): Promise<void> {
8080
const projectId = needProjectId(options);
81-
return ensureApiEnabled.ensure(projectId, "secretmanager.googleapis.com", "secretmanager", true);
81+
return ensureApiEnabled.ensure(projectId, secretManagerOrigin, "secretmanager", true);
8282
}
8383

8484
/**

src/gcp/apphosting.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as deploymentTool from "../deploymentTool";
77
import { FirebaseError } from "../error";
88
import { DeepOmit, RecursiveKeyOf, assertImplements } from "../metaprogramming";
99

10-
export const API_HOST = new URL(apphostingOrigin).host;
1110
export const API_VERSION = "v1alpha";
1211

1312
export const client = new Client({
@@ -503,7 +502,7 @@ export async function listLocations(projectId: string): Promise<Location[]> {
503502
*/
504503
export async function ensureApiEnabled(options: any): Promise<void> {
505504
const projectId = needProjectId(options);
506-
return await ensure(projectId, API_HOST, "app hosting", true);
505+
return await ensure(projectId, apphostingOrigin, "app hosting", true);
507506
}
508507

509508
/**

src/gcp/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ interface StorageServiceAccountResponse {
149149
}
150150

151151
export async function getDefaultBucket(projectId: string): Promise<string> {
152-
await ensure(projectId, "firebasestorage.googleapis.com", "storage", false);
152+
await ensure(projectId, firebaseStorageOrigin, "storage", false);
153153
try {
154154
const localAPIClient = new Client({ urlPrefix: firebaseStorageOrigin, apiVersion: "v1alpha" });
155155
const response = await localAPIClient.get<GetDefaultBucketResponse>(

0 commit comments

Comments
 (0)