-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
360 lines (320 loc) · 9.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const core = require('@actions/core');
const {AppMeshClient, CreateVirtualNodeCommand, DeleteVirtualNodeCommand, DescribeVirtualNodeCommand} = require('@aws-sdk/client-app-mesh');
const _ = require('lodash');
/**
*
* ERRORS
* Provides signals for controlling application behavior.
*
*****************************************************************************************/
/**
* An error type representing a failure to find a service
* @extends Error
*/
class NotFoundException extends Error {
/**
* @param {String} message Error message
*/
constructor(message) {
super(message);
this.name = 'NotFoundException';
this.message = message;
this.stack = (new Error()).stack;
}
}
/**
*
* PARAMETER CONVERSION
* Converts the supplied (create) parameters into the formats for describe, update, and delete.
*
*****************************************************************************************/
/**
* return only defined properties
* @param {Object} obj
* @return {Object} sans keynames with 'undefined' values'
*/
function omitUndefined(obj) {
return _.pickBy(obj, (value, key) => {
return value !== undefined;
});
}
/**
* Filter parameters according to createGatewayRoute API
* @param {Object} parameters Original parameters
* @return {Object} Filtered parameters
*/
function createInput(parameters) {
return omitUndefined(
{
...describeInput(parameters),
spec: parameters.spec,
},
);
}
/**
* Filter parameters according to describeGatewayRoute API
* @param {Object} parameters Original parameters
* @return {Object} Filtered parameters
*/
function describeInput(parameters) {
return omitUndefined(
{
virtualNodeName: parameters.virtualNodeName,
meshName: parameters.meshName,
meshOwner: parameters.meshOwner,
},
);
}
/**
* Filter parameters according to deleteGatewayRoute API
* @param {Object} parameters Original parameters
* @return {Object} Filtered parameters
*/
function deleteInput(parameters) {
return omitUndefined(
{
...describeInput(parameters),
},
);
}
/**
*
* Custom Waiter
* Create a custom waiter while the SDK doesn't have one built in yet.
*
*****************************************************************************************/
const {WaiterState, checkExceptions, createWaiter} = require('@aws-sdk/util-waiter');
const checkState = async (client, parameters) => {
let response;
let status;
try {
core.info('... polling resource...');
response = await describeResource(client, describeInput(parameters));
status = response.virtualNode.status.status;
} catch (err) {
if (err.name == 'NotFoundException') {
core.info('... and it is missing ...');
status = 'MISSING';
} else {
throw err;
}
}
if (status == 'MISSING') {
return {state: WaiterState.SUCCESS, response};
}
if (status == 'DELETED') {
return {state: WaiterState.SUCCESS, response};
}
return {state: WaiterState.RETRY, response};
};
async function waitUntilResourceDeleted(client, parameters) {
core.info('Waiting for resource to be deleted...');
const serviceDefaults = {minDelay: 15, maxDelay: 120};
const result = await createWaiter({...serviceDefaults, ...client}, parameters, checkState);
core.info('...done waiiting for resource to be deleted.');
return checkExceptions(result);
};
/**
*
* AWS CALLS
* Take the supplied parameters and send them to AWS
*
*****************************************************************************************/
/**
* Fetch Node or throw an error
* @param {@aws-sdk/client-app-mesh/AppMeshClient} client client
* @param {Object} parameters Parameters describing the Node
* @return {Promise} that resolves to {@aws-sdk/client-app-mesh/DescribeVirtualNodeCommandOutput}
*/
async function describeResource(client, parameters) {
const command = new DescribeVirtualNodeCommand(describeInput(parameters));
const response = await client.send(command);
return response;
}
/**
* Create Node or throw an error
* @param {@aws-sdk/client-app-mesh/AppMeshClient} client client
* @param {Object} parameters Parameters describing the Node
* @return {Promise} that resolves to {@aws-sdk/client-app-mesh/CreateVirtualNodeCommandOutput}
*/
async function createResource(client, parameters) {
const command = new CreateVirtualNodeCommand(createInput(parameters));
const response = await client.send(command);
return response;
}
/**
* Delete Node or throw an error
* @param {@aws-sdk/client-app-mesh/AppMeshClient} client client
* @param {Object} parameters Parameters describing the Node
* @return {Promise} that resolves to {@aws-sdk/client-app-mesh/deleteVirtualNodeCommandOutput}
*/
async function deleteResource(client, parameters) {
const command = new DeleteVirtualNodeCommand(deleteInput(parameters));
const response = await client.send(command);
return response;
}
/**
*
* FIND/CREATE/DELETE BUSINESS LOGIC
*
*****************************************************************************************/
/**
* Find or create the Node
* @param {@aws-sdk/client-app-mesh/AppMeshClient} client client
* @param {Object} parameters Parameters describing the Node
* @return {Promise} that resolves to {@aws-sdk/client-app-mesh/DescribeVirtualNodeCommandOutput} or {@aws-sdk/client-app-mesh/CreateVirtualNodeCommandOutput}
*/
async function findOrCreate(client, parameters) {
core.info(`Searching for ${parameters.virtualNodeName}`);
try {
const response = await describeResource(client, parameters);
if (response && response.virtualNode) {
switch (response.virtualNode.status.status) {
case 'ACTIVE':
core.info(`${response.virtualNode.virtualNodeName} found.`);
return response;
case 'INACTIVE':
core.warn(`${response.virtualNode.virtualNodeName} found, but it is INACTIVE.`);
return response;
case 'DELETED':
const message = `${parameters.virtualNodeName} found, but it is DELETED.`;
throw new NotFoundException(message);
default:
throw new Error(response);
}
} else {
throw new Error(`Invalid response from describeResource: ${JSON.stringify(response)}`);
}
} catch (err) {
if (err.name === 'NotFoundException') {
core.info(`Unable to find ${parameters.virtualNodeName}. Creating newly.`);
return await createResource(client, parameters);
} else {
throw err;
}
}
}
/**
*
* GITHUB ACTIONS INTERFACE
* - Gets parameters from the user.
* - Posts results as output.
*
*****************************************************************************************/
/**
* @param {Error} err The original error
* @param {String} param The parameter that was being evaluated
* @param {String} s The supplied string
* @return {Error} The Error indicating invalid JSON, if JSON, else err.
*/
function handleGetParameterErrors(err, param, s) {
if (err instanceof SyntaxError) {
return new Error(`Invalid JSON for ${param}: ${err.message}: ${s}`);
} else {
return err;
}
}
/**
* Fetch parameters from environment
* @return {Object} parameters
*/
function getParameters() {
const parameters = {
action: core.getInput('action', {required: false}) || 'create',
meshOwner: core.getInput('mesh-owner', {required: false}),
meshName: core.getInput('mesh-name', {required: true}),
virtualNodeName: core.getInput('name', {required: true}),
};
// JSON Parameters
Object.entries({
spec: 'spec',
tags: 'tags',
}).forEach(([key, value]) => {
const s = core.getInput(value, {required: false});
if (s) {
let t;
try {
t = JSON.parse(s);
} catch (err) {
throw handleGetParameterErrors(err, key, s);
}
Object.assign(parameters, {[key]: t});
}
});
return _.pickBy(
parameters,
(value, key) => {
return value !== '';
},
);
}
/**
* Posts the results of the action to GITHUB_ENV
* @param {Object} response Response response
*/
function postToGithub(response) {
let arn;
if (response.virtualNode && response.virtualNode.metadata) {
arn = response.virtualNode.metadata.arn;
} else {
throw new Error('Unable to determine ARN');
}
core.info('ARN found, created, or deleted: ' + arn);
core.setOutput('response', response);
core.setOutput('arn', arn);
}
/**
*
* ENTRYPOINT
*
*****************************************************************************************/
/**
* Executes the action
* @return {Promise} that resolves to {@aws-sdk/client-app-mesh/DescribeVirtualNodeCommandOutput} or {@aws-sdk/client-app-mesh/CreateVirtualNodeCommandOutput}
*/
async function run() {
const client = new AppMeshClient({
customUserAgent: 'amazon-appmesh-virtual-node-for-github-actions',
});
client.middlewareStack.add((next, context) => (args) => {
core.debug(`Middleware sending ${context.commandName} to ${context.clientName} with: ${JSON.stringify(args)}`);
return next(args);
},
{
step: 'build', // add to `finalize` or `deserialize` for greater verbosity
},
);
// Get input parameters
const parameters = getParameters();
let response;
if (parameters.action == 'delete') {
response = await deleteResource(client, parameters);
await waitUntilResourceDeleted({client, maxWaitTime: 300}, parameters);
} else {
response = await findOrCreate(client, parameters);
}
postToGithub(response);
return response;
}
/* istanbul ignore next */
if (require.main === module) {
run().catch((err) => {
const httpStatusCode = err.$metadata ? err.$metadata.httpStatusCode : undefined;
core.setFailed(`${err.name} (Status code: ${httpStatusCode}): ${err.message}`);
core.debug(`Received error: ${JSON.stringify(err)}`);
core.debug(err.stack);
process.exit(1);
});
}
module.exports = {
createInput,
createResource,
findOrCreate,
deleteInput,
deleteResource,
describeInput,
describeResource,
getParameters,
postToGithub,
run,
};