Moved getEvents() to project.

Extraced Project._filterSPaths
This commit is contained in:
Kamil Burzynski 2016-02-11 19:23:37 +01:00
parent f26a1cc13d
commit 8f9ea9773d
10 changed files with 76 additions and 117 deletions

View File

@ -399,6 +399,10 @@ class ServerlessFunction {
return component.getRuntime();
}
getAllEvents() {
return this.events;
}
/**
* Get Project
* - Returns reference to the instance

View File

@ -403,6 +403,24 @@ class ServerlessProject {
})
}
_filterSPaths( collection, options ) {
// TODO: remove this, caller should be filtering such stuff later
let paths = _.get( options, 'paths' );
if( paths && (paths.length > 0) ) {
collection = _.filter( collection, f =>
_.any( paths, p =>
p.indexOf( f.getSPath() ) !== -1;
)
)
}
if( _.get( options, 'returnPaths' ) == true ){
collection = _.map( collection, func => func.getSPath(); );
}
return collection;
}
/**
* Get Functions
* - Returns an array of this project's function instances
@ -412,25 +430,9 @@ class ServerlessProject {
*/
getAllFunctions(options) {
let allFunctions = _.flatten( _.map( _.values( this.components ), component =>
return this._filterSPaths( _.flatten( _.map( _.values( this.components ), component =>
component.getAllFunctions();
));
// TODO: remove this, caller should be filtering such stuff later
let paths = _.get( options, 'paths' );
if( paths ) {
allFunctions = _.filter( allFunctions, f =>
_.any( paths, p =>
p.indexOf( f.getSPath() ) !== -1;
)
)
}
if( _.get( options, 'returnPaths' ) == true ){
return _.map( allFunctions, func => func.getSPath(); );
}
return allFunctions;
)), options );
}
getFunction( path ){
@ -447,18 +449,7 @@ class ServerlessProject {
*/
getAllComponents(options){
let c = _.values( this.components );
let paths = _.get( options, 'paths' );
if( paths ) {
c = _.filter( c, component =>
_.any( paths, p =>
p.indexOf( component.getSPath() ) !== -1;
)
)
}
return c;
return this._filterSPaths( this.components, options );
}
getComponent( path ){
@ -466,6 +457,23 @@ class ServerlessProject {
c.getSPath().indexOf( path ) !== -1;
)
}
/**
* Get Events
* - Returns an array of this state's Events instances
* - Options: paths, component, module, function, event name
* - options.paths is an array of Serverless paths like this: ['component/module/function#eventName']
*/
getEvents(options) {
return this._filterSPaths( _.flatten( _.map( this.getAllFunctions(), f => f.getAllEvents() ) ), options );
}
getEvent( path ){
return _.find( _.values( this.getAllEvents() ), e =>
e.getSPath().indexOf( path ) !== -1;
)
}
}
module.exports = ServerlessProject;

View File

@ -178,6 +178,9 @@ class ServerlessState {
allEndpoints = [],
foundEndpoints = [];
// Default options.paths
if (options && !options.paths) options.paths = [];
// Get all functions
for (let i = 0; i < Object.keys(_this._S.getProject().components).length; i++) {
let component = _this._S.getProject().components[Object.keys(_this._S.getProject().components)[i]];
@ -190,12 +193,12 @@ class ServerlessState {
}
}
// Default options.paths
options = options ? options : {};
options.paths = options.paths ? options.paths : [];
// Return if no options specified
if (!options) return allEndpoints;
if (options && Object.keys(options).length === 1 && options.returnPaths === true) return allEndpoints.map(function(d) { return d._config.sPath }); // XXX
// If component, module or functions convert to sPath
// TODO: Back Compat Support -- Eventually remove and support sPath only
// TODO: Eventually remove and support sPath only
if (options.component) {
options.paths = [options.component];
if (options.module) options.paths[0] = options.paths[0] + '/' + options.module;
@ -204,18 +207,13 @@ class ServerlessState {
if (options.endpointMethod) options.paths[0] = options.paths[0] + '~' + options.endpointMethod;
}
// Return if no paths
if (!options.paths.length) {
return options.returnPaths ? allEndpoints.map(function(d) { return d._config.sPath }) : allEndpoints;
}
// If options specified, loop through functions and find the ones specified
for (let i = 0; i < allEndpoints.length; i++) {
let endpoint = allEndpoints[i];
for (let j = 0; j < options.paths.length; j++) {
if (endpoint._config.sPath.indexOf(options.paths[j]) !== -1) {
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint);
if (endpoint._config.sPath.indexOf(options.paths[j]) !== -1) { // XXX
foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint); // XXX
break;
}
}
@ -236,56 +234,6 @@ class ServerlessState {
return foundEndpoints;
}
/**
* Get Events
* - Returns an array of this state's Events instances
* - Options: paths, component, module, function, event name
* - options.paths is an array of Serverless paths like this: ['component/module/function#eventName']
*/
getEvents(options) {
let _this = this,
allFunctions = [],
allEvents = [],
foundEvents = [];
// If options.component, options.module or options.function, throw error
if (options && (options.component || options.module || options.function)) {
throw new SError('options.component, options.module, options.function is not supported.');
}
// Get all events
allFunctions = this.getFunctions();
for (let i = 0; i < allFunctions.length; i++) {
if (allFunctions[i].events) {
allEvents = allEvents.concat(allFunctions[i].events);
}
}
// Default options.paths
options = options ? options : {};
options.paths = options.paths ? options.paths : [];
// Return if no paths
if (!options.paths.length) {
return options.returnPaths ? allEvents.map(function(d) { return d._config.sPath }) : allEvents;
}
// If options specified, loop through functions and find the ones specified
for (let i = 0; i < allEvents.length; i++) {
let event = allEvents[i];
for (let j = 0; j < options.paths.length; j++) {
if (event._config.sPath.indexOf(options.paths[j]) !== -1) {
foundEvents.push(options.returnPaths ? event._config.sPath : event);
break;
}
}
}
return foundEvents;
}
/**
* Validate Stage Exists
* - Checks to see if a stage exists in your project

View File

@ -163,7 +163,7 @@ module.exports = function(SPlugin, serverlessPath) {
throw new SError(`You must be in a component to deploy events`);
}
_this.evt.options.paths = _this.S.state.getEvents({
_this.evt.options.paths = _this.S.getProject().getEvents({
paths: [sPath],
returnPaths: true
});
@ -177,7 +177,7 @@ module.exports = function(SPlugin, serverlessPath) {
// If --all is selected, load all paths
if (_this.evt.options.all) {
_this.evt.options.paths = _this.S.state.getEvents({ returnPaths: true });
_this.evt.options.paths = _this.S.getProject().getEvents({ returnPaths: true });
}
// Validate Stage
@ -231,7 +231,7 @@ module.exports = function(SPlugin, serverlessPath) {
async.eachSeries(_this.evt.options.paths, function(path, eCb) {
let event = _this.S.state.getEvents({ paths: [path] })[0];
let event = _this.S.getProject().getEvent( path );
if(!event) throw new SError(`Event could not be found: ${path}`);

View File

@ -49,7 +49,7 @@ module.exports = function(SPlugin, serverlessPath) {
if (!_this.evt.options.stage || !_this.evt.options.region || !_this.evt.options.path) {
return BbPromise.reject(new SError(`Missing stage, region or path.`));
}
let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0],
let event = _this.S.getProject().getEvent( _this.evt.options.path ),
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options),
statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage,

View File

@ -50,7 +50,7 @@ module.exports = function(SPlugin, serverlessPath) {
return BbPromise.reject(new SError(`Missing stage, region or path.`));
}
let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0],
let event = _this.S.getProject().getEvent( _this.evt.options.path ),
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options),
statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage,

View File

@ -49,7 +49,7 @@ module.exports = function (SPlugin, serverlessPath) {
if (!_this.evt.options.stage || !_this.evt.options.region || !_this.evt.options.path) {
return BbPromise.reject(new SError(`Missing stage, region or path.`));
}
let event = _this.S.state.getEvents({paths: [_this.evt.options.path]})[0],
let event = _this.S.getProject().getEvent( _this.evt.options.path ),
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options),
ruleName = functionName + '-' + event.name,

View File

@ -59,7 +59,7 @@ module.exports = function(SPlugin, serverlessPath) {
_this.Lambda = require('../utils/aws/Lambda')(awsConfig);
let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0],
let event = _this.S.getProject().getEvent( _this.evt.options.path ),
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options),
regionVars = _this.S.state.getMeta().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables,

View File

@ -135,6 +135,24 @@ describe('Test Serverless Project Class', function() {
done();
});
it('Get events w/o paths', function(done) {
let events = instance.getEvents();
assert.equal(true, events.length === 4);
done();
});
it('Get events w paths', function(done) {
let events = instance.getEvents({ paths: ['nodejscomponent/group1/function1#s3'] });
assert.equal(true, events.length === 1);
done();
});
it('Get events w partial paths', function(done) {
let events = instance.getEvents({ paths: ['nodejscomponent/group1'] });
assert.equal(true, events.length === 4);
done();
});
it('Create new and save', function(done) {
// TODO: Project creation is an unholy mess now. It currently is done partially outside of Project class,
// split between ServerlessState and Meta classes, ProjectInit action, and ServerlessProject itself.

View File

@ -170,25 +170,6 @@ describe('Test Serverless State Class', function() {
done();
});
// asfasf
it('Get events w/o paths', function(done) {
let events = instance.getEvents();
assert.equal(true, events.length === 4);
done();
});
it('Get events w paths', function(done) {
let events = instance.getEvents({ paths: ['nodejscomponent/group1/function1#s3'] });
assert.equal(true, events.length === 1);
done();
});
it('Get events w partial paths', function(done) {
let events = instance.getEvents({ paths: ['nodejscomponent/group1'] });
assert.equal(true, events.length === 4);
done();
});
it('Validate stage exists', function(done) {
assert.equal(true, instance.validateStageExists(config.stage));
assert.equal(false, instance.validateStageExists('invalid'));