diff --git a/src/gcp/frameworks.ts b/src/gcp/frameworks.ts index 0b753f84dbe..edc404150f1 100644 --- a/src/gcp/frameworks.ts +++ b/src/gcp/frameworks.ts @@ -16,8 +16,8 @@ interface Codebase { rootDirectory: string; } -/** A Stack, the primary resource of Frameworks. */ -export interface Stack { +/** A Backend, the primary resource of Frameworks. */ +export interface Backend { name: string; mode?: string; codebase: Codebase; @@ -27,7 +27,7 @@ export interface Stack { uri: string; } -export type StackOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri"; +export type BackendOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri"; export interface Build { name: string; @@ -82,21 +82,21 @@ export interface Operation { } export interface ListBackendsResponse { - backends: Stack[]; + backends: Backend[]; } /** - * Creates a new Stack in a given project and location. + * Creates a new Backend in a given project and location. */ -export async function createStack( +export async function createBackend( projectId: string, location: string, - stackReqBoby: Omit, + backendReqBoby: Omit, backendId: string ): Promise { - const res = await client.post, Operation>( + const res = await client.post, Operation>( `projects/${projectId}/locations/${location}/backends`, - stackReqBoby, + backendReqBoby, { queryParams: { backendId } } ); @@ -110,9 +110,9 @@ export async function getBackend( projectId: string, location: string, backendId: string -): Promise { +): Promise { const name = `projects/${projectId}/locations/${location}/backends/${backendId}`; - const res = await client.get(name); + const res = await client.get(name); return res.body; } @@ -150,12 +150,12 @@ export async function deleteBackend( export async function createBuild( projectId: string, location: string, - stackId: string, + backendId: string, buildInput: Omit ): Promise { const buildId = buildInput.name; const res = await client.post, Operation>( - `projects/${projectId}/locations/${location}/backends/${stackId}/builds`, + `projects/${projectId}/locations/${location}/backends/${backendId}/builds`, buildInput, { queryParams: { buildId } } ); diff --git a/src/init/features/frameworks/index.ts b/src/init/features/frameworks/index.ts index f92b7054290..c78df64d59a 100644 --- a/src/init/features/frameworks/index.ts +++ b/src/init/features/frameworks/index.ts @@ -9,7 +9,7 @@ import { ALLOWED_DEPLOY_METHODS, } from "./constants"; import * as repo from "./repo"; -import { Stack, StackOutputOnlyFields } from "../../../gcp/frameworks"; +import { Backend, BackendOutputOnlyFields } from "../../../gcp/frameworks"; import { Repository } from "../../../gcp/cloudbuild"; import * as poller from "../../../operation-poller"; import { frameworksOrigin } from "../../../api"; @@ -70,13 +70,13 @@ export async function doSetup(setup: any, projectId: string): Promise { setup.frameworks ); - const stack: Stack | undefined = await getOrCreateStack(projectId, setup); - if (stack) { - utils.logSuccess(`Successfully created a stack: ${stack.name}`); + const backend: Backend | undefined = await getOrCreateBackend(projectId, setup); + if (backend) { + utils.logSuccess(`Successfully created a backend: ${backend.name}`); } } -function toStack(cloudBuildConnRepo: Repository): Omit { +function toBackend(cloudBuildConnRepo: Repository): Omit { return { codebase: { repository: `${cloudBuildConnRepo.name}`, @@ -87,24 +87,32 @@ function toStack(cloudBuildConnRepo: Repository): Omit { +export async function getOrCreateBackend( + projectId: string, + setup: any +): Promise { const location: string = setup.frameworks.region; const deployMethod: string = setup.frameworks.deployMethod; try { - return await getExistingStack(projectId, setup, location); + return await getExistingBackend(projectId, setup, location); } catch (err: unknown) { if ((err as FirebaseError).status === 404) { - logger.info("Creating new stack."); + logger.info("Creating new backend."); if (deployMethod === "github") { const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location); - const stackDetails = toStack(cloudBuildConnRepo); - return await createStack(projectId, location, stackDetails, setup.frameworks.serviceName); + const backendDetails = toBackend(cloudBuildConnRepo); + return await createBackend( + projectId, + location, + backendDetails, + setup.frameworks.serviceName + ); } } else { throw new FirebaseError( - `Failed to get or create a stack using the given initialization details: ${err}` + `Failed to get or create a backend using the given initialization details: ${err}` ); } } @@ -112,23 +120,27 @@ export async function getOrCreateStack(projectId: string, setup: any): Promise { - let stack = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); - while (stack) { +async function getExistingBackend( + projectId: string, + setup: any, + location: string +): Promise { + let backend = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); + while (backend) { setup.frameworks.serviceName = undefined; await promptOnce( { - name: "existingStack", + name: "existingBackend", type: "confirm", default: true, message: - "A stack already exists for the given serviceName, do you want to use existing stack? (yes/no)", + "A backend already exists for the given serviceName, do you want to use existing backend? (yes/no)", }, setup.frameworks ); - if (setup.frameworks.existingStack) { - logger.info("Using the existing stack."); - return stack; + if (setup.frameworks.existingBackend) { + logger.info("Using the existing backend."); + return backend; } await promptOnce( { @@ -139,28 +151,28 @@ async function getExistingStack(projectId: string, setup: any, location: string) }, setup.frameworks ); - stack = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); - setup.frameworks.existingStack = undefined; + backend = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); + setup.frameworks.existingBackend = undefined; } - return stack; + return backend; } /** - * Creates Stack object from long running operations. + * Creates backend object from long running operations. */ -export async function createStack( +export async function createBackend( projectId: string, location: string, - stackReqBoby: Omit, - stackId: string -): Promise { - const op = await gcp.createStack(projectId, location, stackReqBoby, stackId); - const stack = await poller.pollOperation({ + backendReqBoby: Omit, + backendId: string +): Promise { + const op = await gcp.createBackend(projectId, location, backendReqBoby, backendId); + const backend = await poller.pollOperation({ ...frameworksPollerOptions, - pollerName: `create-${projectId}-${location}-${stackId}`, + pollerName: `create-${projectId}-${location}-${backendId}`, operationResourceName: op.name, }); - return stack; + return backend; } diff --git a/src/init/features/frameworks/repo.ts b/src/init/features/frameworks/repo.ts index 1cbe8169f7a..425ef154835 100644 --- a/src/init/features/frameworks/repo.ts +++ b/src/init/features/frameworks/repo.ts @@ -45,7 +45,7 @@ function generateConnectionId(location: string): string { } /** - * Prompts the user to link their stack to a GitHub repository. + * Prompts the user to link their backend to a GitHub repository. */ export async function linkGitHubRepository( projectId: string, diff --git a/src/test/init/frameworks/index.spec.ts b/src/test/init/frameworks/index.spec.ts index 01096de69cd..b6f758988b8 100644 --- a/src/test/init/frameworks/index.spec.ts +++ b/src/test/init/frameworks/index.spec.ts @@ -4,23 +4,23 @@ import { expect } from "chai"; import * as gcp from "../../../gcp/frameworks"; import * as repo from "../../../init/features/frameworks/repo"; import * as poller from "../../../operation-poller"; -import { createStack, getOrCreateStack } from "../../../init/features/frameworks/index"; +import { createBackend, getOrCreateBackend } from "../../../init/features/frameworks/index"; import { FirebaseError } from "../../../error"; describe("operationsConverter", () => { const sandbox: sinon.SinonSandbox = sinon.createSandbox(); let pollOperationStub: sinon.SinonStub; - let createStackStub: sinon.SinonStub; - let getStackStub: sinon.SinonStub; + let createBackendStub: sinon.SinonStub; + let getBackendStub: sinon.SinonStub; let linkGitHubRepositoryStub: sinon.SinonStub; beforeEach(() => { pollOperationStub = sandbox .stub(poller, "pollOperation") .throws("Unexpected pollOperation call"); - createStackStub = sandbox.stub(gcp, "createStack").throws("Unexpected createStack call"); - getStackStub = sandbox.stub(gcp, "getBackend").throws("Unexpected getBackend call"); + createBackendStub = sandbox.stub(gcp, "createBackend").throws("Unexpected createBackend call"); + getBackendStub = sandbox.stub(gcp, "getBackend").throws("Unexpected getBackend call"); linkGitHubRepositoryStub = sandbox .stub(repo, "linkGitHubRepository") .throws("Unexpected getBackend call"); @@ -30,16 +30,16 @@ describe("operationsConverter", () => { sandbox.verifyAndRestore(); }); - describe("createStack", () => { + describe("createBackend", () => { const projectId = "projectId"; const location = "us-central1"; - const stackId = "stackId"; + const backendId = "backendId"; const op = { - name: `projects/${projectId}/locations/${location}/stacks/${stackId}`, + name: `projects/${projectId}/locations/${location}/backends/${backendId}`, done: true, }; - const completeStack = { - name: `projects/${projectId}/locations/${location}/stacks/${stackId}`, + const completeBackend = { + name: `projects/${projectId}/locations/${location}/backends/${backendId}`, labels: {}, createTime: "0", updateTime: "1", @@ -48,8 +48,8 @@ describe("operationsConverter", () => { const setup = { frameworks: { region: location, - serviceName: stackId, - existingStack: true, + serviceName: backendId, + existingBackend: true, deployMethod: "github", }, }; @@ -59,46 +59,46 @@ describe("operationsConverter", () => { createTime: "0", updateTime: "1", }; - const stackInput = { + const backendInput = { codebase: { repository: cloudBuildConnRepo.name, rootDirectory: "/", }, labels: {}, }; - it("should createStack", async () => { - createStackStub.resolves(op); - pollOperationStub.resolves(completeStack); + it("should createBackend", async () => { + createBackendStub.resolves(op); + pollOperationStub.resolves(completeBackend); - await createStack(projectId, location, stackInput, stackId); + await createBackend(projectId, location, backendInput, backendId); - expect(createStackStub).to.be.calledWith(projectId, location, stackInput); + expect(createBackendStub).to.be.calledWith(projectId, location, backendInput); }); - it("should return a stack, if user wants use the exiting stack", async () => { - getStackStub.resolves(completeStack); + it("should return a backend, if user wants use the exiting backend", async () => { + getBackendStub.resolves(completeBackend); - const result = await getOrCreateStack("projectId", setup); + const result = await getOrCreateBackend("projectId", setup); - expect(result).to.deep.equal(completeStack); - expect(getStackStub.calledOnceWithExactly(projectId, location, stackId)).to.be.true; + expect(result).to.deep.equal(completeBackend); + expect(getBackendStub.calledOnceWithExactly(projectId, location, backendId)).to.be.true; }); - it("should create a new stack, if stack doesn't exist", async () => { - const newStackId = "newStackId"; - const newPath = `projects/${projectId}/locations/${location}/stacks/${newStackId}`; - setup.frameworks.serviceName = newStackId; + it("should create a new backend, if backend doesn't exist", async () => { + const newBackendId = "newBackendId"; + const newPath = `projects/${projectId}/locations/${location}/backends/${newBackendId}`; + setup.frameworks.serviceName = newBackendId; op.name = newPath; - completeStack.name = newPath; - getStackStub.throws(new FirebaseError("error", { status: 404 })); + completeBackend.name = newPath; + getBackendStub.throws(new FirebaseError("error", { status: 404 })); linkGitHubRepositoryStub.resolves(cloudBuildConnRepo); - createStackStub.resolves(op); - pollOperationStub.resolves(completeStack); + createBackendStub.resolves(op); + pollOperationStub.resolves(completeBackend); - const result = await getOrCreateStack(projectId, setup); + const result = await getOrCreateBackend(projectId, setup); - expect(result).to.deep.equal(completeStack); - expect(createStackStub).to.be.calledWith(projectId, location, stackInput); + expect(result).to.deep.equal(completeBackend); + expect(createBackendStub).to.be.calledWith(projectId, location, backendInput); }); }); });