Skip to content

Commit 01b38b2

Browse files
committed
[WIP] add api keys functionality
1 parent f8105e1 commit 01b38b2

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const BbPromise = require('bluebird');
5+
6+
module.exports = {
7+
compileApiKeys() {
8+
if (this.serverless.service.provider.apiKeys) {
9+
if (!Array.isArray(this.serverless.service.provider.apiKeys)) {
10+
throw new this.serverless.classes.Error('apiKeys property must be an array');
11+
}
12+
13+
_.forEach(this.serverless.service.provider.apiKeys, (apiKey, i) => {
14+
const apiKeyNumber = i + 1;
15+
16+
if (typeof apiKey !== 'string') {
17+
throw new this.serverless.classes.Error('API Keys must be strings');
18+
}
19+
20+
const apiKeyLogicalId = this.provider.naming
21+
.getApiKeyLogicalId(apiKeyNumber);
22+
23+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
24+
[apiKeyLogicalId]: {
25+
Type: 'AWS::ApiGateway::ApiKey',
26+
Properties: {
27+
Enabled: true,
28+
Name: apiKey,
29+
StageKeys: [{
30+
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },
31+
StageName: this.provider.getStage(),
32+
}],
33+
},
34+
DependsOn: this.apiGatewayDeploymentLogicalId,
35+
},
36+
});
37+
});
38+
}
39+
return BbPromise.resolve();
40+
},
41+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Serverless = require('serverless/lib/Serverless');
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
6+
const ServerlessStepFunctions = require('./../../../index');
7+
8+
describe('#methods()', () => {
9+
let serverless;
10+
let serverlessStepFunctions;
11+
12+
beforeEach(() => {
13+
serverless = new Serverless();
14+
const options = {
15+
stage: 'dev',
16+
region: 'us-east-1',
17+
};
18+
serverless.setProvider('aws', new AwsProvider(serverless));
19+
serverless.service.provider.apiKeys = ['1234567890'];
20+
serverless.service.provider.compiledCloudFormationTemplate = {
21+
Resources: {},
22+
};
23+
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
24+
serverlessStepFunctions.serverless.service.stepFunctions = {
25+
stateMachines: {
26+
first: {},
27+
},
28+
};
29+
serverlessStepFunctions.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
30+
serverlessStepFunctions.apiGatewayDeploymentLogicalId = 'ApiGatewayDeploymentTest';
31+
});
32+
33+
it('should compile api key resource', () =>
34+
serverlessStepFunctions.compileApiKeys().then(() => {
35+
expect(
36+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
37+
.Resources[
38+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
39+
].Type
40+
).to.equal('AWS::ApiGateway::ApiKey');
41+
42+
expect(
43+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
44+
.Resources[
45+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
46+
].Properties.Enabled
47+
).to.equal(true);
48+
49+
expect(
50+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
51+
.Resources[
52+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
53+
].Properties.Name
54+
).to.equal('1234567890');
55+
56+
expect(
57+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
58+
.Resources[
59+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
60+
].Properties.StageKeys[0].RestApiId.Ref
61+
).to.equal('ApiGatewayRestApi');
62+
63+
expect(
64+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
65+
.Resources[
66+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
67+
].Properties.StageKeys[0].StageName
68+
).to.equal('dev');
69+
70+
expect(
71+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
72+
.Resources[
73+
serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1)
74+
].DependsOn
75+
).to.equal('ApiGatewayDeploymentTest');
76+
})
77+
);
78+
79+
it('throw error if apiKey property is not an array', () => {
80+
serverlessStepFunctions.serverless.service.provider.apiKeys = 2;
81+
expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error);
82+
});
83+
84+
it('throw error if an apiKey is not a string', () => {
85+
serverlessStepFunctions.serverless.service.provider.apiKeys = [2];
86+
expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error);
87+
});
88+
});

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const httpValidate = require('./deploy/events/apiGateway/validate');
88
const httpResources = require('./deploy/events/apiGateway/resources');
99
const httpMethods = require('./deploy/events/apiGateway/methods');
1010
const httpCors = require('./deploy/events/apiGateway/cors');
11+
const httpApiKeys = require('./deploy/events/apiGateway/apiKeys');
1112
const httpIamRole = require('./deploy/events/apiGateway/iamRole');
1213
const httpDeployment = require('./deploy/events/apiGateway/deployment');
1314
const httpRestApi = require('./deploy/events/apiGateway/restApi');
@@ -38,6 +39,7 @@ class ServerlessStepFunctions {
3839
httpResources,
3940
httpMethods,
4041
httpCors,
42+
httpApiKeys,
4143
httpIamRole,
4244
httpDeployment,
4345
invoke,
@@ -106,6 +108,7 @@ class ServerlessStepFunctions {
106108
.then(this.compileResources)
107109
.then(this.compileMethods)
108110
.then(this.compileCors)
111+
.then(this.compileApiKeys)
109112
.then(this.compileHttpIamRole)
110113
.then(this.compileDeployment);
111114
}

0 commit comments

Comments
 (0)