finished refactoring actions to use Provider class. NEEDS TESTING

This commit is contained in:
Eslam A. Hefnawy 2016-02-15 19:21:55 +07:00
parent 79bed1ee63
commit e9ef0c7acf
7 changed files with 38 additions and 65 deletions

View File

@ -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)

View File

@ -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));
}
});

View File

@ -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));
}
});

View File

@ -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) {

View File

@ -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}`);

View File

@ -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);
});

View File

@ -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}`);