From cd063fdbe860a6d8143b5efdd499e988f61d4ceb Mon Sep 17 00:00:00 2001 From: Frederic Lemay Date: Fri, 8 Sep 2017 05:52:03 +1000 Subject: [PATCH 1/4] add function log group id as dependency --- index.js | 9 +++++++-- test/index-test.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d2a879c..43d7b4d 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,7 @@ class LogForwardingPlugin { const filterPattern = service.custom.logForwarding.filterPattern || ''; // Get options and parameters to make resources object const serviceName = service.service; + const awsProvider = this.serverless.getProvider('aws'); const arn = service.custom.logForwarding.destinationARN; const stage = options.stage && options.stage.length > 0 ? options.stage @@ -74,8 +75,9 @@ class LogForwardingPlugin { }; for (let i = 0; i < functions.length; i += 1) { /* merge new SubscriptionFilter with current resources object */ + const functionLogGroupId = awsProvider.naming.getLogGroupLogicalId(functions[i]); const subscriptionFilter = LogForwardingPlugin.makeSubscriptionFilter(serviceName, - stage, arn, functions[i], filterPattern); + stage, arn, functions[i], filterPattern, functionLogGroupId); _.extend(resourceObj, subscriptionFilter); } return resourceObj; @@ -89,9 +91,11 @@ class LogForwardingPlugin { * @param {String} arn arn of the lambda to forward to * @param {String} functionName name of function to make SubscriptionFilter for * @param {String} filterPattern filter pattern for the Subscription + * @param {String} functionLogGroupId name of the function Log Group to add as a dependency * @return {Object} SubscriptionFilter */ - static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern) { + static makeSubscriptionFilter(serviceName, stage, arn, functionName, filterPattern, + functionLogGroupId) { const logGroupName = `/aws/lambda/${serviceName}-${stage}-${functionName}`; const filter = {}; filter[`SubscriptionFilter${functionName}`] = { @@ -103,6 +107,7 @@ class LogForwardingPlugin { }, DependsOn: [ 'LogForwardingLambdaPermission', + functionLogGroupId, ], }; return filter; diff --git a/test/index-test.js b/test/index-test.js index 4def040..1cd2505 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -18,6 +18,18 @@ const correctConfigWithStageFilter = { filterPattern: 'Test Pattern', stages: ['production'], }; + +const awsProvider = (provider) => { + if (provider !== 'aws') { + throw new Error('provider must be aws'); + } + return { + naming: { + getLogGroupLogicalId: functionName => `${functionName.charAt(0).toUpperCase()}${functionName.slice(1)}LogGroup`, + }, + }; +}; + const constructPluginResources = (logForwarding) => { const serverless = { service: { @@ -46,6 +58,7 @@ const constructPluginResources = (logForwarding) => { }, service: 'test-service', }, + getProvider: awsProvider, cli: { log() { }, @@ -74,6 +87,7 @@ const constructPluginNoResources = (logForwarding) => { }, service: 'test-service', }, + getProvider: awsProvider, cli: { log() { }, @@ -110,6 +124,7 @@ const constructPluginResourcesWithParam = (logForwarding) => { }, service: 'test-service', }, + getProvider: awsProvider, cli: { log() { }, @@ -143,6 +158,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionOneLogGroup', ], }, SubscriptionFiltertestFunctionTwo: { @@ -154,6 +170,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionTwoLogGroup', ], }, }, @@ -185,6 +202,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionOneLogGroup', ], }, SubscriptionFiltertestFunctionTwo: { @@ -196,6 +214,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionTwoLogGroup', ], }, }, @@ -224,6 +243,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionOneLogGroup', ], }, SubscriptionFiltertestFunctionTwo: { @@ -235,6 +255,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionTwoLogGroup', ], }, }, @@ -266,6 +287,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionOneLogGroup', ], }, SubscriptionFiltertestFunctionTwo: { @@ -277,6 +299,7 @@ describe('Given a serverless config', () => { }, DependsOn: [ 'LogForwardingLambdaPermission', + 'TestFunctionTwoLogGroup', ], }, }, From 27244cf8667267edfeb897c5c72027c04fb9fa09 Mon Sep 17 00:00:00 2001 From: Frederic Lemay Date: Fri, 8 Sep 2017 06:06:50 +1000 Subject: [PATCH 2/4] rename to awsProviderMock --- test/index-test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/index-test.js b/test/index-test.js index 1cd2505..841b6c7 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -19,7 +19,7 @@ const correctConfigWithStageFilter = { stages: ['production'], }; -const awsProvider = (provider) => { +const awsProviderMock = (provider) => { if (provider !== 'aws') { throw new Error('provider must be aws'); } @@ -58,7 +58,7 @@ const constructPluginResources = (logForwarding) => { }, service: 'test-service', }, - getProvider: awsProvider, + getProvider: awsProviderMock, cli: { log() { }, @@ -87,7 +87,7 @@ const constructPluginNoResources = (logForwarding) => { }, service: 'test-service', }, - getProvider: awsProvider, + getProvider: awsProviderMock, cli: { log() { }, @@ -124,7 +124,7 @@ const constructPluginResourcesWithParam = (logForwarding) => { }, service: 'test-service', }, - getProvider: awsProvider, + getProvider: awsProviderMock, cli: { log() { }, From d184956865d44329b62004b5f6d00b45f4ceb1cb Mon Sep 17 00:00:00 2001 From: Frederic Lemay Date: Fri, 8 Sep 2017 07:04:29 +1000 Subject: [PATCH 3/4] use serverless aws provider the same way @domaslasauskas uses for his branch feature/use-naming-module --- package.json | 3 +- test/index-test.js | 158 ++++++++++++++++++++------------------------- 2 files changed, 71 insertions(+), 90 deletions(-) diff --git a/package.json b/package.json index 4e2fa01..09ab402 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "eslint-plugin-jsx-a11y": "^3.0.2", "eslint-plugin-react": "^6.10.3", "istanbul": "^0.4.5", - "mocha": "^2.2.5" + "mocha": "^2.2.5", + "serverless": "^1.20.2" }, "scripts": { "test": "node ./node_modules/istanbul/lib/cli.js cover _mocha -- -R spec && node ./node_modules/istanbul/lib/cli.js check-coverage --line 70 coverage/coverage.json", diff --git a/test/index-test.js b/test/index-test.js index 841b6c7..b8811ad 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -19,118 +19,98 @@ const correctConfigWithStageFilter = { stages: ['production'], }; -const awsProviderMock = (provider) => { - if (provider !== 'aws') { - throw new Error('provider must be aws'); - } - return { - naming: { - getLogGroupLogicalId: functionName => `${functionName.charAt(0).toUpperCase()}${functionName.slice(1)}LogGroup`, +const Serverless = require('serverless'); +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider'); + +const createServerless = (options, service) => { + const serverless = new Serverless(options); + serverless.cli = { + log() { }, }; + new AwsProvider(serverless, options); // eslint-disable-line no-new + serverless.service.update(service); + serverless.service.setFunctionNames(options); + return serverless; }; const constructPluginResources = (logForwarding) => { - const serverless = { - service: { - provider: { - region: 'us-moon-1', - stage: 'test-stage', - }, - custom: { - logForwarding, - }, - resources: { - Resources: { - TestExistingFilter: { - Type: 'AWS:Test:Filter', - }, - }, - }, - functions: { - testFunctionOne: { - name: 'functionOne', - filterPattern: 'Pattern', - }, - testFunctionTwo: { - name: 'functionTwo', + const options = {}; + const serverless = createServerless(options, { + provider: { + region: 'us-moon-1', + stage: 'test-stage', + }, + custom: { + logForwarding, + }, + resources: { + Resources: { + TestExistingFilter: { + Type: 'AWS:Test:Filter', }, }, - service: 'test-service', }, - getProvider: awsProviderMock, - cli: { - log() { + functions: { + testFunctionOne: { + filterPattern: 'Pattern', + }, + testFunctionTwo: { }, }, - }; - return new LogForwardingPlugin(serverless, {}); + service: 'test-service', + }); + return new LogForwardingPlugin(serverless, options); }; const constructPluginNoResources = (logForwarding) => { - const serverless = { - service: { - provider: { - region: 'us-moon-1', - stage: 'test-stage', - }, - custom: { - logForwarding, - }, - resources: undefined, - functions: { - testFunctionOne: { - name: 'functionOne', - }, - testFunctionTwo: { - name: 'functionTwo', - }, - }, - service: 'test-service', + const options = {}; + const serverless = createServerless(options, { + provider: { + region: 'us-moon-1', + stage: 'test-stage', + }, + custom: { + logForwarding, }, - getProvider: awsProviderMock, - cli: { - log() { + functions: { + testFunctionOne: { + }, + testFunctionTwo: { }, }, - }; - return new LogForwardingPlugin(serverless, {}); + service: 'test-service', + }); + serverless.service.resources = undefined; + return new LogForwardingPlugin(serverless, options); }; const constructPluginResourcesWithParam = (logForwarding) => { - const serverless = { - service: { - provider: { - region: 'us-moon-1', - stage: 'test-stage', - }, - custom: { - logForwarding, - }, - resources: { - Resources: { - TestExistingFilter: { - Type: 'AWS:Test:Filter', - }, - }, - }, - functions: { - testFunctionOne: { - name: 'functionOne', - filterPattern: 'Pattern', - }, - testFunctionTwo: { - name: 'functionTwo', + const options = { stage: 'dev' }; + const serverless = createServerless(options, { + provider: { + region: 'us-moon-1', + stage: 'test-stage', + }, + custom: { + logForwarding, + }, + resources: { + Resources: { + TestExistingFilter: { + Type: 'AWS:Test:Filter', }, }, - service: 'test-service', }, - getProvider: awsProviderMock, - cli: { - log() { + functions: { + testFunctionOne: { + filterPattern: 'Pattern', + }, + testFunctionTwo: { }, }, - }; - return new LogForwardingPlugin(serverless, { stage: 'dev' }); + service: 'test-service', + }); + return new LogForwardingPlugin(serverless, options); }; describe('Given a serverless config', () => { From 880a10fb83d14c4621ca2e42317c1688aca8ba13 Mon Sep 17 00:00:00 2001 From: Frederic Lemay Date: Fri, 8 Sep 2017 07:05:21 +1000 Subject: [PATCH 4/4] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 09ab402..2a64291 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-log-forwarding", - "version": "1.1.3", + "version": "1.1.4", "description": "a serverless plugin to forward logs to given lambda function", "main": "index.js", "directories": {