From 700f7a2a64785e2e9bd7e56a9fe58bdd65b9c7bd Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Mon, 27 Nov 2023 20:25:48 +0900 Subject: [PATCH] Ensure API is enabled for frameworks commands. --- src/commands/frameworks-backends-create.ts | 2 ++ src/commands/frameworks-backends-delete.ts | 3 +++ src/commands/frameworks-backends-get.ts | 3 +++ src/commands/frameworks-backends-list.ts | 2 ++ src/gcp/frameworks.ts | 11 +++++++++++ 5 files changed, 21 insertions(+) diff --git a/src/commands/frameworks-backends-create.ts b/src/commands/frameworks-backends-create.ts index b31b5033820..a0e869a4181 100644 --- a/src/commands/frameworks-backends-create.ts +++ b/src/commands/frameworks-backends-create.ts @@ -3,9 +3,11 @@ import { Options } from "../options"; import { needProjectId } from "../projectUtils"; import requireInteractive from "../requireInteractive"; import { doSetup } from "../init/features/frameworks"; +import { ensureApiEnabled } from "../gcp/frameworks"; export const command = new Command("backends:create") .description("Create a backend in a Firebase project") + .before(ensureApiEnabled) .before(requireInteractive) .action(async (options: Options) => { const projectId = needProjectId(options); diff --git a/src/commands/frameworks-backends-delete.ts b/src/commands/frameworks-backends-delete.ts index 1b579dbb03b..ef4a76dcd47 100644 --- a/src/commands/frameworks-backends-delete.ts +++ b/src/commands/frameworks-backends-delete.ts @@ -7,6 +7,8 @@ import { promptOnce } from "../prompt"; import * as utils from "../utils"; import { logger } from "../logger"; import { DEFAULT_REGION, ALLOWED_REGIONS } from "../init/features/frameworks/constants"; +import { ensureApiEnabled } from "../gcp/frameworks"; + const Table = require("cli-table"); const COLUMN_LENGTH = 20; @@ -24,6 +26,7 @@ export const command = new Command("backends:delete") .option("-l, --location ", "App Backend location", "") .option("-s, --backend ", "Backend Id", "") .withForce() + .before(ensureApiEnabled) .action(async (options: Options) => { const projectId = needProjectId(options); let location = options.location as string; diff --git a/src/commands/frameworks-backends-get.ts b/src/commands/frameworks-backends-get.ts index 152b80acb16..ac059ff009d 100644 --- a/src/commands/frameworks-backends-get.ts +++ b/src/commands/frameworks-backends-get.ts @@ -4,6 +4,8 @@ import { needProjectId } from "../projectUtils"; import * as gcp from "../gcp/frameworks"; import { FirebaseError } from "../error"; import { logger } from "../logger"; +import { ensureApiEnabled } from "../gcp/frameworks"; + const Table = require("cli-table"); const COLUMN_LENGTH = 20; const TABLE_HEAD = [ @@ -18,6 +20,7 @@ export const command = new Command("backends:get") .description("Get backend details of a Firebase project") .option("-l, --location ", "App Backend location", "-") .option("-b, --backend ", "Backend Id", "") + .before(ensureApiEnabled) .action(async (options: Options) => { const projectId = needProjectId(options); const location = options.location as string; diff --git a/src/commands/frameworks-backends-list.ts b/src/commands/frameworks-backends-list.ts index 253c7d00dad..6f15c9219c9 100644 --- a/src/commands/frameworks-backends-list.ts +++ b/src/commands/frameworks-backends-list.ts @@ -5,6 +5,7 @@ import * as gcp from "../gcp/frameworks"; import { FirebaseError } from "../error"; import { logger } from "../logger"; import { bold } from "colorette"; +import { ensureApiEnabled } from "../gcp/frameworks"; const Table = require("cli-table"); const COLUMN_LENGTH = 20; @@ -12,6 +13,7 @@ const TABLE_HEAD = ["Backend Id", "Repository", "Location", "URL", "Created Date export const command = new Command("backends:list") .description("List backends of a Firebase project.") .option("-l, --location ", "App Backend location", "-") + .before(ensureApiEnabled) .action(async (options: Options) => { const projectId = needProjectId(options); const location = options.location as string; diff --git a/src/gcp/frameworks.ts b/src/gcp/frameworks.ts index edc404150f1..af4ce113eee 100644 --- a/src/gcp/frameworks.ts +++ b/src/gcp/frameworks.ts @@ -1,6 +1,9 @@ import { Client } from "../apiv2"; +import { needProjectId } from "../projectUtils"; import { frameworksOrigin } from "../api"; +import { ensure } from "../ensureApiEnabled"; +export const API_HOST = new URL(frameworksOrigin).host; export const API_VERSION = "v1alpha"; const client = new Client({ @@ -162,3 +165,11 @@ export async function createBuild( return res.body; } + +/** + * Ensure that Frameworks API is enabled on the project. + */ +export async function ensureApiEnabled(options: any): Promise { + const projectId = needProjectId(options); + return await ensure(projectId, API_HOST, "frameworks", true); +}