From 21f4f7fe2f3a22a67fb464ad983c3b7c528c441e Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 8 Aug 2019 12:56:41 +0200 Subject: [PATCH] Fix support for EventBridge partner event sources --- .../resources/eventBridge/lib/utils.js | 2 +- .../compile/events/eventBridge/index.js | 2 +- .../compile/events/eventBridge/index.test.js | 130 +++++++++++++++++- 3 files changed, 131 insertions(+), 3 deletions(-) diff --git a/lib/plugins/aws/customResources/resources/eventBridge/lib/utils.js b/lib/plugins/aws/customResources/resources/eventBridge/lib/utils.js index 862612cb1..faa54fe4d 100644 --- a/lib/plugins/aws/customResources/resources/eventBridge/lib/utils.js +++ b/lib/plugins/aws/customResources/resources/eventBridge/lib/utils.js @@ -2,7 +2,7 @@ function getEventBusName(eventBus) { if (eventBus && eventBus.startsWith('arn')) { - return eventBus.split('/').pop(); + return eventBus.slice(eventBus.indexOf('/') + 1); } return eventBus; } diff --git a/lib/plugins/aws/package/compile/events/eventBridge/index.js b/lib/plugins/aws/package/compile/events/eventBridge/index.js index 57e09e672..923367547 100644 --- a/lib/plugins/aws/package/compile/events/eventBridge/index.js +++ b/lib/plugins/aws/package/compile/events/eventBridge/index.js @@ -119,7 +119,7 @@ class AwsCompileEventBridgeEvents { if (EventBus) { let eventBusName = EventBus; if (EventBus.startsWith('arn')) { - eventBusName = EventBus.split('/').pop(); + eventBusName = EventBus.slice(EventBus.indexOf('/') + 1); } eventBusResource = { 'Fn::Join': [ diff --git a/lib/plugins/aws/package/compile/events/eventBridge/index.test.js b/lib/plugins/aws/package/compile/events/eventBridge/index.test.js index 48bf115c3..b17c12fe2 100644 --- a/lib/plugins/aws/package/compile/events/eventBridge/index.test.js +++ b/lib/plugins/aws/package/compile/events/eventBridge/index.test.js @@ -346,7 +346,7 @@ describe('AwsCompileEventBridgeEvents', () => { ); }); - it('should create the necessary resources when using an event bus arn', () => { + it('should create the necessary resources when using an own event bus arn', () => { awsCompileEventBridgeEvents.serverless.service.functions = { first: { name: 'first', @@ -485,6 +485,134 @@ describe('AwsCompileEventBridgeEvents', () => { ); }); + it('should create the necessary resources when using a partner event bus arn', () => { + awsCompileEventBridgeEvents.serverless.service.functions = { + first: { + name: 'first', + events: [ + { + eventBridge: { + eventBus: 'arn:aws:events:us-east-1:12345:event-bus/aws.partner/partner.com/12345', + pattern: { + source: ['aws.partner/partner.com/12345'], + detail: { + event: ['My Event'], + type: ['track'], + }, + }, + }, + }, + ], + }, + }; + + return expect(awsCompileEventBridgeEvents.compileEventBridgeEvents()).to.be.fulfilled.then( + () => { + const { + Resources, + } = awsCompileEventBridgeEvents.serverless.service.provider.compiledCloudFormationTemplate; + + expect(addCustomResourceToServiceStub).to.have.been.calledOnce; + expect(addCustomResourceToServiceStub.args[0][1]).to.equal('eventBridge'); + expect(addCustomResourceToServiceStub.args[0][2]).to.deep.equal([ + { + Action: ['events:CreateEventBus', 'events:DeleteEventBus'], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + ':', + [ + 'arn:aws:events', + { + Ref: 'AWS::Region', + }, + { + Ref: 'AWS::AccountId', + }, + 'event-bus/aws.partner/partner.com/12345', + ], + ], + }, + }, + { + Action: [ + 'events:PutRule', + 'events:RemoveTargets', + 'events:PutTargets', + 'events:DeleteRule', + ], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + ':', + [ + 'arn:aws:events', + { + Ref: 'AWS::Region', + }, + { + Ref: 'AWS::AccountId', + }, + 'rule/aws.partner/partner.com/12345/first-rule-1', + ], + ], + }, + }, + { + Action: ['lambda:AddPermission', 'lambda:RemovePermission'], + Effect: 'Allow', + Resource: { + 'Fn::Join': [ + ':', + [ + 'arn:aws:lambda', + { + Ref: 'AWS::Region', + }, + { + Ref: 'AWS::AccountId', + }, + 'function', + 'first', + ], + ], + }, + }, + ]); + expect(Resources.FirstCustomEventBridge1).to.deep.equal({ + Type: 'Custom::EventBridge', + Version: 1, + DependsOn: [ + 'FirstLambdaFunction', + 'CustomDashresourceDasheventDashbridgeLambdaFunction', + ], + Properties: { + ServiceToken: { + 'Fn::GetAtt': ['CustomDashresourceDasheventDashbridgeLambdaFunction', 'Arn'], + }, + FunctionName: 'first', + EventBridgeConfig: { + EventBus: 'arn:aws:events:us-east-1:12345:event-bus/aws.partner/partner.com/12345', + Input: undefined, + InputPath: undefined, + InputTransformer: undefined, + Pattern: { + detail: { + event: ['My Event'], + type: ['track'], + }, + + source: ['aws.partner/partner.com/12345'], + }, + Schedule: undefined, + RuleName: 'first-rule-1', + }, + }, + }); + } + ); + }); + it('should create the necessary resources when using an input configuration', () => { awsCompileEventBridgeEvents.serverless.service.functions = { first: {