UNPKG

@idiamantino/livepro-utils

Version:

Stuff for LP

219 lines (188 loc) 7.27 kB
const AWS = require('aws-sdk'); const Utils = require("../library/utils"); const Lambda = require('../aws/lambda.js'); let LpUtils = new Utils(); class cloudwatch { constructor(region, apiVersion = '2012-10-17', debug = false) { this.setDebug(debug); this.region = region; LpUtils.printDebug("Cloudwatch constructor region:", region); AWS.config.update({region:this.region}); this.cloudwatchevents = new AWS.CloudWatchEvents(); this.cloudwatchevents = new AWS.CloudWatchEvents({region:this.region}); } setDebug(debug = false) { this.debug = debug; LpUtils.setDebug(debug); } async test(params){ LpUtils.printDebug("Cloudwatch function accessible:", params); LpUtils.printDebug("Region:", this.region); // this.setProcessClientStatus("cdf2c150-c724-11ea-94ea-5d5d37731bbc","testing: " + params); return (true); } async enableRule(ruleName){ try { let event_params = { Name: ruleName }; LpUtils.printDebug("Enabling cloudwatch event rule ", ruleName); let data = await this.cloudwatchevents.enableRule(event_params).promise(); if(data) return true; } catch (err) { console.error(err); return false; } return false; } async disableRule(ruleName){ try { let event_params = { Name: ruleName }; LpUtils.printDebug("Disabling cloudwatch event rule: ", ruleName); let data = await this.cloudwatchevents.disableRule(event_params).promise(); if(data) return true; } catch (err) { console.error(err); return false; } return false; } async createRule(ruleName, scheduleExpression, state = "DISABLED", description = ""){ try { var params = { Name: ruleName, ScheduleExpression: scheduleExpression, State: state }; if(description !== "") { if(description.length > 200) { description = description.substring(0,200).trimEnd() + "..." } params.Description = description; } LpUtils.printDebug("Creating cloudwatch event rule: ", ruleName); const createdRule = await this.cloudwatchevents.putRule(params).promise(); console.log(createdRule); if(createdRule) return true; // await this.cloudwatchevents.putRule(params, function(err, data) { // if (err) { // console.log("Error creating cloudwatch event rule", err); // return false; // } else { // return true; // } // }); } catch (err) { console.log("Error creating cloudwatch event rule", err); return false; } return false; } async createLambdaRule(ruleName, scheduleExpression, state = "DISABLED", lambdaName, lambdaJson, description = ""){ try { const LpLambda = new Lambda(this.region); LpLambda.setDebug(true); const lambdaFunction = await LpLambda.getFunction(lambdaName); if(!lambdaFunction) { console.log("Error retrieving lambda:", lambdaName); return false; } const createRule = await this.createRule(ruleName, scheduleExpression, state, description); console.log("createrule:", createRule); if(createRule) { var paramsRule = { Name: ruleName }; const rule = await this.cloudwatchevents.describeRule(paramsRule).promise(); console.log(rule); } var paramsTarget = { Rule: ruleName, Targets: [ { Id: 'default', Arn: lambdaFunction.Configuration.FunctionArn, // RoleArn: ruleParams.RoleArn, Input: JSON.stringify(lambdaJson) } ] }; LpUtils.printDebug("Creating cloudwatch event rule target: ", ruleName); const targetreturn = new Promise((res, rej) => { try { this.cloudwatchevents.putTargets(paramsTarget, 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); } }); } catch (err) { console.log("Error creating cloudwatch event rule", err); return false; } return false; } async deleteRule(ruleName){ try { var paramsTarget = { Ids: [ 'default' ], Rule: ruleName }; LpUtils.printDebug("Deleting cloudwatch event rule targets: ", ruleName); await this.cloudwatchevents.removeTargets(paramsTarget, function(err, data) { if (err) { console.log("Error deleting cloudwatch event rule target", err); } else { LpUtils.printDebug("Success creating cloudwatch event rule target: ", ruleName); } }); var params = { Name: ruleName }; LpUtils.printDebug("Deleting cloudwatch event rule: ", ruleName); await this.cloudwatchevents.deleteRule(params, function(err, data) { if (err) { console.log("Error deleting cloudwatch event rule", err); return false; } else { LpUtils.printDebug("Success creating cloudwatch event rule: ", ruleName); return true; } }); } catch (err) { console.log("Error deleting cloudwatch event rule", err); return false; } return false; } // cloudwatchevents.removeTargets(paramsTarget, function(err, data) { // if (err) { // console.log(err, err.stack) // } else { // console.log(data); // var params = { // Name: `ForOrder_${order_id}` // }; // cloudwatchevents.deleteRule(params, function(err, data) { // if (err) { // console.log(err, err.stack); // } else { // console.log("Delete cloudwatch rule successful! - data: " + JSON.stringify(data, null, 2)); // } // callback(null, result); // }); // } } module.exports = cloudwatch;