Skip to content

Commit f61aac3

Browse files
authored
Merge pull request #457 from medikoo/0915-apigateway-props
Regognize API Gateway properties at `provider.apiGateway`
2 parents 7057afe + fca45f6 commit f61aac3

File tree

8 files changed

+61
-46
lines changed

8 files changed

+61
-46
lines changed

lib/deploy/events/apiGateway/apiKeys.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const BbPromise = require('bluebird');
55

66
module.exports = {
77
compileApiKeys() {
8-
if (this.serverless.service.provider.apiKeys) {
9-
if (!Array.isArray(this.serverless.service.provider.apiKeys)) {
8+
const apiKeys = _.get(this.serverless.service.provider.apiGateway, 'apiKeys')
9+
|| this.serverless.service.provider.apiKeys;
10+
if (apiKeys) {
11+
if (!Array.isArray(apiKeys)) {
1012
throw new this.serverless.classes.Error('apiKeys property must be an array');
1113
}
1214

13-
_.forEach(this.serverless.service.provider.apiKeys, (apiKey, i) => {
15+
_.forEach(apiKeys, (apiKey, i) => {
1416
const apiKeyNumber = i + 1;
1517

1618
if (typeof apiKey !== 'string') {

lib/deploy/events/apiGateway/apiKeys.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe('#methods()', () => {
1616
region: 'us-east-1',
1717
};
1818
serverless.setProvider('aws', new AwsProvider(serverless));
19-
serverless.service.provider.apiKeys = ['1234567890'];
19+
if (!serverless.service.provider.apiGateway) serverless.service.provider.apiGateway = {};
20+
serverless.service.provider.apiGateway.apiKeys = ['1234567890'];
2021
serverless.service.provider.compiledCloudFormationTemplate = {
2122
Resources: {},
2223
};
@@ -79,12 +80,12 @@ describe('#methods()', () => {
7980
}));
8081

8182
it('throw error if apiKey property is not an array', () => {
82-
serverlessStepFunctions.serverless.service.provider.apiKeys = 2;
83+
serverlessStepFunctions.serverless.service.provider.apiGateway.apiKeys = 2;
8384
expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error);
8485
});
8586

8687
it('throw error if an apiKey is not a string', () => {
87-
serverlessStepFunctions.serverless.service.provider.apiKeys = [2];
88+
serverlessStepFunctions.serverless.service.provider.apiGateway.apiKeys = [2];
8889
expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error);
8990
});
9091
});

lib/deploy/events/apiGateway/restApi.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ module.exports = {
4343
},
4444
});
4545

46-
if (!_.isEmpty(this.serverless.service.provider.resourcePolicy)) {
46+
const resourcePolicy = _.get(this.serverless.service.provider.apiGateway, 'resourcePolicy')
47+
|| this.serverless.service.provider.resourcePolicy;
48+
49+
if (!_.isEmpty(resourcePolicy)) {
4750
const policy = {
4851
Version: '2012-10-17',
49-
Statement: this.serverless.service.provider.resourcePolicy,
52+
Statement: resourcePolicy,
5053
};
5154
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate
5255
.Resources[this.apiGatewayRestApiLogicalId].Properties, {

lib/deploy/events/apiGateway/restApi.test.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,21 @@ describe('#compileRestApi()', () => {
8989
}));
9090

9191
it('should create a REST API resource with resource policy', () => {
92-
serverlessStepFunctions.serverless.service.provider.resourcePolicy = [
93-
{
94-
Effect: 'Allow',
95-
Principal: '*',
96-
Action: 'execute-api:Invoke',
97-
Resource: ['execute-api:/*/*/*'],
98-
Condition: {
99-
IpAddress: {
100-
'aws:SourceIp': ['123.123.123.123'],
92+
serverlessStepFunctions.serverless.service.provider.apiGateway = {
93+
resourcePolicy: [
94+
{
95+
Effect: 'Allow',
96+
Principal: '*',
97+
Action: 'execute-api:Invoke',
98+
Resource: ['execute-api:/*/*/*'],
99+
Condition: {
100+
IpAddress: {
101+
'aws:SourceIp': ['123.123.123.123'],
102+
},
101103
},
102104
},
103-
},
104-
];
105+
],
106+
};
105107
return serverlessStepFunctions.compileRestApi().then(() => {
106108
expect(serverlessStepFunctions.serverless.service.provider
107109
.compiledCloudFormationTemplate.Resources).to.deep.equal(

lib/deploy/events/apiGateway/usagePlan.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const BbPromise = require('bluebird');
55

66
module.exports = {
77
compileUsagePlan() {
8-
if (this.serverless.service.provider.usagePlan || this.serverless.service.provider.apiKeys) {
8+
const usagePlan = _.get(this.serverless.service.provider.apiGateway, 'usagePlan')
9+
|| this.serverless.service.provider.usagePlan;
10+
if (usagePlan
11+
|| _.get(this.serverless.service.provider.apiGateway, 'apiKeys')
12+
|| this.serverless.service.provider.apiKeys) {
913
this.apiGatewayUsagePlanLogicalId = this.provider.naming.getUsagePlanLogicalId();
1014
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
1115
[this.apiGatewayUsagePlanLogicalId]: {
@@ -27,28 +31,26 @@ module.exports = {
2731
},
2832
},
2933
});
30-
if (_.has(this.serverless.service.provider, 'usagePlan.quota')
31-
&& this.serverless.service.provider.usagePlan.quota !== null) {
34+
if (_.has(usagePlan, 'quota') && usagePlan.quota !== null) {
3235
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
3336
[this.apiGatewayUsagePlanLogicalId]: {
3437
Properties: {
3538
Quota: _.merge(
36-
{ Limit: this.serverless.service.provider.usagePlan.quota.limit },
37-
{ Offset: this.serverless.service.provider.usagePlan.quota.offset },
38-
{ Period: this.serverless.service.provider.usagePlan.quota.period },
39+
{ Limit: usagePlan.quota.limit },
40+
{ Offset: usagePlan.quota.offset },
41+
{ Period: usagePlan.quota.period },
3942
),
4043
},
4144
},
4245
});
4346
}
44-
if (_.has(this.serverless.service.provider, 'usagePlan.throttle')
45-
&& this.serverless.service.provider.usagePlan.throttle !== null) {
47+
if (_.has(usagePlan, 'throttle') && usagePlan.throttle !== null) {
4648
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
4749
[this.apiGatewayUsagePlanLogicalId]: {
4850
Properties: {
4951
Throttle: _.merge(
50-
{ BurstLimit: this.serverless.service.provider.usagePlan.throttle.burstLimit },
51-
{ RateLimit: this.serverless.service.provider.usagePlan.throttle.rateLimit },
52+
{ BurstLimit: usagePlan.throttle.burstLimit },
53+
{ RateLimit: usagePlan.throttle.rateLimit },
5254
),
5355
},
5456
},

lib/deploy/events/apiGateway/usagePlan.test.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ describe('#compileUsagePlan()', () => {
1717
};
1818
serverless.service.service = 'first-service';
1919
serverless.setProvider('aws', new AwsProvider(serverless));
20-
serverless.service.provider.apiKeys = ['1234567890'];
20+
if (!serverless.service.provider.apiGateway) serverless.service.provider.apiGateway = {};
21+
serverless.service.provider.apiGateway.apiKeys = ['1234567890'];
2122
serverless.service.provider.compiledCloudFormationTemplate = {
2223
Resources: {},
2324
};
@@ -36,7 +37,7 @@ describe('#compileUsagePlan()', () => {
3637
});
3738

3839
it('should compile default usage plan resource', () => {
39-
serverless.service.provider.apiKeys = ['1234567890'];
40+
serverless.service.provider.apiGateway.apiKeys = ['1234567890'];
4041
return serverlessStepFunctions.compileUsagePlan().then(() => {
4142
expect(
4243
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
@@ -83,15 +84,17 @@ describe('#compileUsagePlan()', () => {
8384
});
8485

8586
it('should compile custom usage plan resource', () => {
86-
serverless.service.provider.usagePlan = {
87-
quota: {
88-
limit: 500,
89-
offset: 10,
90-
period: 'MONTH',
91-
},
92-
throttle: {
93-
burstLimit: 200,
94-
rateLimit: 100,
87+
serverless.service.provider.apiGateway = {
88+
usagePlan: {
89+
quota: {
90+
limit: 500,
91+
offset: 10,
92+
period: 'MONTH',
93+
},
94+
throttle: {
95+
burstLimit: 200,
96+
rateLimit: 100,
97+
},
9598
},
9699
};
97100

lib/deploy/events/apiGateway/usagePlanKeys.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const BbPromise = require('bluebird');
55

66
module.exports = {
77
compileUsagePlanKeys() {
8-
if (this.serverless.service.provider.apiKeys) {
9-
if (!Array.isArray(this.serverless.service.provider.apiKeys)) {
8+
const apiKeys = _.get(this.serverless.service.provider.apiGateway, 'apiKeys')
9+
|| this.serverless.service.provider.apiKeys;
10+
if (apiKeys) {
11+
if (!Array.isArray(apiKeys)) {
1012
throw new this.serverless.classes.Error('apiKeys property must be an array');
1113
}
1214

13-
_.forEach(this.serverless.service.provider.apiKeys, (apiKey, i) => {
15+
_.forEach(apiKeys, (apiKey, i) => {
1416
const usagePlanKeyNumber = i + 1;
1517

1618
if (typeof apiKey !== 'string') {

lib/deploy/events/apiGateway/usagePlanKeys.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('#compileUsagePlanKeys()', () => {
1919
serverless.service.service = 'first-service';
2020
serverless.service.provider = {
2121
name: 'aws',
22-
apiKeys: ['1234567890'],
22+
apiGateway: { apiKeys: ['1234567890'] },
2323
};
2424
serverless.service.provider.compiledCloudFormationTemplate = {
2525
Resources: {},
@@ -66,12 +66,12 @@ describe('#compileUsagePlanKeys()', () => {
6666
}));
6767

6868
it('throw error if apiKey property is not an array', () => {
69-
serverlessStepFunctions.serverless.service.provider.apiKeys = 2;
69+
serverlessStepFunctions.serverless.service.provider.apiGateway.apiKeys = 2;
7070
expect(() => serverlessStepFunctions.compileUsagePlanKeys()).to.throw(Error);
7171
});
7272

7373
it('throw error if an apiKey is not a string', () => {
74-
serverlessStepFunctions.serverless.service.provider.apiKeys = [2];
74+
serverlessStepFunctions.serverless.service.provider.apiGateway.apiKeys = [2];
7575
expect(() => serverlessStepFunctions.compileUsagePlanKeys()).to.throw(Error);
7676
});
7777
});

0 commit comments

Comments
 (0)