forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.app.mjs
67 lines (66 loc) · 1.91 KB
/
aws.app.mjs
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
import {
EC2Client,
DescribeRegionsCommand,
} from "@aws-sdk/client-ec2";
import {
regions,
defaultRegion,
} from "./common/regions.mjs";
export default {
type: "app",
app: "aws",
propDefinitions: {
region: {
type: "string",
label: "AWS Region",
description: "The AWS region",
default: defaultRegion,
async options() {
try {
const { Regions } = await this.ec2ListRegions();
return Regions.map((regionInfo) => regionInfo.RegionName).sort();
} catch (error) {
// Retrieval of available regions can fail if the registered account
// does not have enough permissions to call the EC2 `DescribeRegions`
// API. In that case, we default to the static list of regions.
console.log(`Could not retrieve available regions from AWS. ${error}, falling back to default regions`);
return regions;
}
},
},
eventData: {
type: "object",
label: "Event data",
description: "A JSON object that will be sent as an event",
optional: true,
},
},
methods: {
getAWSClient(clientType, region = defaultRegion) {
return new clientType({
credentials: {
accessKeyId: this.$auth.accessKeyId,
secretAccessKey: this.$auth.secretAccessKey,
},
region,
});
},
async ec2ListRegions() {
const client = this.getAWSClient(EC2Client);
return client.send(new DescribeRegionsCommand({}));
},
async pagination(fn, params, nextTokenAttr, lastTokenAttr = null) {
let response;
const results = [];
do {
response = await fn(params);
results.push(...response.Items);
params[nextTokenAttr] = lastTokenAttr
? response[lastTokenAttr]
: response[nextTokenAttr];
} while (params[nextTokenAttr]);
response.Items = results;
return response;
},
},
};