From f866c8d6131e858171e9bbb42a63140f8861c0e9 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 May 2019 14:37:47 -0700 Subject: [PATCH 1/2] [src] updated for aws-cdk syntax --- lib/util.js | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/util.js b/lib/util.js index 8090967..c3df96b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -10,6 +10,7 @@ const RESOURCES = [ 'alert', 'beanstalkElastigroup', 'group', + 'Group', 'healthCheck', 'mrScaler', 'spectrumAlert', @@ -36,10 +37,21 @@ function getToken(event, cb) { if(err) return cb("Token creation failed: " + err); if(response.statusCode > 201) return cb("Token creation failed: " + response.statusMessage); - var accessToken = JSON.parse(body)['response']['items'][0]['accessToken']; + var accessToken; + var group = JSON.parse(body)['response']['items'][0] + + if (group['AccessToken']) { + accessToken = group['AccessToken'] + } else if (group['accessToken']) { + accessToken = group['accessToken'] + } + cb(null, accessToken); }); } + else if(config.AccessToken) { + cb(null, config.AccessToken); + } else if(config.accessToken) { cb(null, config.accessToken); } @@ -70,7 +82,6 @@ function addNulls(oldConfig, newConfig) { diff.applyChange(patchConfig, oldConfig, patch); } }); - return patchConfig; } @@ -92,6 +103,7 @@ function getConfig(event, cb) { } } } + if(!config) { var matchResourceConfig = _.intersection(_.keys(event), RESOURCES); config = event[matchResourceConfig[0]]; @@ -214,10 +226,12 @@ module.exports.parseBoolean = function(str) { module.exports.getSpotinstAccountId = function(event) { console.log("acctId Full Event: " + JSON.stringify(event, null, 2)); - var accountId = _.get(event, 'ResourceProperties.accountId') || _.get(event, 'accountId'); + var accountId = _.get(event, 'ResourceProperties.AccountId') || _.get(event, 'AccountId') + || _.get(event, 'ResourceProperties.accountId') || _.get(event, 'accountId'); + if (!accountId) { console.log(_.get(event, 'OldResourceProperties')); - accountId = _.get(event, 'OldResourceProperties.accountId'); + accountId = _.get(event, 'OldResourceProperties.accountId') || _.get(event, 'OldResourceProperties.AccountId'); } console.log('spotinst account id is: ', accountId); return accountId; @@ -265,11 +279,11 @@ module.exports.parseGroupConfig = function(config) { * @param {Object} event - Event from serverless call * @param {Object} context - Context from serverless call * @param {String} token - Spotinst API token - * @param {String} accountId - Spotinst Account Id + * @param {String} AccountId - Spotinst Account Id * * @property {String} asg name string */ -module.exports.createGroupFromImport = function(importedConfig, groupConfig, event, context, token, accountId) { +module.exports.createGroupFromImport = function(importedConfig, groupConfig, event, context, token, AccountId) { return new Promise((resolve, reject)=>{ if(groupConfig.capacity){ _.set(importedConfig, 'capacity', groupConfig.capacity); @@ -307,7 +321,7 @@ module.exports.createGroupFromImport = function(importedConfig, groupConfig, eve method: 'POST', url: 'https://api.spotinst.io/aws/ec2/group', qs: { - accountId, + AccountId, ignoreInitHealthChecks: createPolicy.ignoreInitHealthChecks }, headers: { @@ -387,7 +401,7 @@ module.exports.validateGroup = function(groupId, token, event, context) { method: 'GET', url: 'https://api.spotinst.io/aws/ec2/group/' + groupId, qs: { - accountId: module.exports.getSpotinstAccountId(event) + AccountId: module.exports.getSpotinstAccountId(event) }, headers: { 'content-type' : 'application/json', @@ -485,7 +499,7 @@ module.exports.updateGroup = function(obj) { method: 'PUT', url: 'https://api.spotinst.io/aws/ec2/group/' + obj.refId, qs: { - accountId: module.exports.getSpotinstAccountId(obj.event), + AccountId: module.exports.getSpotinstAccountId(obj.event), shouldResumeStateful: obj.updatePolicy.shouldResumeStateful }, headers: { @@ -541,7 +555,7 @@ module.exports.rollGroup = function(obj) { method: 'PUT', url: 'https://api.spotinst.io/aws/ec2/group/' + obj.refId + '/roll', qs: { - accountId: module.exports.getSpotinstAccountId(obj.event) + AccountId: module.exports.getSpotinstAccountId(obj.event) }, headers: { 'Content-Type' : 'application/json', @@ -591,7 +605,7 @@ module.exports.asgOperation = function(obj){ method:"POST", url:"https://api.spotinst.io/aws/ec2/group/" + obj.refId + "/asgOperation/scaleOnce", qs:{ - accountId : module.exports.getSpotinstAccountId(obj.event), + AccountId : module.exports.getSpotinstAccountId(obj.event), }, headers:{ 'Content-Type' : 'application/json', @@ -727,13 +741,13 @@ module.exports.getTokenAsync = function(obj){ return reject("Token creation failed: " + response.statusMessage); } - var accessToken = JSON.parse(body)['response']['items'][0]['accessToken']; + var AccessToken = JSON.parse(body)['response']['items'][0]['AccessToken']; - return resolve(accessToken); + return resolve(AccessToken); }); } - else if(config.accessToken) { - return resolve(config.accessToken); + else if(config.AccessToken) { + return resolve(config.AccessToken); } else { return reject("No valid long or short term credentials provided"); @@ -761,7 +775,7 @@ module.exports.validateOceanCluster = function(clusterId, token, event, context) method: 'GET', url: 'https://api.spotinst.io/ocean/aws/k8s/cluster/' + clusterId, qs: { - accountId: module.exports.getSpotinstAccountId(event) + AccountId: module.exports.getSpotinstAccountId(event) }, headers: { 'content-type' : 'application/json', From dbca9b08cbb86fe0280965d409cbfe3feb49b342 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 22 May 2019 14:56:23 -0700 Subject: [PATCH 2/2] [src] added update changes --- lib/util.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/util.js b/lib/util.js index c3df96b..0549584 100644 --- a/lib/util.js +++ b/lib/util.js @@ -38,7 +38,7 @@ function getToken(event, cb) { if(response.statusCode > 201) return cb("Token creation failed: " + response.statusMessage); var accessToken; - var group = JSON.parse(body)['response']['items'][0] + var group = JSON.parse(body)['response']['items'][0]; if (group['AccessToken']) { accessToken = group['AccessToken'] @@ -279,11 +279,11 @@ module.exports.parseGroupConfig = function(config) { * @param {Object} event - Event from serverless call * @param {Object} context - Context from serverless call * @param {String} token - Spotinst API token - * @param {String} AccountId - Spotinst Account Id + * @param {String} accountId - Spotinst Account Id * * @property {String} asg name string */ -module.exports.createGroupFromImport = function(importedConfig, groupConfig, event, context, token, AccountId) { +module.exports.createGroupFromImport = function(importedConfig, groupConfig, event, context, token, accountId) { return new Promise((resolve, reject)=>{ if(groupConfig.capacity){ _.set(importedConfig, 'capacity', groupConfig.capacity); @@ -321,7 +321,7 @@ module.exports.createGroupFromImport = function(importedConfig, groupConfig, eve method: 'POST', url: 'https://api.spotinst.io/aws/ec2/group', qs: { - AccountId, + accountId, ignoreInitHealthChecks: createPolicy.ignoreInitHealthChecks }, headers: { @@ -401,7 +401,7 @@ module.exports.validateGroup = function(groupId, token, event, context) { method: 'GET', url: 'https://api.spotinst.io/aws/ec2/group/' + groupId, qs: { - AccountId: module.exports.getSpotinstAccountId(event) + accountId: module.exports.getSpotinstAccountId(event) }, headers: { 'content-type' : 'application/json', @@ -499,7 +499,7 @@ module.exports.updateGroup = function(obj) { method: 'PUT', url: 'https://api.spotinst.io/aws/ec2/group/' + obj.refId, qs: { - AccountId: module.exports.getSpotinstAccountId(obj.event), + accountId: module.exports.getSpotinstAccountId(obj.event), shouldResumeStateful: obj.updatePolicy.shouldResumeStateful }, headers: { @@ -555,7 +555,7 @@ module.exports.rollGroup = function(obj) { method: 'PUT', url: 'https://api.spotinst.io/aws/ec2/group/' + obj.refId + '/roll', qs: { - AccountId: module.exports.getSpotinstAccountId(obj.event) + accountId: module.exports.getSpotinstAccountId(obj.event) }, headers: { 'Content-Type' : 'application/json', @@ -605,7 +605,7 @@ module.exports.asgOperation = function(obj){ method:"POST", url:"https://api.spotinst.io/aws/ec2/group/" + obj.refId + "/asgOperation/scaleOnce", qs:{ - AccountId : module.exports.getSpotinstAccountId(obj.event), + accountId : module.exports.getSpotinstAccountId(obj.event), }, headers:{ 'Content-Type' : 'application/json', @@ -740,15 +740,25 @@ module.exports.getTokenAsync = function(obj){ if(response.statusCode > 201) { return reject("Token creation failed: " + response.statusMessage); } + + var accessToken; + var group = JSON.parse(body)['response']['items'][0]; + + if (group['AccessToken']) { + accessToken = group['AccessToken'] + } else if (group['accessToken']) { + accessToken = group['accessToken'] + } - var AccessToken = JSON.parse(body)['response']['items'][0]['AccessToken']; - - return resolve(AccessToken); + return resolve(accessToken); }); } else if(config.AccessToken) { return resolve(config.AccessToken); } + else if(config.accessToken) { + return resolve(config.accessToken); + } else { return reject("No valid long or short term credentials provided"); } @@ -775,7 +785,7 @@ module.exports.validateOceanCluster = function(clusterId, token, event, context) method: 'GET', url: 'https://api.spotinst.io/ocean/aws/k8s/cluster/' + clusterId, qs: { - AccountId: module.exports.getSpotinstAccountId(event) + accountId: module.exports.getSpotinstAccountId(event) }, headers: { 'content-type' : 'application/json',