-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.js
71 lines (61 loc) · 1.97 KB
/
core.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
const _ = require("lodash");
const kaholoPluginLibrary = require("@kaholo/plugin-library");
const helpers = require("./helpers");
const consts = require("./consts.json");
function bootstrap(
awsService,
pluginMethods,
autocompleteFuncs = {},
credentialLabels = consts.DEFAULT_CREDENTIAL_LABELS,
) {
const preparedPluginMethods = _.mapValues(pluginMethods, (actionMethod) => (
async (params, ...args) => {
const region = helpers.readRegion(params, credentialLabels.REGION, false);
const awsServiceClient = getServiceInstance(
awsService,
params,
credentialLabels,
);
const result = await actionMethod.apply(null, [awsServiceClient, params, region, ...args]);
return helpers.stripAwsResultOffMetadata(result);
}
));
const preparedAutocompleteFunctions = _.mapValues(autocompleteFuncs, (autocompleteFunction) => (
(query, params, ...args) => {
const region = helpers.readRegion(params, credentialLabels.REGION, false);
const awsServiceClient = getServiceInstance(
awsService,
params,
credentialLabels,
);
return autocompleteFunction.apply(null, [query, params, awsServiceClient, region, ...args]);
}
));
return kaholoPluginLibrary.bootstrap(
preparedPluginMethods,
preparedAutocompleteFunctions,
);
}
function generateAwsMethod(Command, payloadFunction = null) {
return async (awsServiceClient, params, region) => {
const payload = helpers.removeUndefinedAndEmpty(
payloadFunction
? payloadFunction(params, region)
: params,
);
const commandInstance = new Command(payload);
return awsServiceClient.send(commandInstance);
};
}
function getServiceInstance(
AWSService,
actionParams,
credentialsLabels = consts.DEFAULT_CREDENTIAL_LABELS,
) {
const credentials = helpers.readCredentials(actionParams, credentialsLabels);
return new AWSService(credentials);
}
module.exports = {
generateAwsMethod,
bootstrap,
};