forked from amirams/spotinst-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhandler.js
More file actions
47 lines (43 loc) · 1.27 KB
/
handler.js
File metadata and controls
47 lines (43 loc) · 1.27 KB
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
const rp = require('request-promise')
/*
This function take in the user specified key and value through environment variables then
POSTs the new document to the document store in the specified environment
*/
module.exports.main = function main (event, context, callback) {
// Spotinst Credentials
let account = process.env['spotAccount']
let token = process.env['spotToken']
let environment = process.env['spotEnvironment']
// Key Value Pair to be added into the document store
let key = process.env['key']
let value = process.env['value']
// Options to make post request to add key/value pair to document store
let postOptions = {
uri:'https://api.spotinst.io/functions/environment/'+environment+'/userDocument',
method: "POST",
qs: {accountId: account},
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token},
body: {
"userDocument": {
"key": key,
"value": value}},
json:true
}
rp(postOptions).then((res)=>{
console.log(res['response'])
callback(null, {
statusCode: 200,
body: 'Success',
headers: {"Content-Type": "application/json"}
});
}).catch((err)=>{
console.log(err)
callback(null, {
statusCode: 400,
body: 'Error. Check Logs',
headers: {"Content-Type": "application/json"}
});
})
};