@idiamantino/livepro-utils
Version:
Stuff for LP
184 lines (162 loc) • 6.71 kB
JavaScript
const AWS = require('aws-sdk');
const Utils = require("../library/utils");
let LpUtils = new Utils();
class lambda {
constructor(region, apiVersion = '2015-03-31', debug = false) {
this.setDebug(debug);
this.region = region;
LpUtils.printDebug("Lambda constructor region:", region);
AWS.config.update({region:this.region});
this.lambda = new AWS.Lambda({apiVersion: apiVersion}, { region: this.region });
}
setDebug(debug = false) {
this.debug = debug;
LpUtils.setDebug(debug);
}
async test(params){
LpUtils.printDebug("Lambda function accessible:", params);
LpUtils.printDebug("Region:", this.region);
let result = "";
// let lambdaname = "LiveproSandbox";
// let lambdaargs = '{"key1": "value1","key2": "value2","key3": "value3"}';
let lambdaname = "LiveproConfiguration";
// Return one field
// let lambdaargs = { "parameters": '{ "client_name":"devsearch"}',"method": "GET", "body": "", "path":"getclientconfig"};
// Return multiple fields
let lambdaargs = { "parameters": '{ "source":"dynamodb"}',"method": "GET", "body": "", "path":"getallclientsconfig"};
// let j1 = JSON.stringify(lambdaargs);
// console.log(j1);
// let j2 = JSON.parse(j1);
// console.log(j2);
// process.exit();
// result = await this.callAsync(lambdaname,lambdaargs);
// console.log(result);
result = await this.getJsonBody(lambdaname,lambdaargs);
// result = await this.callSync(lambdaname,lambdaargs);
console.log(result.qa416);
let client_name = "qa416";
result = await this.getJsonBody("LiveproConfiguration",{ "parameters": "{\"client_name\": \"" + client_name + "\" }","method": "GET", "body": "", "path":"getclientprocesssecret"});
if(result.process_secret !== undefined)
console.log(result.process_secret);
return (true);
}
async getJsonBody(lambdaname,lambdaargs) {
const result = await this.callSync(lambdaname,lambdaargs);
if(result !== undefined && result !== "") {
if(result.statusCode !== undefined && result.statusCode == 200) {
if(result.body !== undefined && result.body !== "") {
try {
const jsonResponse = JSON.parse(result.body);
return(jsonResponse);
} catch (error) {
console.log("Response.body is not a valid JSON")
console.log(error);
}
} else {
console.log("Response.body is empty or undefined")
}
} else {
console.log("Response.statusCode is not 200")
}
} else {
console.log("Response is empty or underfined")
}
return false;
}
async callSync(name, lambdaargs) {
LpUtils.printDebug("Calling Sync Lambda", name);
var params = {
FunctionName: name,
// RequestResponse is important here. Without it we won't get the result Payload
InvocationType: 'RequestResponse',
LogType: 'Tail' // other option is 'None'
};
params.Payload = lambdaargs;
// Required, convert the payload to a string
params.Payload = JSON.stringify(params.Payload);
LpUtils.printDebug("Params inside callSync", params);
const result = new Promise((res, rej) => {
try {
LpUtils.printDebug("Invoking Sync");
this.lambda.invoke(params, function(err, data) {
if (err) {
if (err)
console.log("Error calling lambda:" + name);
console.log(err, err.stack); // an error occurred
res(false);
} else {
LpUtils.printDebug("Successful Response");
// console.log(data.Payload);
const resultJson = JSON.parse(data.Payload);
// res(resultObject);
res(resultJson);
}
});
} catch(err) {
console.log("Error calling lambda:" + name);
console.log(err, err.stack);
res(false);
}
});
return(await result);
}
async callAsync(name, lambdaargs) {
LpUtils.printDebug("Calling Async Lambda", name);
lambdaargs = JSON.stringify(lambdaargs);
var params = {
FunctionName: name,
InvokeArgs: lambdaargs
};
LpUtils.printDebug("Params inside callAsync", params);
const result = new Promise((res, rej) => {
try {
LpUtils.printDebug("Invoking Async");
this.lambda.invokeAsync(params, function(err, data) {
if (err) {
if (err)
console.log("Error calling lambda:" + name);
console.log(err, err.stack); // an error occurred
res(false);
} else {
LpUtils.printDebug("Successful Response");
res(true);
}
});
} catch(err) {
console.log("Error calling lambda:" + name);
console.log(err, err.stack);
res(false);
}
});
return(await result);
}
async getFunction(lambdaName, version = "") {
var params = {
FunctionName: lambdaName
};
if(version !== "") params.Qualifier = version;
LpUtils.printDebug("Get function:", lambdaName + " qualifier:" + version);
const lambdareturn = new Promise((res, rej) => {
try {
this.lambda.getFunction(params, function(err, data) {
if (err) {
if (err)
console.log(err, err.stack);
res(false);
} else {
res(data);
}
});
} catch(err) {
console.log(err, err.stack);
res(false);
}
});
if(lambdareturn) {
return(await lambdareturn);
} else {
return false;
}
}
}
module.exports = lambda;