-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
108 lines (92 loc) · 2.45 KB
/
helpers.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
const _ = require("lodash");
const { parsers } = require("@kaholo/plugin-library");
const consts = require("./consts.json");
function removeCredentials(params, labels = consts.DEFAULT_CREDENTIAL_LABELS) {
return { ..._.omit(params, _.values(labels)) };
}
function readRegion(
params,
label = consts.DEFAULT_CREDENTIAL_LABELS.REGION,
silent = true,
) {
if (_.has(params, label)) {
return parsers.autocomplete(params[label]);
}
if (!silent) {
console.info(`Region parameter not specified, using default value: "${consts.DEFAULT_REGION}"`);
}
return consts.DEFAULT_REGION;
}
function readCredentials(
params,
labels = consts.DEFAULT_CREDENTIAL_LABELS,
) {
const areCredentialsDefined = (
_.has(params, labels.ACCESS_KEY)
&& _.has(params, labels.SECRET_KEY)
);
if (!areCredentialsDefined) {
throw new Error("Credential labels has not been found in params");
}
return {
credentials: {
accessKeyId: params[labels.ACCESS_KEY],
secretAccessKey: params[labels.SECRET_KEY],
},
region: readRegion(params, labels.REGION),
};
}
function removeUndefinedAndEmpty(object) {
if (!_.isPlainObject(object)) {
return object;
}
return _.omitBy(object, (value) => value === "" || _.isNil(value) || (_.isObjectLike(value) && _.isEmpty(value)));
}
function tryParseJson(value) {
if (!_.isString(value)) {
return value;
}
try {
return JSON.parse(value);
} catch {
return value;
}
}
function buildTagSpecification(resourceType, tags) {
if (resourceType === "" || _.isNil(resourceType)) {
throw new Error("Resource type cannot be empty nor null / undefined");
}
if (_.isNil(tags)) {
return [];
}
const unparsedTags = !_.isArray(tags) ? [tags] : tags;
if (_.isEmpty(_.compact(unparsedTags))) {
return [];
}
return [{
ResourceType: resourceType,
Tags: _.flatten(unparsedTags.map((v) => parsers.tags(tryParseJson(v)))),
}];
}
const stripAwsResultOffMetadata = (result) => {
if (!_.isPlainObject(result) && !_.isArray(result)) {
return result;
}
if (_.isArray(result)) {
return result.map(stripAwsResultOffMetadata);
}
return Object.fromEntries(
Object
.entries(result)
.filter(([key]) => key !== "$metadata")
.map(([key, value]) => [key, stripAwsResultOffMetadata(value)]),
);
};
module.exports = {
removeCredentials,
removeUndefinedAndEmpty,
readRegion,
readCredentials,
buildTagSpecification,
stripAwsResultOffMetadata,
};