Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved stage to its own resource #3232

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers/aws/guide/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ We're also using the term `normalizedName` or similar terms in this guide. This
|ApiGateway::Method | ApiGatewayResource{normalizedPath}{normalizedMethod} | ApiGatewayResourceUsersGet |
|ApiGateway::Authorizer | {normalizedFunctionName}ApiGatewayAuthorizer | HelloApiGatewayAuthorizer |
|ApiGateway::Deployment | ApiGatewayDeployment{randomNumber} | ApiGatewayDeployment12356789 |
|ApiGateway::Stage | ApiGatewayStage | ApiGatewayStage |
|ApiGateway::ApiKey | ApiGatewayApiKey{SequentialID} | ApiGatewayApiKey1 |
|SNS::Topic | SNSTopic{normalizedTopicName} | SNSTopicSometopic |
|SNS::Subscription | {normalizedFunctionName}SnsSubscription{normalizedTopicName} | HelloSnsSubscriptionSomeTopic |
Expand Down
3 changes: 3 additions & 0 deletions lib/plugins/aws/deploy/compile/events/apiGateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const compileCors = require('./lib/cors');
const compileMethods = require('./lib/method/index');
const compileAuthorizers = require('./lib/authorizers');
const compileDeployment = require('./lib/deployment');
const compileStage = require('./lib/stage');
const compilePermissions = require('./lib/permissions');
const getMethodAuthorization = require('./lib/method/authorization');
const getMethodIntegration = require('./lib/method/integration');
Expand All @@ -31,6 +32,7 @@ class AwsCompileApigEvents {
compileMethods,
compileAuthorizers,
compileDeployment,
compileStage,
compilePermissions,
getMethodAuthorization,
getMethodIntegration,
Expand All @@ -52,6 +54,7 @@ class AwsCompileApigEvents {
.then(this.compileMethods)
.then(this.compileAuthorizers)
.then(this.compileDeployment)
.then(this.compileStage)
.then(this.compileApiKeys)
.then(this.compilePermissions);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe('AwsCompileApigEvents', () => {
.stub(awsCompileApigEvents, 'compileMethods').returns(BbPromise.resolve());
const compileDeploymentStub = sinon
.stub(awsCompileApigEvents, 'compileDeployment').returns(BbPromise.resolve());
const compileStageStub = sinon
.stub(awsCompileApigEvents, 'compileStage').returns(BbPromise.resolve());
const compilePermissionsStub = sinon
.stub(awsCompileApigEvents, 'compilePermissions').returns(BbPromise.resolve());

Expand All @@ -68,13 +70,15 @@ describe('AwsCompileApigEvents', () => {
expect(compileResourcesStub.calledAfter(compileRestApiStub)).to.be.equal(true);
expect(compileMethodsStub.calledAfter(compileResourcesStub)).to.be.equal(true);
expect(compileDeploymentStub.calledAfter(compileMethodsStub)).to.be.equal(true);
expect(compilePermissionsStub.calledAfter(compileDeploymentStub)).to.be.equal(true);
expect(compileStageStub.calledAfter(compileDeploymentStub)).to.be.equal(true);
expect(compilePermissionsStub.calledAfter(compileStageStub)).to.be.equal(true);

awsCompileApigEvents.validate.restore();
awsCompileApigEvents.compileRestApi.restore();
awsCompileApigEvents.compileResources.restore();
awsCompileApigEvents.compileMethods.restore();
awsCompileApigEvents.compileDeployment.restore();
awsCompileApigEvents.compileStage.restore();
awsCompileApigEvents.compilePermissions.restore();
});
});
Expand Down
16 changes: 0 additions & 16 deletions lib/plugins/aws/deploy/compile/events/apiGateway/lib/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,11 @@ module.exports = {
Type: 'AWS::ApiGateway::Deployment',
Properties: {
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },
StageName: this.options.stage,
},
DependsOn: this.apiGatewayMethodLogicalIds,
},
});

// create CLF Output for endpoint
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs, {
ServiceEndpoint: {
Description: 'URL of the service endpoint',
Value: {
'Fn::Join': ['',
[
'https://',
{ Ref: this.apiGatewayRestApiLogicalId },
`.execute-api.${this.options.region}.amazonaws.com/${this.options.stage}`,
],
],
},
},
});
return BbPromise.resolve();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('#compileDeployment()', () => {
};
const options = {
stage: 'dev',
region: 'us-east-1',
};
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
Expand All @@ -39,28 +38,6 @@ describe('#compileDeployment()', () => {
DependsOn: ['method-dependency1', 'method-dependency2'],
Properties: {
RestApiId: { Ref: awsCompileApigEvents.apiGatewayRestApiLogicalId },
StageName: 'dev',
},
});
})
);

it('should add service endpoint output', () =>
awsCompileApigEvents.compileDeployment().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
.Outputs.ServiceEndpoint
).to.deep.equal({
Description: 'URL of the service endpoint',
Value: {
'Fn::Join': [
'',
[
'https://',
{ Ref: awsCompileApigEvents.apiGatewayRestApiLogicalId },
'.execute-api.us-east-1.amazonaws.com/dev',
],
],
},
});
})
Expand Down
38 changes: 38 additions & 0 deletions lib/plugins/aws/deploy/compile/events/apiGateway/lib/stage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compileStage() {
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
ApiGatewayStage: {
Type: 'AWS::ApiGateway::Stage',
Properties: {
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },
DeploymentId: { Ref: this.apiGatewayDeploymentLogicalId },
StageName: this.options.stage,
},
DependsOn: [this.apiGatewayDeploymentLogicalId],
},
});

// create CLF Output for endpoint
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs, {
ServiceEndpoint: {
Description: 'URL of the service endpoint',
Value: {
'Fn::Join': ['',
[
'https://',
{ Ref: this.apiGatewayRestApiLogicalId },
`.execute-api.${this.options.region}.amazonaws.com/${this.options.stage}`,
],
],
},
},
});

return BbPromise.resolve();
},
};
65 changes: 65 additions & 0 deletions lib/plugins/aws/deploy/compile/events/apiGateway/lib/stage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const expect = require('chai').expect;
const AwsCompileApigEvents = require('../index');
const Serverless = require('../../../../../../../Serverless');
const AwsProvider = require('../../../../../provider/awsProvider');

describe('#compileStage()', () => {
let serverless;
let awsCompileApigEvents;

beforeEach(() => {
serverless = new Serverless();
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.provider.compiledCloudFormationTemplate = {
Resources: {},
Outputs: {},
};
const options = {
stage: 'dev',
region: 'us-east-1',
};
awsCompileApigEvents = new AwsCompileApigEvents(serverless, options);
awsCompileApigEvents.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi';
awsCompileApigEvents.apiGatewayDeploymentLogicalId = 'ApiGatewayDeployment';
});

it('should create a stage resource', () => awsCompileApigEvents
.compileStage().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources.ApiGatewayStage
).to.deep.equal({
Type: 'AWS::ApiGateway::Stage',
DependsOn: [awsCompileApigEvents.apiGatewayDeploymentLogicalId],
Properties: {
RestApiId: { Ref: awsCompileApigEvents.apiGatewayRestApiLogicalId },
DeploymentId: { Ref: awsCompileApigEvents.apiGatewayDeploymentLogicalId },
StageName: 'dev',
},
});
})
);

it('should add service endpoint output', () =>
awsCompileApigEvents.compileStage().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
.Outputs.ServiceEndpoint
).to.deep.equal({
Description: 'URL of the service endpoint',
Value: {
'Fn::Join': [
'',
[
'https://',
{ Ref: awsCompileApigEvents.apiGatewayRestApiLogicalId },
'.execute-api.us-east-1.amazonaws.com/dev',
],
],
},
});
})
);
});