Skip to content

Commit

Permalink
feat(dataSources): Add Support for EventBridge DataSource (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Mar 7, 2023
1 parent 69a110a commit 2ef9b20
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 3 deletions.
17 changes: 16 additions & 1 deletion doc/dataSources.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ appSync:
```yaml
appSync:
dataSources:
api:
myDatabase:
type: 'RELATIONAL_DATABASE'
config:
databaseName: myDatabase
Expand All @@ -124,6 +124,21 @@ appSync:
- `serviceRoleArn`: The service role ARN for this DataSource. If not provided, a new one will be created.
- `iamRoleStatements`: Statements to use for the generated IAM Role. If not provided, default statements will be used.

## EventBridge

```yaml
appSync:
dataSources:
myEventBus:
type: 'AMAZON_EVENTBRIDGE'
confing:
eventBusArn: !GetAtt MyEventBus.Arn
```

### config

- `eventBusArn`: The ARN of the event bus

## NONE

```yaml
Expand Down
192 changes: 192 additions & 0 deletions src/__tests__/__snapshots__/dataSources.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,198 @@ Object {
}
`;

exports[`DataSource EventBridge should generate Resource with default role 1`] = `
Object {
"GraphQlDseventBridge": Object {
"Properties": Object {
"ApiId": Object {
"Fn::GetAtt": Array [
"GraphQlApi",
"ApiId",
],
},
"Description": "My eventBridge bus",
"EventBridgeConfig": Object {
"EventBusArn": "arn:aws:events:us-east-1:123456789012:event-bus/default",
},
"Name": "eventBridge",
"ServiceRoleArn": Object {
"Fn::GetAtt": Array [
"GraphQlDseventBridgeRole",
"Arn",
],
},
"Type": "AMAZON_EVENTBRIDGE",
},
"Type": "AWS::AppSync::DataSource",
},
"GraphQlDseventBridgeRole": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"sts:AssumeRole",
],
"Effect": "Allow",
"Principal": Object {
"Service": Array [
"appsync.amazonaws.com",
],
},
},
],
"Version": "2012-10-17",
},
"Policies": Array [
Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"events:PutEvents",
],
"Effect": "Allow",
"Resource": Array [
"arn:aws:events:us-east-1:123456789012:event-bus/default",
],
},
],
"Version": "2012-10-17",
},
"PolicyName": "AppSync-Datasource-eventBridge",
},
],
},
"Type": "AWS::IAM::Role",
},
}
`;

exports[`DataSource EventBridge should generate default role with a Ref for the bus ARN 1`] = `
Object {
"GraphQlDseventBridge": Object {
"Properties": Object {
"ApiId": Object {
"Fn::GetAtt": Array [
"GraphQlApi",
"ApiId",
],
},
"Description": "My eventBridge bus",
"EventBridgeConfig": Object {
"EventBusArn": Object {
"Fn::GetAtt": Array [
"MyEventBus",
"Arn",
],
},
},
"Name": "eventBridge",
"ServiceRoleArn": Object {
"Fn::GetAtt": Array [
"GraphQlDseventBridgeRole",
"Arn",
],
},
"Type": "AMAZON_EVENTBRIDGE",
},
"Type": "AWS::AppSync::DataSource",
},
"GraphQlDseventBridgeRole": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"sts:AssumeRole",
],
"Effect": "Allow",
"Principal": Object {
"Service": Array [
"appsync.amazonaws.com",
],
},
},
],
"Version": "2012-10-17",
},
"Policies": Array [
Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"events:PutEvents",
],
"Effect": "Allow",
"Resource": Array [
Object {
"Fn::GetAtt": Array [
"MyEventBus",
"Arn",
],
},
],
},
],
"Version": "2012-10-17",
},
"PolicyName": "AppSync-Datasource-eventBridge",
},
],
},
"Type": "AWS::IAM::Role",
},
}
`;

exports[`DataSource EventBridge should generate default role with custom statement 1`] = `
Object {
"GraphQlDseventBridgeRole": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"sts:AssumeRole",
],
"Effect": "Allow",
"Principal": Object {
"Service": Array [
"appsync.amazonaws.com",
],
},
},
],
"Version": "2012-10-17",
},
"Policies": Array [
Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"events:PutEvents",
],
"Effect": "Allow",
"Resource": Array [
"arn:aws:events:us-east-1:123456789012:event-bus/default",
"arn:aws:events:us-east-1:123456789012:event-bus/other",
],
},
],
"Version": "2012-10-17",
},
"PolicyName": "AppSync-Datasource-eventBridge",
},
],
},
"Type": "AWS::IAM::Role",
},
}
`;

exports[`DataSource HTTP should generate Resource with IAM authorization config 1`] = `
Object {
"GraphQlDsmyEndpoint": Object {
Expand Down
72 changes: 72 additions & 0 deletions src/__tests__/dataSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,78 @@ describe('DataSource', () => {
});
});

describe('EventBridge', () => {
it('should generate Resource with default role', () => {
const api = new Api(given.appSyncConfig(), plugin);
const dataSource = new DataSource(api, {
type: 'AMAZON_EVENTBRIDGE',
name: 'eventBridge',
description: 'My eventBridge bus',
config: {
eventBusArn:
'arn:aws:events:us-east-1:123456789012:event-bus/default',
},
});

expect(dataSource.compile()).toMatchSnapshot();
});

it('should generate default role with a Ref for the bus ARN', () => {
const api = new Api(given.appSyncConfig(), plugin);
const dataSource = new DataSource(api, {
type: 'AMAZON_EVENTBRIDGE',
name: 'eventBridge',
description: 'My eventBridge bus',
config: {
eventBusArn: { 'Fn::GetAtt': ['MyEventBus', 'Arn'] },
},
});

expect(dataSource.compile()).toMatchSnapshot();
});

it('should generate default role with custom statement', () => {
const api = new Api(given.appSyncConfig(), plugin);
const dataSource = new DataSource(api, {
type: 'AMAZON_EVENTBRIDGE',
name: 'eventBridge',
description: 'My eventBridge bus',
config: {
eventBusArn:
'arn:aws:events:us-east-1:123456789012:event-bus/default',
iamRoleStatements: [
{
Effect: 'Allow',
Action: ['events:PutEvents'],
Resource: [
'arn:aws:events:us-east-1:123456789012:event-bus/default',
'arn:aws:events:us-east-1:123456789012:event-bus/other',
],
},
],
},
});

expect(dataSource.compileDataSourceIamRole()).toMatchSnapshot();
});

it('should not generate default role when a service role arn is passed', () => {
const api = new Api(given.appSyncConfig(), plugin);
const dataSource = new DataSource(api, {
type: 'AMAZON_EVENTBRIDGE',
name: 'eventBridge',
description: 'My eventBridge bus',
config: {
eventBusArn:
'arn:aws:events:us-east-1:123456789012:event-bus/default',
serviceRoleArn: 'arn:aws:iam:',
},
});

expect(dataSource.compileDataSourceIamRole()).toBeUndefined();
});
});

describe('AWS Lambda', () => {
it('should generate Resource with default role', () => {
const api = new Api(given.appSyncConfig(), plugin);
Expand Down
17 changes: 16 additions & 1 deletion src/__tests__/validation/__snapshots__/datasources.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Basic Invalid should validate: Invalid Datasource 1`] = `
"/dataSources/myDynamoSource1/type: must be one of AMAZON_DYNAMODB, AMAZON_OPENSEARCH_SERVICE, AWS_LAMBDA, HTTP, NONE, RELATIONAL_DATABASE
"/dataSources/myDynamoSource1/type: must be one of AMAZON_DYNAMODB, AMAZON_OPENSEARCH_SERVICE, AWS_LAMBDA, HTTP, NONE, RELATIONAL_DATABASE, AMAZON_EVENTBRIDGE
/dataSources: contains invalid data source definitions"
`;

Expand All @@ -28,6 +28,21 @@ exports[`DynamoDB Invalid should validate: Missing config 1`] = `
/dataSources: contains invalid data source definitions"
`;

exports[`EventBridge Invalid should not validate: Empty config 1`] = `
"/dataSources/myEventBridgeSource1/config: must have required property 'eventBusArn'
/dataSources: contains invalid data source definitions"
`;

exports[`EventBridge Invalid should not validate: Invalid config 1`] = `
"/dataSources/myEventBridgeSource1/config/eventBusArn: must be a string or a CloudFormation intrinsic function
/dataSources: contains invalid data source definitions"
`;

exports[`EventBridge Invalid should not validate: Missing config 1`] = `
"/dataSources/myEventBridgeSource1: must have required property 'config'
/dataSources: contains invalid data source definitions"
`;

exports[`HTTP Invalid should validate: Empty config 1`] = `
"/dataSources/http1/config: must have required property 'endpoint'
/dataSources: contains invalid data source definitions"
Expand Down
Loading

0 comments on commit 2ef9b20

Please sign in to comment.