From 0bf69dfe27bc8c11336405b6388aa0c248a503d5 Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Fri, 10 Dec 2021 09:01:34 -0800 Subject: [PATCH] replace single usage of enable with ensure (#3941) * replace single usage of enable with ensure * add comment to enable --- CHANGELOG.md | 1 + src/ensureApiEnabled.ts | 5 ++++- src/init/features/functions/index.ts | 6 +++--- src/test/ensureApiEnabled.spec.ts | 22 +--------------------- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a35339a54a..9a6ca1697a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ - Fixes issue when installing a Firebase Extension where secrets would be created before validation. - Fixes issue with filtering on a specific storage bucket using functions in the emulator (#3893) +- Fixes check in Cloud Functions for Firebase initialization to check for API enablement before trying to enable them. (#2574) diff --git a/src/ensureApiEnabled.ts b/src/ensureApiEnabled.ts index 24273965279..cf328079ad8 100644 --- a/src/ensureApiEnabled.ts +++ b/src/ensureApiEnabled.ts @@ -40,10 +40,13 @@ export async function check( /** * Attempt to enable an API on the specified project (just once). * + * If enabling an API for a customer, prefer `ensure` which will check for the + * API first, which is a seperate permission than enabling. + * * @param projectId The project in which to enable the API. * @param apiName The name of the API e.g. `someapi.googleapis.com`. */ -export async function enable(projectId: string, apiName: string): Promise { +async function enable(projectId: string, apiName: string): Promise { try { await apiClient.post(`/projects/${projectId}/services/${apiName}:enable`); } catch (err) { diff --git a/src/init/features/functions/index.ts b/src/init/features/functions/index.ts index eca63d2cbf9..007a9345376 100644 --- a/src/init/features/functions/index.ts +++ b/src/init/features/functions/index.ts @@ -5,7 +5,7 @@ import { promptOnce } from "../../../prompt"; import { requirePermissions } from "../../../requirePermissions"; import { previews } from "../../../previews"; import { Options } from "../../../options"; -import * as ensureApiEnabled from "../../../ensureApiEnabled"; +import { ensure } from "../../../ensureApiEnabled"; module.exports = async function (setup: any, config: any, options: Options) { logger.info(); @@ -22,8 +22,8 @@ module.exports = async function (setup: any, config: any, options: Options) { if (projectId) { await requirePermissions({ ...options, project: projectId }); await Promise.all([ - ensureApiEnabled.enable(projectId, "cloudfunctions.googleapis.com"), - ensureApiEnabled.enable(projectId, "runtimeconfig.googleapis.com"), + ensure(projectId, "cloudfunctions.googleapis.com", "unused", true), + ensure(projectId, "runtimeconfig.googleapis.com", "unused", true), ]); } const choices = [ diff --git a/src/test/ensureApiEnabled.spec.ts b/src/test/ensureApiEnabled.spec.ts index 0fa9c05ca93..2ab95c18e39 100644 --- a/src/test/ensureApiEnabled.spec.ts +++ b/src/test/ensureApiEnabled.spec.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import * as nock from "nock"; -import { check, enable, ensure, POLL_SETTINGS } from "../ensureApiEnabled"; +import { check, ensure, POLL_SETTINGS } from "../ensureApiEnabled"; const FAKE_PROJECT_ID = "my_project"; const FAKE_API = "myapi.googleapis.com"; @@ -43,26 +43,6 @@ describe("ensureApiEnabled", () => { }); }); - describe("enable", () => { - before(() => { - nock.disableNetConnect(); - }); - - after(() => { - nock.enableNetConnect(); - }); - - it("should call the API to enable the API", async () => { - nock("https://serviceusage.googleapis.com") - .post(`/v1/projects/${FAKE_PROJECT_ID}/services/${FAKE_API}:enable`) - .reply(200); - - await enable(FAKE_PROJECT_ID, FAKE_API); - - expect(nock.isDone()).to.be.true; - }); - }); - describe("ensure", () => { const originalPollInterval = POLL_SETTINGS.pollInterval; const originalPollsBeforeRetry = POLL_SETTINGS.pollsBeforeRetry;