Skip to content

Commit

Permalink
replace single usage of enable with ensure (#3941)
Browse files Browse the repository at this point in the history
* replace single usage of enable with ensure

* add comment to enable
  • Loading branch information
bkendall committed Dec 10, 2021
1 parent f777a6e commit 0bf69df
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
1 change: 1 addition & 0 deletions 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)
5 changes: 4 additions & 1 deletion src/ensureApiEnabled.ts
Expand Up @@ -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<void> {
async function enable(projectId: string, apiName: string): Promise<void> {
try {
await apiClient.post(`/projects/${projectId}/services/${apiName}:enable`);
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions src/init/features/functions/index.ts
Expand Up @@ -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();
Expand All @@ -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 = [
Expand Down
22 changes: 1 addition & 21 deletions 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";
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0bf69df

Please sign in to comment.