Skip to content

Commit 6066511

Browse files
committed
Refactor createBackend and other utility functions.
1 parent 63bb0bd commit 6066511

File tree

2 files changed

+55
-113
lines changed

2 files changed

+55
-113
lines changed

src/init/features/frameworks/index.ts

Lines changed: 46 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as clc from "colorette";
22
import * as repo from "./repo";
33
import * as poller from "../../../operation-poller";
44
import * as gcp from "../../../gcp/frameworks";
5-
import { logBullet, logSuccess } from "../../../utils";
5+
import { logBullet, logSuccess, logWarning } from "../../../utils";
66
import { frameworksOrigin } from "../../../api";
77
import { Backend, BackendOutputOnlyFields } from "../../../gcp/frameworks";
88
import { Repository } from "../../../gcp/cloudbuild";
@@ -26,32 +26,40 @@ export async function doSetup(setup: any, projectId: string): Promise<void> {
2626

2727
logBullet("First we need a few details to create your backend.");
2828

29-
await promptOnce(
30-
{
31-
name: "serviceName",
29+
const location = await promptOnce({
30+
name: "region",
31+
type: "list",
32+
default: DEFAULT_REGION,
33+
message:
34+
"Please select a region " +
35+
`(${clc.yellow("info")}: Your region determines where your backend is located):\n`,
36+
choices: ALLOWED_REGIONS,
37+
});
38+
39+
logSuccess(`Region set to ${location}.\n`);
40+
41+
let backendId: string;
42+
while (true) {
43+
backendId = await promptOnce({
44+
name: "backendId",
3245
type: "input",
3346
default: "acme-inc-web",
3447
message: "Create a name for your backend [1-30 characters]",
35-
},
36-
setup.frameworks
37-
);
38-
39-
await promptOnce(
40-
{
41-
name: "region",
42-
type: "list",
43-
default: DEFAULT_REGION,
44-
message:
45-
"Please select a region " +
46-
`(${clc.yellow("info")}: Your region determines where your backend is located):\n`,
47-
choices: ALLOWED_REGIONS,
48-
},
49-
setup.frameworks
50-
);
51-
52-
logSuccess(`Region set to ${setup.frameworks.region}.\n`);
53-
54-
const backend: Backend | undefined = await getOrCreateBackend(projectId, setup);
48+
});
49+
try {
50+
await gcp.getBackend(projectId, location, backendId);
51+
} catch (err: any) {
52+
if (err.status === 404) {
53+
break;
54+
}
55+
throw new FirebaseError(
56+
`Failed to check if backend with id ${backendId} already exists in ${location}`,
57+
{ original: err }
58+
);
59+
}
60+
logWarning(`Backend with id ${backendId} already exists in ${location}`);
61+
}
62+
const backend: Backend | undefined = await onboardBackend(projectId, location, backendId);
5563

5664
if (backend) {
5765
logSuccess(`Successfully created backend:\n\t${backend.name}`);
@@ -74,75 +82,23 @@ function toBackend(cloudBuildConnRepo: Repository): Omit<Backend, BackendOutputO
7482
}
7583

7684
/**
77-
* Creates backend if it doesn't exist.
85+
* Walks thorugh creating a new backend.
7886
*/
79-
export async function getOrCreateBackend(
87+
export async function onboardBackend(
8088
projectId: string,
81-
setup: any
89+
location: string,
90+
backendId: string
8291
): Promise<Backend | undefined> {
83-
const location: string = setup.frameworks.region;
84-
try {
85-
return await getExistingBackend(projectId, setup, location);
86-
} catch (err: unknown) {
87-
if ((err as FirebaseError).status === 404) {
88-
const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location);
89-
await promptOnce(
90-
{
91-
name: "branchName",
92-
type: "input",
93-
default: "main",
94-
message: "Which branch do you want to deploy?",
95-
},
96-
setup.frameworks
97-
);
98-
const backendDetails = toBackend(cloudBuildConnRepo);
99-
return await createBackend(projectId, location, backendDetails, setup.frameworks.serviceName);
100-
} else {
101-
throw new FirebaseError(
102-
`Failed to get or create a backend using the given initialization details: ${err}`
103-
);
104-
}
105-
}
106-
107-
return undefined;
108-
}
109-
110-
async function getExistingBackend(
111-
projectId: string,
112-
setup: any,
113-
location: string
114-
): Promise<Backend> {
115-
let backend = await gcp.getBackend(projectId, location, setup.frameworks.serviceName);
116-
while (backend) {
117-
setup.frameworks.serviceName = undefined;
118-
await promptOnce(
119-
{
120-
name: "existingBackend",
121-
type: "confirm",
122-
default: true,
123-
message:
124-
"A backend already exists for the given serviceName, do you want to use existing backend? (yes/no)",
125-
},
126-
setup.frameworks
127-
);
128-
if (setup.frameworks.existingBackend) {
129-
logBullet("Using the existing backend.");
130-
return backend;
131-
}
132-
await promptOnce(
133-
{
134-
name: "serviceName",
135-
type: "input",
136-
default: "acme-inc-web",
137-
message: "Please enter a new service name [1-30 characters]",
138-
},
139-
setup.frameworks
140-
);
141-
backend = await gcp.getBackend(projectId, location, setup.frameworks.serviceName);
142-
setup.frameworks.existingBackend = undefined;
143-
}
144-
145-
return backend;
92+
const cloudBuildConnRepo = await repo.linkGitHubRepository(projectId, location);
93+
const barnchName = await promptOnce({
94+
name: "branchName",
95+
type: "input",
96+
default: "main",
97+
message: "Which branch do you want to deploy?",
98+
});
99+
// branchName unused for now.
100+
const backendDetails = toBackend(cloudBuildConnRepo);
101+
return await createBackend(projectId, location, backendDetails, backendId);
146102
}
147103

148104
/**

src/test/init/frameworks/index.spec.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from "chai";
44
import * as gcp from "../../../gcp/frameworks";
55
import * as repo from "../../../init/features/frameworks/repo";
66
import * as poller from "../../../operation-poller";
7-
import { createBackend, getOrCreateBackend } from "../../../init/features/frameworks/index";
7+
import { createBackend, onboardBackend } from "../../../init/features/frameworks/index";
88
import { FirebaseError } from "../../../error";
99

1010
describe("operationsConverter", () => {
@@ -30,36 +30,31 @@ describe("operationsConverter", () => {
3030
sandbox.verifyAndRestore();
3131
});
3232

33-
describe("createBackend", () => {
33+
describe("onboardBackend", () => {
3434
const projectId = "projectId";
3535
const location = "us-central1";
3636
const backendId = "backendId";
37+
3738
const op = {
3839
name: `projects/${projectId}/locations/${location}/backends/${backendId}`,
3940
done: true,
4041
};
42+
4143
const completeBackend = {
4244
name: `projects/${projectId}/locations/${location}/backends/${backendId}`,
4345
labels: {},
4446
createTime: "0",
4547
updateTime: "1",
4648
uri: "https://placeholder.com",
4749
};
48-
const setup = {
49-
frameworks: {
50-
region: location,
51-
serviceName: backendId,
52-
existingBackend: true,
53-
deployMethod: "github",
54-
branchName: "main",
55-
},
56-
};
50+
5751
const cloudBuildConnRepo = {
5852
name: `projects/${projectId}/locations/${location}/connections/framework-${location}/repositories/repoId`,
5953
remoteUri: "remoteUri",
6054
createTime: "0",
6155
updateTime: "1",
6256
};
57+
6358
const backendInput: Omit<gcp.Backend, gcp.BackendOutputOnlyFields> = {
6459
servingLocality: "GLOBAL_ACCESS",
6560
codebase: {
@@ -68,6 +63,7 @@ describe("operationsConverter", () => {
6863
},
6964
labels: {},
7065
};
66+
7167
it("should createBackend", async () => {
7268
createBackendStub.resolves(op);
7369
pollOperationStub.resolves(completeBackend);
@@ -77,27 +73,17 @@ describe("operationsConverter", () => {
7773
expect(createBackendStub).to.be.calledWith(projectId, location, backendInput);
7874
});
7975

80-
it("should return a backend, if user wants use the exiting backend", async () => {
81-
getBackendStub.resolves(completeBackend);
82-
83-
const result = await getOrCreateBackend("projectId", setup);
84-
85-
expect(result).to.deep.equal(completeBackend);
86-
expect(getBackendStub.calledOnceWithExactly(projectId, location, backendId)).to.be.true;
87-
});
88-
89-
it("should create a new backend, if backend doesn't exist", async () => {
76+
it("should onboard a new backend", async () => {
9077
const newBackendId = "newBackendId";
9178
const newPath = `projects/${projectId}/locations/${location}/backends/${newBackendId}`;
92-
setup.frameworks.serviceName = newBackendId;
9379
op.name = newPath;
9480
completeBackend.name = newPath;
9581
getBackendStub.throws(new FirebaseError("error", { status: 404 }));
9682
linkGitHubRepositoryStub.resolves(cloudBuildConnRepo);
9783
createBackendStub.resolves(op);
9884
pollOperationStub.resolves(completeBackend);
9985

100-
const result = await getOrCreateBackend(projectId, setup);
86+
const result = await onboardBackend(projectId, location, backendId);
10187

10288
expect(result).to.deep.equal(completeBackend);
10389
expect(createBackendStub).to.be.calledWith(projectId, location, backendInput);

0 commit comments

Comments
 (0)