-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-aws.js
153 lines (143 loc) · 6.04 KB
/
simple-aws.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
const AWS = require('aws-sdk')
const paginators = require('./resources/paginators.json')
module.exports = function(RED) {
function SimpleAWSNode(config) {
//console.log("config", config)
RED.nodes.createNode(this, config);
var node = this;
let awsConfig = RED.nodes.getNode(config.aws);
this.region = awsConfig.region
/*
this.parameterType = config.parameterType
this.parameter = config.parameter
this.operation = config.operation
this.service = config.service
this.aws = config.aws
*/
AWS.config.update({
accessKeyId: awsConfig.accessKey,
secretAccessKey: awsConfig.secretKey
});
let serviceOptions = {}
try {
if ( config.serviceOptions && config.serviceOptions.trim() > 0 ) {
serviceOptions = JSON.parse(config.serviceOptions)
}
} catch (e) {
let eMsg = "service options were not valid JSON"
node.status({ fill: "red", shape: "ring", text: sMsg })
node.error(e, eMsg)
}
if ( config.apiVersion ) {
serviceOptions.apiVersion = config.apiVersion
}
if ( this.region ) {
serviceOptions.region = this.region
}
//console.log("creating new AWS service " + config.service + " with params", serviceOptions)
let client = null
try {
client = new AWS[config.service](serviceOptions)
} catch (e) {
let eMsg = "error instantiating AWS service " + config.service + "(" + config.apiVersion + ":" + config.operation + " " + e.message
node.status({ fill: "red", shape: "ring", text: eMsg})
node.error(e, eMsg);
}
/* let's handle the payload parameter */
let valid = true
if ( config.parameterType === 'json' ) {
try {
// check this is parsable JSON
JSON.parse(config.parameter);
} catch(e) {
this.error(RED._("change.errors.invalid-json, "));
}
} else if ( config.parameterType === 'jsonata') {
try {
node.jsonata = RED.util.prepareJSONataExpression(config.parameterType, this);
} catch(e) {
valid = false;
let eMsg = "error parsing JSONata expression " + config.service + "(" + config.apiVersion + ":" + config.operation + " " + e.message
node.status({ fill: "red", shape: "ring", text: eMsg})
node.error(e, eMsg);
}
}
/* let's deal with the paginators here */
let paginatorsDef = paginators[config.service.toLowerCase() + '-' + config.apiVersion]
let paginatorDef = paginatorsDef ? paginatorsDef[config.operation] : null
//console.log("pagDefs", paginatorsDef, "pagDef", paginatorDef,"op", config.operation)
/*
if (this.awsConfig.proxyRequired){
var proxy = require('proxy-agent');
AWS.config.update({
httpOptions: { agent: new proxy(this.awsConfig.proxy) }
});
*/
node.on('input', async function(msg, nodeSend, nodeDone) {
//console.log("message received", msg, "node", node,"send",nodeSend,"done",nodeDone)
let operationParam = {}
let msgCopy = { ...msg }
try {
switch (config.parameterType) {
case 'jsonata' :
operationParam = await new Promise((resolve, reject) => {
RED.util.evaluateNodeProperty(config.parameter, config.parameterType, node, msg, (err, result) => {
console.log("in promise", err, result)
if ( err ) {
reject(err)
} else {
resolve(result)
}
})
})
break
case 'msg':
operationParam = RED.util.getMessageProperty(msg,config.parameter);
break
case 'json':
operationParam = JSON.parse(config.parameter)
break
default:
throw "Unexpected config parameter type " + config.parameterType
}
} catch (e) {
let eMsg = "AWS parameter to " + config.service + ":" + config.operation + " was malformed. " + e.message
msgCopy.error = e
msgCopy.errorMsg = eMsg
node.status({ fill: "red", shape: "ring", text: eMsg})
node.error(eMsg);
node.send([null, msgCopy])
return
}
node.status({ fill: "green", shape: "ring", text: "calling " + config.service + ":" + config.operation });
let operationParamCopy = { ... operationParam }
try {
let done = false
while (!done) {
var response = await client[config.operation](operationParamCopy).promise()
msgCopy.payload = response
if ( config.paging !== 'disabled' && paginatorDef && response[paginatorDef.output_token]) {
//console.log(`paginating ${config.service}:${config.operation} on ${paginatorDef.output_token}`)
operationParamCopy[paginatorDef.input_token] = response[paginatorDef.output_token]
delete msgCopy.complete
node.send([msgCopy, null])
} else {
done = true
msgCopy.complete = true
node.status({ fill: "green", shape: "ring", text: "done" });
node.send([msgCopy, null])
nodeDone()
}
}
} catch (e) {
let eMsg = `Error while calling ${config.service}:${config.operation} ${e.message}`
msgCopy.error = e
msgCopy.errorMsg = eMsg
node.status({ fill: "red", shape: "ring", text: e });
node.error(e, `error calling ${config.service}:${config.operation} ${e}`);
node.send([null, msgCopy])
}
});
}
RED.nodes.registerType("simple-aws",SimpleAWSNode);
}