Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace single usage of enable with ensure #3941

Merged
merged 4 commits into from Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add these last 2 args? Doesn't seem like they should be here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least the 3rd is required. It's "label" (for printing messages) and "silent" (again, printing messages). Setting them to have the same behavior as it does today.

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