Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
516 changes: 0 additions & 516 deletions index.js

This file was deleted.

635 changes: 0 additions & 635 deletions index.test.js

This file was deleted.

101 changes: 101 additions & 0 deletions lib/dataProcessing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const _ = require('lodash');

module.exports = {
functionArns: {},
yamlParse() {
const servicePath = this.serverless.config.servicePath;

if (!servicePath) {
return BbPromise.resolve();
}

const serverlessYmlPath = path.join(servicePath, 'serverless.yml');
return this.serverless.yamlParser
.parse(serverlessYmlPath)
.then((serverlessFileParam) => {
this.serverless.service.stepFunctions = serverlessFileParam.stepFunctions.stateMachine;
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
return BbPromise.resolve();
});
},

parseInputdate() {
if (!this.options.data && this.options.path) {
const absolutePath = path.isAbsolute(this.options.path) ?
this.options.path :
path.join(this.serverless.config.servicePath, this.options.path);
if (!this.serverless.utils.fileExistsSync(absolutePath)) {
throw new this.serverless.classes.Error('The file you provided does not exist.');
}
this.options.data = JSON.stringify(this.serverless.utils.readFileSync(absolutePath));
}
return BbPromise.resolve();
},

getFunctionArns() {
return this.provider.request('STS',
'getCallerIdentity',
{},
this.options.stage,
this.options.region)
.then((result) => {
_.forEach(this.serverless.service.functions, (value, key) => {
this.functionArns[key]
= `arn:aws:lambda:${this.region}:${result.Account}:function:${value.name}`;
});
return BbPromise.resolve();
});
},

compile() {
if (!this.serverless.service.stepFunctions) {
const errorMessage = [
'stepFunctions statement does not exists in serverless.yml',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}

if (typeof this.serverless.service.stepFunctions[this.options.state] === 'undefined') {
const errorMessage = [
`Step function "${this.options.state}" is not exists`,
].join('');
throw new this.serverless.classes.Error(errorMessage);
}

this.serverless.service.stepFunctions[this.options.state] =
JSON.stringify(this.serverless.service.stepFunctions[this.options.state]);
_.forEach(this.functionArns, (value, key) => {
const regExp = new RegExp(`"Resource":"${key}"`, 'g');
this.serverless.service.stepFunctions[this.options.state] =
this.serverless.service.stepFunctions[this.options.state]
.replace(regExp, `"Resource":"${value}"`);
});
return BbPromise.resolve();
},

compileAll() {
if (!this.serverless.service.stepFunctions) {
const errorMessage = [
'stepFunctions statement does not exists in serverless.yml',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}

_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
this.serverless.service.stepFunctions[stepFunctionKey] = JSON.stringify(stepFunctionObj);
});

_.forEach(this.functionArns, (functionObj, functionKey) => {
const regExp = new RegExp(`"Resource":"${functionKey}"`, 'g');
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
this.serverless.service.stepFunctions[stepFunctionKey] =
this.serverless.service.stepFunctions[stepFunctionKey]
.replace(regExp, `"Resource":"${functionObj}"`);
});
});
return BbPromise.resolve();
},
};
230 changes: 230 additions & 0 deletions lib/dataProcessing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
'use strict';

const expect = require('chai').expect;
const BbPromise = require('bluebird');
const sinon = require('sinon');
const Serverless = require('serverless/lib/Serverless');
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
const ServerlessStepFunctions = require('./index');

describe('dataProsessing', () => {
let serverless;
let serverlessStepFunctions;

beforeEach(() => {
serverless = new Serverless();
serverless.servicePath = true;
serverless.service.service = 'step-functions';
serverless.service.functions = {
first: {
handler: true,
name: 'first',
},
};

const options = {
stage: 'dev',
region: 'us-east-1',
function: 'first',
functionObj: {
name: 'first',
},
state: 'hellofunc',
data: 'inputData',
};

serverless.init();
serverless.setProvider('aws', new AwsProvider(serverless));
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
});

describe('#yamlParse()', () => {
let yamlParserStub;
beforeEach(() => {
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
.returns(BbPromise.resolve({ stepFunctions: { stateMachine: 'stepFunctions' } }));
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
});

it('should yamlParse with correct params'
, () => serverlessStepFunctions.yamlParse()
.then(() => {
expect(yamlParserStub.calledOnce).to.be.equal(true);
expect(serverless.service.stepFunctions).to.be.equal('stepFunctions');
serverlessStepFunctions.serverless.yamlParser.parse.restore();
})
);

it('should return resolve when servicePath does not exists', () => {
serverlessStepFunctions.serverless.config.servicePath = null;
serverlessStepFunctions.yamlParse()
.then(() => {
expect(yamlParserStub.callCount).to.be.equal(0);
serverlessStepFunctions.serverless.yamlParser.parse.restore();
});
});

it('should return resolve when variables exists in the yaml', () => {
serverlessStepFunctions.serverless.yamlParser.parse.restore();
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
.returns(BbPromise.resolve({ stepFunctions: { stateMachine: '${self:defaults.region}' } }));
serverlessStepFunctions.yamlParse()
.then(() => {
expect(yamlParserStub.calledOnce).to.be.equal(true);
expect(serverless.service.stepFunctions).to.be.equal('us-east-1');
});
});
});

describe('#compile()', () => {
it('should throw error when stepFunction state does not exists', () => {
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
});

it('should throw error when stateMachine name does not exists', () => {
serverlessStepFunctions.stepFunctions = {};
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
});

it('should comple with correct params', () => {
serverless.service.stepFunctions = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'first',
},
},
},
};
serverlessStepFunctions.functionArns.first = 'lambdaArn';
serverlessStepFunctions.compile().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc)
.to.be.equal('{"States":{"HelloWorld":{"Resource":"lambdaArn"}}}');
});
});

it('should comple with correct params when nested Resource', () => {
serverlessStepFunctions.serverless.service.stepFunctions = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'first',
HelloWorld: {
Resource: 'first',
HelloWorld: {
Resource: 'first',
},
},
},
},
},
};

let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"';
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
serverlessStepFunctions.functionArns.first = 'lambdaArn';
serverlessStepFunctions.compile().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
});
});
});

describe('#parseInputdate()', () => {
beforeEach(() => {
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(true);
sinon.stub(serverlessStepFunctions.serverless.utils, 'readFileSync')
.returns({ foo: 'var' });
serverlessStepFunctions.options.data = null;
serverlessStepFunctions.options.path = 'data.json';
});

it('should throw error if file does not exists', () => {
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(false);
expect(() => serverlessStepFunctions.parseInputdate()).to.throw(Error);
serverlessStepFunctions.serverless.utils.readFileSync.restore();
});

it('should parse file if path param is provided'
, () => serverlessStepFunctions.parseInputdate().then(() => {
expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}');
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
serverlessStepFunctions.serverless.utils.readFileSync.restore();
})
);

it('should parse file if path param is provided when absolute path', () => {
serverlessStepFunctions.options.path = '/data.json';
serverlessStepFunctions.parseInputdate().then(() => {
expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}');
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
serverlessStepFunctions.serverless.utils.readFileSync.restore();
});
});

it('should return resolve if path param is not provided', () => {
serverlessStepFunctions.options.path = null;
return serverlessStepFunctions.parseInputdate().then(() => {
expect(serverlessStepFunctions.options.data).to.deep.equal(null);
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
serverlessStepFunctions.serverless.utils.readFileSync.restore();
});
});
});

describe('#getFunctionArns()', () => {
let getCallerIdentityStub;
beforeEach(() => {
getCallerIdentityStub = sinon.stub(serverlessStepFunctions.provider, 'request')
.returns(BbPromise.resolve({ Account: 1234 }));
});

it('should getFunctionArns with correct params', () => serverlessStepFunctions.getFunctionArns()
.then(() => {
expect(getCallerIdentityStub.calledOnce).to.be.equal(true);
expect(getCallerIdentityStub.calledWithExactly(
'STS',
'getCallerIdentity',
{},
serverlessStepFunctions.options.stage,
serverlessStepFunctions.options.region
)).to.be.equal(true);
expect(serverlessStepFunctions.functionArns.first).to.be
.equal('arn:aws:lambda:us-east-1:1234:function:first');
serverlessStepFunctions.provider.request.restore();
})
);
});

describe('#compileAll()', () => {
it('should throw error when stepFunction state does not exists', () => {
expect(() => serverlessStepFunctions.compileAll()).to.throw(Error);
});

it('should comple with correct params when nested Resource', () => {
serverlessStepFunctions.serverless.service.stepFunctions = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'first',
HelloWorld: {
Resource: 'first',
HelloWorld: {
Resource: 'first',
},
},
},
},
},
};

let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"';
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
serverlessStepFunctions.functionArns.first = 'lambdaArn';
serverlessStepFunctions.compileAll().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
});
});
});
});
Loading