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

Svn main 001 rem #6492

Merged
merged 26 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a02cc2c
Change name from stack to backend
svnsairam Nov 1, 2023
000a270
Change name from stack to backend
svnsairam Nov 1, 2023
12e8ae5
changed file names
svnsairam Nov 1, 2023
8a43326
changed file names in experiments
svnsairam Nov 1, 2023
3fb49b4
changed name
svnsairam Nov 1, 2023
272d0e0
lower case backend
svnsairam Nov 1, 2023
e0696f5
Resolved merge conflicts
svnsairam Nov 1, 2023
7ead930
Changed stack to backend
svnsairam Nov 1, 2023
38449bc
change stack to backend in index file
svnsairam Nov 1, 2023
270a0a8
List stacks in table
svnsairam Nov 1, 2023
5884f2b
Backends for project message
svnsairam Nov 1, 2023
f264a59
resolve merge conflicts
svnsairam Nov 1, 2023
d7ba0a5
removed linter errors
svnsairam Nov 1, 2023
28fb6e0
changed table head color to green
svnsairam Nov 1, 2023
5a861a9
test method stack to backend
svnsairam Nov 1, 2023
fbdfb27
merge conflicts
svnsairam Nov 1, 2023
274ed43
Changed stack to backend (#6488)
svnsairam Nov 1, 2023
b6d3fc0
Merge branch 'master' into svn-main-001-rem
svnsairam Nov 1, 2023
a111ece
Merge branch 'master' into svn-main-001-rem
svnsairam Nov 2, 2023
88455f7
Merge remote-tracking branch 'origin/master' into svn-main-001-rem
svnsairam Nov 2, 2023
5caa352
Merge branch 'svn-main-001-rem' of https://github.com/firebase/fireba…
svnsairam Nov 2, 2023
16e5a0c
Change Stack objects to Backend (#6490)
svnsairam Nov 6, 2023
8387738
Merge remote-tracking branch 'origin/master' into svn-main-001-rem
svnsairam Nov 7, 2023
47f91fa
Merge branch 'svn-main-001-rem' of https://github.com/firebase/fireba…
svnsairam Nov 7, 2023
03ca951
Merge branch 'master' into svn-main-001-rem
svnsairam Nov 7, 2023
95e6d8c
Merge branch 'svn-main-001-rem' of https://github.com/firebase/fireba…
svnsairam Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/gcp/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
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;
Expand All @@ -27,7 +27,7 @@
uri: string;
}

export type StackOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri";
export type BackendOutputOnlyFields = "name" | "createTime" | "updateTime" | "uri";

export interface Build {
name: string;
Expand All @@ -50,7 +50,7 @@
interface Status {
code: number;
message: string;
details: any[];

Check warning on line 53 in src/gcp/frameworks.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type
}

interface CodebaseSource {
Expand All @@ -77,26 +77,26 @@
done: boolean;
// oneof result
error?: Status;
response?: any;

Check warning on line 80 in src/gcp/frameworks.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type
// end oneof result
}

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<Stack, StackOutputOnlyFields>,
backendReqBoby: Omit<Backend, BackendOutputOnlyFields>,
backendId: string
): Promise<Operation> {
const res = await client.post<Omit<Stack, StackOutputOnlyFields>, Operation>(
const res = await client.post<Omit<Backend, BackendOutputOnlyFields>, Operation>(
`projects/${projectId}/locations/${location}/backends`,
stackReqBoby,
backendReqBoby,
{ queryParams: { backendId } }
);

Expand All @@ -110,9 +110,9 @@
projectId: string,
location: string,
backendId: string
): Promise<Stack> {
): Promise<Backend> {
const name = `projects/${projectId}/locations/${location}/backends/${backendId}`;
const res = await client.get<Stack>(name);
const res = await client.get<Backend>(name);

return res.body;
}
Expand Down Expand Up @@ -150,12 +150,12 @@
export async function createBuild(
projectId: string,
location: string,
stackId: string,
backendId: string,
buildInput: Omit<Build, BuildOutputOnlyFields>
): Promise<Operation> {
const buildId = buildInput.name;
const res = await client.post<Omit<Build, BuildOutputOnlyFields>, Operation>(
`projects/${projectId}/locations/${location}/backends/${stackId}/builds`,
`projects/${projectId}/locations/${location}/backends/${backendId}/builds`,
buildInput,
{ queryParams: { buildId } }
);
Expand Down
76 changes: 44 additions & 32 deletions src/init/features/frameworks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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";
Expand All @@ -27,8 +27,8 @@
/**
* Setup new frameworks project.
*/
export async function doSetup(setup: any, projectId: string): Promise<void> {

Check warning on line 30 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type
setup.frameworks = {};

Check warning on line 31 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .frameworks on an `any` value

utils.logBullet("First we need a few details to create your service.");

Expand All @@ -39,7 +39,7 @@
default: "acme-inc-web",
message: "Create a name for your service [1-30 characters]",
},
setup.frameworks

Check warning on line 42 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe argument of type `any` assigned to a parameter of type `Options | undefined`

Check warning on line 42 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .frameworks on an `any` value
);

await promptOnce(
Expand All @@ -52,10 +52,10 @@
`(${clc.yellow("info")}: Your region determines where your backend is located):\n`,
choices: ALLOWED_REGIONS,
},
setup.frameworks

Check warning on line 55 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe argument of type `any` assigned to a parameter of type `Options | undefined`

Check warning on line 55 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .frameworks on an `any` value
);

utils.logSuccess(`Region set to ${setup.frameworks.region}.`);

Check warning on line 58 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Invalid type "any" of template literal expression

Check warning on line 58 in src/init/features/frameworks/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .frameworks on an `any` value

logger.info(clc.bold(`\n${clc.white("===")} Deploy Setup`));

Expand All @@ -70,13 +70,13 @@
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<Stack, StackOutputOnlyFields> {
function toBackend(cloudBuildConnRepo: Repository): Omit<Backend, BackendOutputOnlyFields> {
return {
codebase: {
repository: `${cloudBuildConnRepo.name}`,
Expand All @@ -87,48 +87,60 @@
}

/**
* Creates stack if it doesn't exist.
* Creates backend if it doesn't exist.
*/
export async function getOrCreateStack(projectId: string, setup: any): Promise<Stack | undefined> {
export async function getOrCreateBackend(
projectId: string,
setup: any
): Promise<Backend | undefined> {
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}`
);
}
}

return undefined;
}

async function getExistingStack(projectId: string, setup: any, location: string): Promise<Stack> {
let stack = await gcp.getBackend(projectId, location, setup.frameworks.serviceName);
while (stack) {
async function getExistingBackend(
projectId: string,
setup: any,
location: string
): Promise<Backend> {
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(
{
Expand All @@ -139,28 +151,28 @@
},
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<Stack, StackOutputOnlyFields>,
stackId: string
): Promise<Stack> {
const op = await gcp.createStack(projectId, location, stackReqBoby, stackId);
const stack = await poller.pollOperation<Stack>({
backendReqBoby: Omit<Backend, BackendOutputOnlyFields>,
backendId: string
): Promise<Backend> {
const op = await gcp.createBackend(projectId, location, backendReqBoby, backendId);
const backend = await poller.pollOperation<Backend>({
...frameworksPollerOptions,
pollerName: `create-${projectId}-${location}-${stackId}`,
pollerName: `create-${projectId}-${location}-${backendId}`,
operationResourceName: op.name,
});

return stack;
return backend;
}
2 changes: 1 addition & 1 deletion src/init/features/frameworks/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 34 additions & 34 deletions src/test/init/frameworks/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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",
Expand All @@ -48,8 +48,8 @@ describe("operationsConverter", () => {
const setup = {
frameworks: {
region: location,
serviceName: stackId,
existingStack: true,
serviceName: backendId,
existingBackend: true,
deployMethod: "github",
},
};
Expand All @@ -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);
});
});
});
Loading