Skip to content

Commit 2d35db6

Browse files
authored
Merge pull request #16 from horike37/add-all-functions-deployment
Add all functions deployment
2 parents 656d8d5 + 28247d6 commit 2d35db6

File tree

13 files changed

+1824
-1153
lines changed

13 files changed

+1824
-1153
lines changed

index.js

Lines changed: 0 additions & 516 deletions
This file was deleted.

index.test.js

Lines changed: 0 additions & 635 deletions
This file was deleted.

lib/dataProcessing.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
const BbPromise = require('bluebird');
3+
const path = require('path');
4+
const _ = require('lodash');
5+
6+
module.exports = {
7+
functionArns: {},
8+
yamlParse() {
9+
const servicePath = this.serverless.config.servicePath;
10+
11+
if (!servicePath) {
12+
return BbPromise.resolve();
13+
}
14+
15+
const serverlessYmlPath = path.join(servicePath, 'serverless.yml');
16+
return this.serverless.yamlParser
17+
.parse(serverlessYmlPath)
18+
.then((serverlessFileParam) => {
19+
this.serverless.service.stepFunctions = serverlessFileParam.stepFunctions.stateMachine;
20+
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
21+
return BbPromise.resolve();
22+
});
23+
},
24+
25+
parseInputdate() {
26+
if (!this.options.data && this.options.path) {
27+
const absolutePath = path.isAbsolute(this.options.path) ?
28+
this.options.path :
29+
path.join(this.serverless.config.servicePath, this.options.path);
30+
if (!this.serverless.utils.fileExistsSync(absolutePath)) {
31+
throw new this.serverless.classes.Error('The file you provided does not exist.');
32+
}
33+
this.options.data = JSON.stringify(this.serverless.utils.readFileSync(absolutePath));
34+
}
35+
return BbPromise.resolve();
36+
},
37+
38+
getFunctionArns() {
39+
return this.provider.request('STS',
40+
'getCallerIdentity',
41+
{},
42+
this.options.stage,
43+
this.options.region)
44+
.then((result) => {
45+
_.forEach(this.serverless.service.functions, (value, key) => {
46+
this.functionArns[key]
47+
= `arn:aws:lambda:${this.region}:${result.Account}:function:${value.name}`;
48+
});
49+
return BbPromise.resolve();
50+
});
51+
},
52+
53+
compile() {
54+
if (!this.serverless.service.stepFunctions) {
55+
const errorMessage = [
56+
'stepFunctions statement does not exists in serverless.yml',
57+
].join('');
58+
throw new this.serverless.classes.Error(errorMessage);
59+
}
60+
61+
if (typeof this.serverless.service.stepFunctions[this.options.state] === 'undefined') {
62+
const errorMessage = [
63+
`Step function "${this.options.state}" is not exists`,
64+
].join('');
65+
throw new this.serverless.classes.Error(errorMessage);
66+
}
67+
68+
this.serverless.service.stepFunctions[this.options.state] =
69+
JSON.stringify(this.serverless.service.stepFunctions[this.options.state]);
70+
_.forEach(this.functionArns, (value, key) => {
71+
const regExp = new RegExp(`"Resource":"${key}"`, 'g');
72+
this.serverless.service.stepFunctions[this.options.state] =
73+
this.serverless.service.stepFunctions[this.options.state]
74+
.replace(regExp, `"Resource":"${value}"`);
75+
});
76+
return BbPromise.resolve();
77+
},
78+
79+
compileAll() {
80+
if (!this.serverless.service.stepFunctions) {
81+
const errorMessage = [
82+
'stepFunctions statement does not exists in serverless.yml',
83+
].join('');
84+
throw new this.serverless.classes.Error(errorMessage);
85+
}
86+
87+
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
88+
this.serverless.service.stepFunctions[stepFunctionKey] = JSON.stringify(stepFunctionObj);
89+
});
90+
91+
_.forEach(this.functionArns, (functionObj, functionKey) => {
92+
const regExp = new RegExp(`"Resource":"${functionKey}"`, 'g');
93+
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
94+
this.serverless.service.stepFunctions[stepFunctionKey] =
95+
this.serverless.service.stepFunctions[stepFunctionKey]
96+
.replace(regExp, `"Resource":"${functionObj}"`);
97+
});
98+
});
99+
return BbPromise.resolve();
100+
},
101+
};

lib/dataProcessing.test.js

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const BbPromise = require('bluebird');
5+
const sinon = require('sinon');
6+
const Serverless = require('serverless/lib/Serverless');
7+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
8+
const ServerlessStepFunctions = require('./index');
9+
10+
describe('dataProsessing', () => {
11+
let serverless;
12+
let serverlessStepFunctions;
13+
14+
beforeEach(() => {
15+
serverless = new Serverless();
16+
serverless.servicePath = true;
17+
serverless.service.service = 'step-functions';
18+
serverless.service.functions = {
19+
first: {
20+
handler: true,
21+
name: 'first',
22+
},
23+
};
24+
25+
const options = {
26+
stage: 'dev',
27+
region: 'us-east-1',
28+
function: 'first',
29+
functionObj: {
30+
name: 'first',
31+
},
32+
state: 'hellofunc',
33+
data: 'inputData',
34+
};
35+
36+
serverless.init();
37+
serverless.setProvider('aws', new AwsProvider(serverless));
38+
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
39+
});
40+
41+
describe('#yamlParse()', () => {
42+
let yamlParserStub;
43+
beforeEach(() => {
44+
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
45+
.returns(BbPromise.resolve({ stepFunctions: { stateMachine: 'stepFunctions' } }));
46+
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
47+
});
48+
49+
it('should yamlParse with correct params'
50+
, () => serverlessStepFunctions.yamlParse()
51+
.then(() => {
52+
expect(yamlParserStub.calledOnce).to.be.equal(true);
53+
expect(serverless.service.stepFunctions).to.be.equal('stepFunctions');
54+
serverlessStepFunctions.serverless.yamlParser.parse.restore();
55+
})
56+
);
57+
58+
it('should return resolve when servicePath does not exists', () => {
59+
serverlessStepFunctions.serverless.config.servicePath = null;
60+
serverlessStepFunctions.yamlParse()
61+
.then(() => {
62+
expect(yamlParserStub.callCount).to.be.equal(0);
63+
serverlessStepFunctions.serverless.yamlParser.parse.restore();
64+
});
65+
});
66+
67+
it('should return resolve when variables exists in the yaml', () => {
68+
serverlessStepFunctions.serverless.yamlParser.parse.restore();
69+
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
70+
.returns(BbPromise.resolve({ stepFunctions: { stateMachine: '${self:defaults.region}' } }));
71+
serverlessStepFunctions.yamlParse()
72+
.then(() => {
73+
expect(yamlParserStub.calledOnce).to.be.equal(true);
74+
expect(serverless.service.stepFunctions).to.be.equal('us-east-1');
75+
});
76+
});
77+
});
78+
79+
describe('#compile()', () => {
80+
it('should throw error when stepFunction state does not exists', () => {
81+
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
82+
});
83+
84+
it('should throw error when stateMachine name does not exists', () => {
85+
serverlessStepFunctions.stepFunctions = {};
86+
expect(() => serverlessStepFunctions.compile()).to.throw(Error);
87+
});
88+
89+
it('should comple with correct params', () => {
90+
serverless.service.stepFunctions = {
91+
hellofunc: {
92+
States: {
93+
HelloWorld: {
94+
Resource: 'first',
95+
},
96+
},
97+
},
98+
};
99+
serverlessStepFunctions.functionArns.first = 'lambdaArn';
100+
serverlessStepFunctions.compile().then(() => {
101+
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc)
102+
.to.be.equal('{"States":{"HelloWorld":{"Resource":"lambdaArn"}}}');
103+
});
104+
});
105+
106+
it('should comple with correct params when nested Resource', () => {
107+
serverlessStepFunctions.serverless.service.stepFunctions = {
108+
hellofunc: {
109+
States: {
110+
HelloWorld: {
111+
Resource: 'first',
112+
HelloWorld: {
113+
Resource: 'first',
114+
HelloWorld: {
115+
Resource: 'first',
116+
},
117+
},
118+
},
119+
},
120+
},
121+
};
122+
123+
let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"';
124+
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
125+
serverlessStepFunctions.functionArns.first = 'lambdaArn';
126+
serverlessStepFunctions.compile().then(() => {
127+
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
128+
});
129+
});
130+
});
131+
132+
describe('#parseInputdate()', () => {
133+
beforeEach(() => {
134+
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
135+
sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(true);
136+
sinon.stub(serverlessStepFunctions.serverless.utils, 'readFileSync')
137+
.returns({ foo: 'var' });
138+
serverlessStepFunctions.options.data = null;
139+
serverlessStepFunctions.options.path = 'data.json';
140+
});
141+
142+
it('should throw error if file does not exists', () => {
143+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
144+
sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(false);
145+
expect(() => serverlessStepFunctions.parseInputdate()).to.throw(Error);
146+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
147+
});
148+
149+
it('should parse file if path param is provided'
150+
, () => serverlessStepFunctions.parseInputdate().then(() => {
151+
expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}');
152+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
153+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
154+
})
155+
);
156+
157+
it('should parse file if path param is provided when absolute path', () => {
158+
serverlessStepFunctions.options.path = '/data.json';
159+
serverlessStepFunctions.parseInputdate().then(() => {
160+
expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}');
161+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
162+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
163+
});
164+
});
165+
166+
it('should return resolve if path param is not provided', () => {
167+
serverlessStepFunctions.options.path = null;
168+
return serverlessStepFunctions.parseInputdate().then(() => {
169+
expect(serverlessStepFunctions.options.data).to.deep.equal(null);
170+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
171+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
172+
});
173+
});
174+
});
175+
176+
describe('#getFunctionArns()', () => {
177+
let getCallerIdentityStub;
178+
beforeEach(() => {
179+
getCallerIdentityStub = sinon.stub(serverlessStepFunctions.provider, 'request')
180+
.returns(BbPromise.resolve({ Account: 1234 }));
181+
});
182+
183+
it('should getFunctionArns with correct params', () => serverlessStepFunctions.getFunctionArns()
184+
.then(() => {
185+
expect(getCallerIdentityStub.calledOnce).to.be.equal(true);
186+
expect(getCallerIdentityStub.calledWithExactly(
187+
'STS',
188+
'getCallerIdentity',
189+
{},
190+
serverlessStepFunctions.options.stage,
191+
serverlessStepFunctions.options.region
192+
)).to.be.equal(true);
193+
expect(serverlessStepFunctions.functionArns.first).to.be
194+
.equal('arn:aws:lambda:us-east-1:1234:function:first');
195+
serverlessStepFunctions.provider.request.restore();
196+
})
197+
);
198+
});
199+
200+
describe('#compileAll()', () => {
201+
it('should throw error when stepFunction state does not exists', () => {
202+
expect(() => serverlessStepFunctions.compileAll()).to.throw(Error);
203+
});
204+
205+
it('should comple with correct params when nested Resource', () => {
206+
serverlessStepFunctions.serverless.service.stepFunctions = {
207+
hellofunc: {
208+
States: {
209+
HelloWorld: {
210+
Resource: 'first',
211+
HelloWorld: {
212+
Resource: 'first',
213+
HelloWorld: {
214+
Resource: 'first',
215+
},
216+
},
217+
},
218+
},
219+
},
220+
};
221+
222+
let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"';
223+
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
224+
serverlessStepFunctions.functionArns.first = 'lambdaArn';
225+
serverlessStepFunctions.compileAll().then(() => {
226+
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
227+
});
228+
});
229+
});
230+
});

0 commit comments

Comments
 (0)