From 3b4a7e3de2dcdb7e31faf61b9c8d942821113770 Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Thu, 9 Dec 2021 13:39:03 -0800 Subject: [PATCH 1/2] replace single usage of enable with ensure --- CHANGELOG.md | 1 + src/ensureApiEnabled.ts | 2 +- src/init/features/functions/index.ts | 6 +++--- src/test/ensureApiEnabled.spec.ts | 22 +--------------------- 4 files changed, 6 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a7e12219b7..c72fc498604 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ - 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..efbe90613e3 100644 --- a/src/ensureApiEnabled.ts +++ b/src/ensureApiEnabled.ts @@ -43,7 +43,7 @@ export async function check( * @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; From a7f2b56426a74c666e4d320fad0730f4f40ae254 Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Thu, 9 Dec 2021 13:42:02 -0800 Subject: [PATCH 2/2] add comment to enable --- src/ensureApiEnabled.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ensureApiEnabled.ts b/src/ensureApiEnabled.ts index efbe90613e3..cf328079ad8 100644 --- a/src/ensureApiEnabled.ts +++ b/src/ensureApiEnabled.ts @@ -40,6 +40,9 @@ 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`. */