From 3d2879ef564da2cddb51147c66b7ef497f4ea234 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Tue, 22 Oct 2019 11:45:06 +0200 Subject: [PATCH] Introduce logical id naming function --- lib/plugins/aws/lib/naming.js | 3 +++ lib/plugins/aws/lib/naming.test.js | 8 ++++++++ .../compile/events/lib/ensureApiGatewayCloudWatchRole.js | 3 ++- .../events/lib/ensureApiGatewayCloudWatchRole.test.js | 5 ++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/plugins/aws/lib/naming.js b/lib/plugins/aws/lib/naming.js index 9fe9a4b1f..c5249a436 100644 --- a/lib/plugins/aws/lib/naming.js +++ b/lib/plugins/aws/lib/naming.js @@ -291,6 +291,9 @@ module.exports = { getApiGatewayLogGroupLogicalId() { return 'ApiGatewayLogGroup'; }, + getApiGatewayAccountCloudWatchRoleResourceLogicalId() { + return 'ApiGatewayAccountCloudWatchRole'; + }, // S3 getDeploymentBucketLogicalId() { diff --git a/lib/plugins/aws/lib/naming.test.js b/lib/plugins/aws/lib/naming.test.js index f1c769883..d3f68ced1 100644 --- a/lib/plugins/aws/lib/naming.test.js +++ b/lib/plugins/aws/lib/naming.test.js @@ -456,6 +456,14 @@ describe('#naming()', () => { }); }); + describe('#getApiGatewayAccountCloudWatchRoleResourceLogicalId()', () => { + it('should return the API Gateway account CloudWatch role logical id', () => { + expect(sdk.naming.getApiGatewayAccountCloudWatchRoleResourceLogicalId()).to.equal( + 'ApiGatewayAccountCloudWatchRole' + ); + }); + }); + describe('#getDeploymentBucketLogicalId()', () => { it('should return "ServerlessDeploymentBucket"', () => { expect(sdk.naming.getDeploymentBucketLogicalId()).to.equal('ServerlessDeploymentBucket'); diff --git a/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.js b/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.js index 06f7af55b..b00291005 100644 --- a/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.js +++ b/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.js @@ -6,6 +6,7 @@ const { addCustomResourceToService } = require('../../../../customResources'); module.exports = memoize(provider => { const cfTemplate = provider.serverless.service.provider.compiledCloudFormationTemplate; + const logicalId = provider.naming.getApiGatewayAccountCloudWatchRoleResourceLogicalId(); const customResourceLogicalId = provider.naming.getCustomResourceApiGatewayAccountCloudWatchRoleResourceLogicalId(); const customResourceFunctionLogicalId = provider.naming.getCustomResourceApiGatewayAccountCloudWatchRoleHandlerFunctionLogicalId(); @@ -15,7 +16,7 @@ module.exports = memoize(provider => { const configuredRoleArn = restApi && restApi.role; if (configuredRoleArn) { - cfTemplate.Resources.apiGatewayCloudWatchRole = { + cfTemplate.Resources[logicalId] = { Type: 'AWS::ApiGateway::Account', Properties: { CloudWatchRoleArn: configuredRoleArn, diff --git a/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.test.js b/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.test.js index aefac1fc5..d69fd8bbc 100644 --- a/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.test.js +++ b/lib/plugins/aws/package/compile/events/lib/ensureApiGatewayCloudWatchRole.test.js @@ -14,6 +14,7 @@ describe('ensureApiGatewayCloudWatchRole', () => { let resources; let addCustomResourceToServiceStub; let ensureApiGatewayCloudWatchRole; + const logicalId = 'Id'; const customResourceLogicalId = 'CustomResourceId'; beforeEach(() => { @@ -35,6 +36,7 @@ describe('ensureApiGatewayCloudWatchRole', () => { }, }, naming: { + getApiGatewayAccountCloudWatchRoleResourceLogicalId: () => logicalId, getCustomResourceApiGatewayAccountCloudWatchRoleResourceLogicalId: () => customResourceLogicalId, getCustomResourceApiGatewayAccountCloudWatchRoleHandlerFunctionLogicalId: () => 'bar', @@ -53,7 +55,7 @@ describe('ensureApiGatewayCloudWatchRole', () => { return expect(ensureApiGatewayCloudWatchRole(provider)).to.eventually.be.fulfilled.then( () => { expect(_.isObject(resources[customResourceLogicalId])).to.equal(false); - expect(_.isObject(resources.apiGatewayCloudWatchRole)).to.equal(true); + expect(_.isObject(resources[logicalId])).to.equal(true); } ); }); @@ -74,6 +76,7 @@ describe('ensureApiGatewayCloudWatchRole', () => { it('Should ensure custom resource on template', () => { return expect(ensureApiGatewayCloudWatchRole(provider)).to.eventually.be.fulfilled.then( () => { + expect(_.isObject(resources[logicalId])).to.equal(false); expect(_.isObject(resources[customResourceLogicalId])).to.equal(true); } );