Skip to content

Commit a6c5ce8

Browse files
chrbrtcbrutscher
andauthored
IE-186: Add PATCH method support for redeploying functions and improv… (#263)
* IE-186: Add PATCH method support for redeploying functions and improve deployment logic * fixed buggy test --------- Co-authored-by: cbrutscher <[email protected]>
1 parent e0cceda commit a6c5ce8

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "liveperson-functions-cli",
33
"description": "LivePerson Functions CLI",
4-
"version": "2.0.5-beta",
4+
"version": "2.0.6-beta",
55
"author": {
66
"name": "LivePersonInc",
77
"email": "[email protected]"

src/service/faas.service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../types/IFunction';
1919
import { LPSchedule, LPScheduleCreateParams } from '../types/ISchedule';
2020

21-
export type HttpMethods = 'POST' | 'GET' | 'DELETE' | 'PUT';
21+
export type HttpMethods = 'POST' | 'GET' | 'DELETE' | 'PUT' | 'PATCH';
2222

2323
export interface IPayload {
2424
headers: string[];
@@ -208,6 +208,20 @@ export class FaasService implements IFaaSService {
208208
}
209209
}
210210

211+
public async redeploy(uuid: string): Promise<IDeploymentResponse> {
212+
const urlPart = `/deployments/${uuid}`;
213+
try {
214+
const response = await this.doFetch({ urlPart, method: 'PATCH' });
215+
216+
return response;
217+
} catch (error) {
218+
return {
219+
message: error.errorMsg,
220+
uuid,
221+
};
222+
}
223+
}
224+
211225
public async getLambdasByNames(lambdaNames: string[]): Promise<LPFunction[]> {
212226
const allFnMetas = await this.getAllFunctionMetas();
213227
const existingFunctions = allFnMetas.filter(({ name }) =>
@@ -446,7 +460,7 @@ export class FaasService implements IFaaSService {
446460

447461
public async getFunctionByUuid(uuid: string): Promise<IFunction> {
448462
const urlPart = `/functions/${uuid}`;
449-
const [foundLambda] = await this.doFetch({ urlPart, method: 'GET' });
463+
const foundLambda = await this.doFetch({ urlPart, method: 'GET' });
450464
return foundLambda;
451465
}
452466

src/view/deploy.view.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,17 @@ export class DeployView {
8484
} else {
8585
this.log.print('\nDeploying following functions:\n');
8686
}
87-
8887
confirmedFunctionsToDeploy.forEach((entry: IFunction) => {
8988
this.tasklist.addTask({
9089
title: `Deploying ${entry.name}`,
9190
task: async (_, task) => {
9291
const faasService = await factory.get();
93-
const response = await faasService.deploy(entry.uuid);
92+
let response;
93+
if (entry.state === 'Modified') {
94+
response = await faasService.redeploy(entry.uuid);
95+
} else {
96+
response = await faasService.deploy(entry.uuid);
97+
}
9498

9599
if (response.uuid) {
96100
return task.skip(`${response.message} (${entry.uuid})`);
@@ -117,37 +121,42 @@ export class DeployView {
117121
private async waitForDeployment(
118122
faasService: any,
119123
uuid: string,
120-
timeoutMs = 2000000,
121-
intervalMs = 3000,
122-
): Promise<void> {
123-
const startTime = Date.now();
124+
timeoutMs = 2_000_000,
125+
intervalMs = 3_000,
126+
): Promise<boolean> {
127+
const start = Date.now();
124128

125-
return new Promise<void>((resolve, reject) => {
126-
const poll = async () => {
129+
return new Promise<boolean>((resolve, reject) => {
130+
const poll = async (): Promise<boolean> => {
127131
try {
128-
const isDeployed = await this.checkIfLambdaIsDeployed(
132+
const deployed = await this.checkIfLambdaIsDeployed(
129133
faasService,
130134
uuid,
131135
);
132136

133-
if (isDeployed) {
134-
resolve();
135-
return;
137+
if (deployed) {
138+
resolve(true);
139+
return true;
136140
}
137141

138-
const elapsed = Date.now() - startTime;
139-
if (elapsed >= timeoutMs) {
142+
if (Date.now() - start >= timeoutMs) {
140143
reject(
141144
new Error(
142145
`Deployment timeout after ${timeoutMs}ms for function ${uuid}`,
143146
),
144147
);
145-
return;
148+
return false;
146149
}
147150

148151
setTimeout(poll, intervalMs);
149-
} catch (error) {
150-
reject(error);
152+
return false;
153+
} catch (err) {
154+
reject(
155+
new Error(
156+
`Error while checking deployment for ${uuid}: ${err.message}`,
157+
),
158+
);
159+
return false;
151160
}
152161
};
153162

@@ -165,11 +174,11 @@ export class DeployView {
165174
}
166175

167176
private preparePromptMessage(lambda: any) {
168-
const message = `Do you want to approve and deploy?
169-
177+
const message = `Do you want to approve and (re)deploy?
170178
AccountId: ${this.chalk.green(lambda.accountId)}
171179
Description: ${lambda.description}
172180
UUID: ${lambda.uuid}
181+
UUID: ${lambda.state}
173182
Event: ${lambda.event || 'No Event'}
174183
Last modified by: ${lambda.updatedBy}
175184
Last modified at: ${formatDate(lambda.updatedAt)}

test/service/faas.service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('faas service', () => {
163163
const csdsClient = new CsdsClient();
164164
csdsClient.getUri = jest.fn().mockReturnValue('faasUI');
165165
const gotDefault = jest.fn(() => ({
166-
body: [mock.mockFunction1],
166+
body: mock.mockFunction1,
167167
})) as any;
168168
const faasService = new FaasService({ gotDefault, csdsClient });
169169
const response = await faasService.getFunctionByUuid('123-123-123');

0 commit comments

Comments
 (0)