From 40da6be2b2c19d44f8dcfa08114b550749352218 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 22 Sep 2016 11:40:45 +0200 Subject: [PATCH] Remove dependency on API key id in outputs section --- .../compile/events/apiGateway/lib/apiKeys.js | 15 -------------- .../events/apiGateway/tests/apiKeys.js | 14 ------------- lib/plugins/aws/info/index.js | 20 ++++++------------- lib/plugins/aws/info/tests/index.js | 18 ++++++----------- 4 files changed, 12 insertions(+), 55 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js index 3f9edbe77..31dae6232 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js @@ -38,21 +38,6 @@ module.exports = { _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, newApiKeyObject); - - // Add Id of API Key to Outputs section (Note: This is the Id, not the value!) - const newOutput = { - Description: `Id of API key "${apiKey}"`, - Value: { - Ref: `ApiGatewayApiKey${apiKeyNumber}`, - }, - }; - - const newOutputObject = { - [`ApiGatewayApiKey${apiKeyNumber}Value`]: newOutput, - }; - - _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs, - newOutputObject); }); } diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js index 868e0f8ec..94e7ce305 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/apiKeys.js @@ -82,20 +82,6 @@ describe('#compileApiKeys()', () => { }) ); - it('should add api keys cf output template', () => awsCompileApigEvents - .compileApiKeys().then(() => { - expect( - awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate - .Outputs.ApiGatewayApiKey1Value.Description - ).to.equal('Id of API key "1234567890"'); - - expect( - awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate - .Outputs.ApiGatewayApiKey1Value.Value.Ref - ).to.equal('ApiGatewayApiKey1'); - }) - ); - it('throw error if apiKey property is not an array', () => { awsCompileApigEvents.serverless.service.provider.apiKeys = 2; expect(() => awsCompileApigEvents.compileApiKeys()).to.throw(Error); diff --git a/lib/plugins/aws/info/index.js b/lib/plugins/aws/info/index.js index c1777437d..dbbf6da54 100644 --- a/lib/plugins/aws/info/index.js +++ b/lib/plugins/aws/info/index.js @@ -106,30 +106,22 @@ class AwsInfo { } getApiKeyValues(gatheredData) { - const outputs = gatheredData.outputs; const info = gatheredData.info; - // check if there are API key outputs in the stacks Outputs section - let apiKeyOutputs; - if (outputs) { - apiKeyOutputs = outputs.filter((output) => output.OutputKey.match(/^ApiGatewayApiKey/)); - } + // check if the user has set api keys + const apiKeyNames = this.serverless.service.provider.apiKeys || []; - if (apiKeyOutputs.length) { + if (apiKeyNames.length) { return this.sdk.request('APIGateway', 'getApiKeys', { includeValues: true }, this.options.stage, this.options.region - ).then((apiKeys) => { - const items = apiKeys.items; + ).then((allApiKeys) => { + const items = allApiKeys.items; if (items) { - // extract the ids from the API key object - const apiKeyIds = []; - apiKeyOutputs.forEach((apiKeyOutput) => apiKeyIds.push(apiKeyOutput.OutputValue)); - // filter out the API keys only created for this stack - const filteredItems = items.filter((item) => _.includes(apiKeyIds, item.id)); + const filteredItems = items.filter((item) => _.includes(apiKeyNames, item.name)); // iterate over all apiKeys and push the API key info and update the info object filteredItems.forEach((item) => { diff --git a/lib/plugins/aws/info/tests/index.js b/lib/plugins/aws/info/tests/index.js index 53241d725..871cc261b 100644 --- a/lib/plugins/aws/info/tests/index.js +++ b/lib/plugins/aws/info/tests/index.js @@ -255,19 +255,11 @@ describe('AwsInfo', () => { // TODO: implement a pattern for stub restoring to get rid of this awsInfo.sdk.request.restore(); + // set the API Keys for the service + awsInfo.serverless.service.provider.apiKeys = ['foo', 'bar']; + const gatheredData = { - outputs: [ - { - OutputKey: 'ApiGatewayApiKey1Value', - OutputValue: '1234', - Description: 'Id for API key "foo"', - }, - { - OutputKey: 'ApiGatewayApiKey2Value', - OutputValue: '5678', - Description: 'Id for API key "bar"', - }, - ], + outputs: [], info: { apiKeys: [], }, @@ -315,6 +307,8 @@ describe('AwsInfo', () => { }); it('should resolve with the passed-in data if no API key retrieval is necessary', () => { + awsInfo.serverless.service.provider.apiKeys = null; + const gatheredData = { outputs: [], info: {