Skip to content

Commit ab89ad6

Browse files
authored
Merge pull request #41 from serverless/add-service-resource-merging
Add service resource merging
2 parents d8ccb94 + 4f6c137 commit ab89ad6

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

deploy/googleDeploy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const prepareDeployment = require('./lib/prepareDeployment');
88
const createDeployment = require('./lib/createDeployment');
99
const monitorDeployment = require('../shared/monitorDeployment');
1010
const generateArtifactDirectoryName = require('./lib/generateArtifactDirectoryName');
11+
const mergeServiceResources = require('./lib/mergeServiceResources');
1112
const uploadArtifacts = require('./lib/uploadArtifacts');
1213
const compileFunctions = require('./lib/compileFunctions');
1314
const updateDeployment = require('./lib/updateDeployment');
@@ -27,6 +28,7 @@ class GoogleDeploy {
2728
createDeployment,
2829
monitorDeployment,
2930
generateArtifactDirectoryName,
31+
mergeServiceResources,
3032
uploadArtifacts,
3133
compileFunctions,
3234
updateDeployment,
@@ -48,6 +50,7 @@ class GoogleDeploy {
4850
.then(this.compileFunctions),
4951

5052
'deploy:deploy': () => BbPromise.bind(this)
53+
.then(this.mergeServiceResources)
5154
.then(this.uploadArtifacts)
5255
.then(this.updateDeployment)
5356
.then(this.cleanupDeploymentBucket),

deploy/googleDeploy.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('GoogleDeploy', () => {
4141
let prepareDeploymentStub;
4242
let createDeploymentStub;
4343
let generateArtifactDirectoryNameStub;
44+
let mergeServiceResourcesStub;
4445
let compileFunctionsStub;
4546
let uploadArtifactsStub;
4647
let updateDeploymentStub;
@@ -58,6 +59,8 @@ describe('GoogleDeploy', () => {
5859
generateArtifactDirectoryNameStub = sinon
5960
.stub(googleDeploy, 'generateArtifactDirectoryName')
6061
.returns(BbPromise.resolve());
62+
mergeServiceResourcesStub = sinon.stub(googleDeploy, 'mergeServiceResources')
63+
.returns(BbPromise.resolve());
6164
compileFunctionsStub = sinon.stub(googleDeploy, 'compileFunctions')
6265
.returns(BbPromise.resolve());
6366
uploadArtifactsStub = sinon.stub(googleDeploy, 'uploadArtifacts')
@@ -74,6 +77,7 @@ describe('GoogleDeploy', () => {
7477
googleDeploy.prepareDeployment.restore();
7578
googleDeploy.createDeployment.restore();
7679
googleDeploy.generateArtifactDirectoryName.restore();
80+
googleDeploy.mergeServiceResources.restore();
7781
googleDeploy.compileFunctions.restore();
7882
googleDeploy.uploadArtifacts.restore();
7983
googleDeploy.updateDeployment.restore();
@@ -105,7 +109,8 @@ describe('GoogleDeploy', () => {
105109

106110
it('should run "deploy:deploy" promise chain', () => googleDeploy
107111
.hooks['deploy:deploy']().then(() => {
108-
expect(uploadArtifactsStub.calledOnce).toEqual(true);
112+
expect(mergeServiceResourcesStub.calledOnce).toEqual(true);
113+
expect(uploadArtifactsStub.calledAfter(mergeServiceResourcesStub)).toEqual(true);
109114
expect(updateDeploymentStub.calledAfter(uploadArtifactsStub)).toEqual(true);
110115
expect(cleanupDeploymentBucketStub.calledAfter(updateDeploymentStub)).toEqual(true);
111116
}));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/* eslint no-use-before-define: 0 */
4+
5+
const _ = require('lodash');
6+
const BbPromise = require('bluebird');
7+
8+
module.exports = {
9+
mergeServiceResources() {
10+
const resources = this.serverless.service.resources;
11+
12+
if ((typeof resources === 'undefined') || _.isEmpty(resources)) return BbPromise.resolve();
13+
14+
_.mergeWith(
15+
this.serverless.service.provider.compiledConfigurationTemplate,
16+
resources,
17+
mergeCustomizer);
18+
19+
return BbPromise.resolve();
20+
},
21+
};
22+
23+
const mergeCustomizer = (objValue, srcValue) => {
24+
if (_.isArray(objValue)) return objValue.concat(srcValue);
25+
return objValue;
26+
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use stict';
2+
3+
const GoogleProvider = require('../../provider/googleProvider');
4+
const GoogleDeploy = require('../googleDeploy');
5+
const Serverless = require('../../test/serverless');
6+
7+
describe('MergeServiceResources', () => {
8+
let serverless;
9+
let googleDeploy;
10+
11+
beforeEach(() => {
12+
serverless = new Serverless();
13+
serverless.service.service = 'my-service';
14+
serverless.service.provider = {
15+
compiledConfigurationTemplate: {},
16+
};
17+
serverless.setProvider('google', new GoogleProvider(serverless));
18+
const options = {
19+
stage: 'dev',
20+
region: 'us-central1',
21+
};
22+
googleDeploy = new GoogleDeploy(serverless, options);
23+
});
24+
25+
it('should resolve if service resources are not defined', () => googleDeploy
26+
.mergeServiceResources().then(() => {
27+
expect(serverless.service.provider
28+
.compiledConfigurationTemplate).toEqual({});
29+
}));
30+
31+
it('should resolve if service resources is empty', () => {
32+
serverless.service.resources = {};
33+
34+
return googleDeploy.mergeServiceResources().then(() => {
35+
expect(serverless.service.provider
36+
.compiledConfigurationTemplate).toEqual({});
37+
});
38+
});
39+
40+
it('should merge all the resources if provided', () => {
41+
serverless.service.provider.compiledConfigurationTemplate = {
42+
resources: [
43+
{
44+
name: 'resource1',
45+
type: 'type1',
46+
properties: {
47+
property1: 'value1',
48+
},
49+
},
50+
],
51+
};
52+
53+
serverless.service.resources = {
54+
resources: [
55+
{
56+
name: 'resource2',
57+
type: 'type2',
58+
properties: {
59+
property1: 'value1',
60+
},
61+
},
62+
],
63+
imports: [
64+
{
65+
path: 'path/to/template.jinja',
66+
name: 'my-template',
67+
},
68+
],
69+
};
70+
71+
const expectedResult = {
72+
resources: [
73+
{
74+
name: 'resource1',
75+
type: 'type1',
76+
properties: {
77+
property1: 'value1',
78+
},
79+
},
80+
{
81+
name: 'resource2',
82+
type: 'type2',
83+
properties: {
84+
property1: 'value1',
85+
},
86+
},
87+
],
88+
imports: [
89+
{
90+
path: 'path/to/template.jinja',
91+
name: 'my-template',
92+
},
93+
],
94+
};
95+
96+
return googleDeploy.mergeServiceResources().then(() => {
97+
expect(serverless.service.provider.compiledConfigurationTemplate)
98+
.toEqual(expectedResult);
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)