From e9ef0c7acf875c230ef8bbdd0d90d3f32bb64b79 Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Mon, 15 Feb 2016 19:21:55 +0700 Subject: [PATCH] finished refactoring actions to use Provider class. NEEDS TESTING --- lib/actions/EndpointDeployApiGateway.js | 2 +- lib/actions/EnvSet.js | 19 +++++++++++-------- lib/actions/EnvUnset.js | 18 ++++++++++-------- lib/actions/EventDeployS3Lambda.js | 16 ++++------------ lib/actions/EventDeploySNSLambda.js | 17 ++++------------- lib/actions/EventDeployScheduledLambda.js | 19 +++++-------------- lib/actions/EventDeployStreamLambda.js | 12 +++--------- 7 files changed, 38 insertions(+), 65 deletions(-) diff --git a/lib/actions/EndpointDeployApiGateway.js b/lib/actions/EndpointDeployApiGateway.js index 84e13077c..b82d31b96 100644 --- a/lib/actions/EndpointDeployApiGateway.js +++ b/lib/actions/EndpointDeployApiGateway.js @@ -50,7 +50,7 @@ module.exports = function(SPlugin, serverlessPath) { secretAccessKey: _this.S.config.awsAdminSecretKey }; - _this.aws = _this.S.getProvider('aws'); + _this.aws = _this.S.getProvider('aws'); return _this._validateAndPrepare() .bind(_this) diff --git a/lib/actions/EnvSet.js b/lib/actions/EnvSet.js index ebd901b86..e95d0b7e9 100644 --- a/lib/actions/EnvSet.js +++ b/lib/actions/EnvSet.js @@ -10,7 +10,6 @@ module.exports = function(SPlugin, serverlessPath) { SError = require(path.join(serverlessPath, 'ServerlessError')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), - awsMisc = require(path.join(serverlessPath, 'utils/aws/Misc')), SUtils = require(path.join(serverlessPath, 'utils')); /** @@ -168,7 +167,7 @@ usage: serverless env set`, _setEnvVar(){ let _this = this; - return awsMisc.getEnvFiles(_this.S, _this.evt.options.region, _this.evt.options.stage) + return SUtils.getEnvFiles(_this.S, _this.evt.options.region, _this.evt.options.stage) .then(envMapsByRegion => { let putEnvQ = []; @@ -191,13 +190,17 @@ usage: serverless env set`, bucketName = _this.S.state.meta.get().variables.projectBucket, bucketRegion = SUtils.getProjectBucketRegion(_this.S.state.meta.get().variables); - let awsConfig = { - region: bucketRegion, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey, + _this.aws = _this.S.getProvider('aws'); + + let params = { + Bucket: bucketName, + Key: ['serverless', projectName, _this.evt.options.stage, mapForRegion.region, 'envVars', '.env'].join('/'), + ACL: 'private', + ContentType: 'text/plain', + Body: contents }; - let S3 = require('../utils/aws/S3')(awsConfig); - putEnvQ.push(S3.sPutEnvFile(bucketName, projectName, _this.evt.options.stage, mapForRegion.region, contents)); + + putEnvQ.push(_this.aws.request('S3', 'putObject', params, _this.evt.options.stage, bucketRegion)); } }); diff --git a/lib/actions/EnvUnset.js b/lib/actions/EnvUnset.js index e3ccabd1e..085ea1d5d 100644 --- a/lib/actions/EnvUnset.js +++ b/lib/actions/EnvUnset.js @@ -10,7 +10,6 @@ module.exports = function(SPlugin, serverlessPath) { SError = require(path.join(serverlessPath, 'ServerlessError')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), - awsMisc = require(path.join(serverlessPath, 'utils/aws/Misc')), SUtils = require(path.join(serverlessPath, 'utils')); /** @@ -159,7 +158,7 @@ usage: serverless env unset`, _unsetEnvVar(){ let _this = this; - return awsMisc.getEnvFiles(_this.S, _this.evt.options.region, _this.evt.options.stage) + return SUtils.getEnvFiles(_this.S, _this.evt.options.region, _this.evt.options.stage) .then(envMapsByRegion => { let putEnvQ = []; @@ -182,14 +181,17 @@ usage: serverless env unset`, bucketName = _this.S.state.meta.get().variables.projectBucket, bucketRegion = SUtils.getProjectBucketRegion(_this.S.state.meta.get().variables); - let awsConfig = { - region: bucketRegion, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey + _this.aws = _this.S.getProvider('aws'); + + let params = { + Bucket: bucketName, + Key: ['serverless', projectName, _this.evt.options.stage, mapForRegion.region, 'envVars', '.env'].join('/'), + ACL: 'private', + ContentType: 'text/plain', + Body: contents }; - let S3 = require('../utils/aws/S3')(awsConfig); - putEnvQ.push(S3.sPutEnvFile(bucketName, projectName, _this.evt.options.stage, mapForRegion.region, contents)); + putEnvQ.push(_this.aws.request('S3', 'putObject', params, _this.evt.options.stage, bucketRegion)); } }); diff --git a/lib/actions/EventDeployS3Lambda.js b/lib/actions/EventDeployS3Lambda.js index 0883fe648..ebc992026 100644 --- a/lib/actions/EventDeployS3Lambda.js +++ b/lib/actions/EventDeployS3Lambda.js @@ -56,21 +56,14 @@ module.exports = function(SPlugin, serverlessPath) { awsAccountId = _this.S.state.meta.get().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables.iamRoleArnLambda.split('::')[1].split(':')[0], lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage; - let awsConfig = { - region: _this.evt.options.region, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey - }; - - _this.S3 = require('../utils/aws/S3')(awsConfig); - _this.Lambda = require('../utils/aws/Lambda')(awsConfig); + _this.aws = _this.S.getProvider('aws'); let params = { FunctionName: lambdaArn, StatementId: statementId, Qualifier: _this.evt.options.stage }; - return _this.Lambda.removePermissionPromised(params) + return _this.aws.request('Lambda', 'removePermission', params, _this.evt.options.stage, _this.evt.options.region) .then(function(data) { SUtils.sDebug(`Removed lambda permission with statement ID: ${statementId}`); }) @@ -87,7 +80,7 @@ module.exports = function(SPlugin, serverlessPath) { SourceArn: 'arn:aws:s3:::' + populatedEvent.config.bucket, Qualifier: _this.evt.options.stage }; - return _this.Lambda.addPermissionPromised(params); + return _this.aws.request('Lambda', 'addPermission', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function(data) { let params = { @@ -101,8 +94,7 @@ module.exports = function(SPlugin, serverlessPath) { ] } }; - - return _this.S3.putBucketNotificationConfigurationPromised(params) + return _this.aws.request('S3', 'putBucketNotificationConfiguration', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function(data) { diff --git a/lib/actions/EventDeploySNSLambda.js b/lib/actions/EventDeploySNSLambda.js index f52826a69..beb403f6c 100644 --- a/lib/actions/EventDeploySNSLambda.js +++ b/lib/actions/EventDeploySNSLambda.js @@ -58,22 +58,14 @@ module.exports = function(SPlugin, serverlessPath) { topicArn = 'arn:aws:sns:' + _this.evt.options.region + ':' + awsAccountId + ':' + populatedEvent.config.topicName, lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage; - let awsConfig = { - region: _this.evt.options.region, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey - }; - - _this.SNS = require('../utils/aws/SNS')(awsConfig); - _this.Lambda = require('../utils/aws/Lambda')(awsConfig); - + _this.aws = _this.S.getProvider('aws'); let params = { FunctionName: lambdaArn, StatementId: statementId, Qualifier: _this.evt.options.stage }; - return _this.Lambda.removePermissionPromised(params) + return _this.aws.request('Lambda', 'removePermission', params, _this.evt.options.stage, _this.evt.options.region) .then(function(data) { SUtils.sDebug(`Removed lambda permission with statement ID: ${statementId}`); }) @@ -89,7 +81,7 @@ module.exports = function(SPlugin, serverlessPath) { Principal: 'sns.amazonaws.com', Qualifier: _this.evt.options.stage }; - return _this.Lambda.addPermissionPromised(params); + return _this.aws.request('Lambda', 'addPermission', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function(data) { let params = { @@ -97,8 +89,7 @@ module.exports = function(SPlugin, serverlessPath) { TopicArn: topicArn, Endpoint: lambdaArn }; - - return _this.SNS.subscribePromised(params) + return _this.aws.request('SNS', 'subscribe', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function(data){ SUtils.sDebug(`Subscription to SNS topic ${topicArn} added for lambda ${lambdaArn}`); diff --git a/lib/actions/EventDeployScheduledLambda.js b/lib/actions/EventDeployScheduledLambda.js index d12dbc64f..b65b9db94 100644 --- a/lib/actions/EventDeployScheduledLambda.js +++ b/lib/actions/EventDeployScheduledLambda.js @@ -59,14 +59,7 @@ module.exports = function (SPlugin, serverlessPath) { populatedEvent.config.enabled = populatedEvent.config.enabled ? 'ENABLED' : 'DISABLED'; - let awsConfig = { - region: _this.evt.options.region, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey - }; - - _this.CloudWatchEvents = require('../utils/aws/CloudWatchEvents')(awsConfig); - _this.Lambda = require('../utils/aws/Lambda')(awsConfig); + _this.aws = _this.S.getProvider('aws'); SUtils.sDebug(`Putting CloudWatchEvents Rule ${ruleName}`); @@ -75,9 +68,7 @@ module.exports = function (SPlugin, serverlessPath) { ScheduleExpression: populatedEvent.config.schedule, State: populatedEvent.config.enabled }; - - return _this.CloudWatchEvents.putRuleAsync(params) - + return _this.aws.request('CloudWatchEvents', 'putRule', params, _this.evt.options.stage, _this.evt.options.region) .then(function (data) { // First remove permissions so we can set them again let params = { @@ -85,7 +76,7 @@ module.exports = function (SPlugin, serverlessPath) { StatementId: 's_events_' + ruleName + "_" + stage, Qualifier: _this.evt.options.stage }; - return _this.Lambda.removePermissionPromised(params) + return _this.aws.request('Lambda', 'removePermission', params, _this.evt.options.stage, _this.evt.options.region) .then(function(data) { SUtils.sDebug( '"' @@ -107,7 +98,7 @@ module.exports = function (SPlugin, serverlessPath) { Principal: 'events.amazonaws.com', Qualifier: _this.evt.options.stage }; - return _this.Lambda.addPermissionPromised(params); + return _this.aws.request('Lambda', 'addPermission', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function (data) { @@ -122,7 +113,7 @@ module.exports = function (SPlugin, serverlessPath) { } ] }; - return _this.CloudWatchEvents.putTargetsAsync(params) + return _this.aws.request('CloudWatchEvents', 'putTargets', params, _this.evt.options.stage, _this.evt.options.region) .then(function(data){ return BbPromise.resolve(data); }); diff --git a/lib/actions/EventDeployStreamLambda.js b/lib/actions/EventDeployStreamLambda.js index 4c5c35d03..2021dc1da 100644 --- a/lib/actions/EventDeployStreamLambda.js +++ b/lib/actions/EventDeployStreamLambda.js @@ -51,13 +51,7 @@ module.exports = function(SPlugin, serverlessPath) { return BbPromise.reject(new SError(`Missing stage, region, or event path.`)); } - let awsConfig = { - region: _this.evt.options.region, - accessKeyId: _this.S.config.awsAdminKeyId, - secretAccessKey: _this.S.config.awsAdminSecretKey - }; - - _this.Lambda = require('../utils/aws/Lambda')(awsConfig); + _this.aws = _this.S.getProvider('aws'); let event = _this.S.getProject().getEvent( _this.evt.options.path ), populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}), @@ -81,7 +75,7 @@ module.exports = function(SPlugin, serverlessPath) { if (regionVars[eventVar]) { params.UUID = regionVars[eventVar]; - return _this.Lambda.updateEventSourceMappingPromised(params) + return _this.aws.request('Lambda', 'updateEventSourceMapping', params, _this.evt.options.stage, _this.evt.options.region) .then(function (data) { SUtils.sDebug(`updated stream event source ${populatedEvent.config.streamArn} for lambda ${lambdaArn}`); @@ -94,7 +88,7 @@ module.exports = function(SPlugin, serverlessPath) { params.EventSourceArn = populatedEvent.config.streamArn; params.StartingPosition = populatedEvent.config.startingPosition; - return _this.Lambda.createEventSourceMappingPromised(params) + return _this.aws.request('Lambda', 'createEventSourceMapping', params, _this.evt.options.stage, _this.evt.options.region) .then(function (data) { SUtils.sDebug(`Created stream event source ${populatedEvent.config.streamArn} for lambda ${lambdaArn}`);