Skip to content

Commit

Permalink
Merge pull request #256 from umccr/feature/universal-event-archiver
Browse files Browse the repository at this point in the history
custom event bus archiver props and stateful unit testing fixing
  • Loading branch information
raylrui authored Apr 29, 2024
2 parents ebeea05 + f7160eb commit 1b81581
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
20 changes: 14 additions & 6 deletions config/stacks/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
} from '../constants';
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import { SchemaRegistryProps } from '../../lib/workload/stateful/stacks/shared/constructs/schema-registry';
import { EventBusProps } from '../../lib/workload/stateful/stacks/shared/constructs/event-bus';
import {
EventBusProps,
EventBusArchiverProps,
} from '../../lib/workload/stateful/stacks/shared/constructs/event-bus';
import { ComputeProps } from '../../lib/workload/stateful/stacks/shared/constructs/compute';
import { EventSourceProps } from '../../lib/workload/stateful/stacks/shared/constructs/event-source';

Expand All @@ -37,31 +40,36 @@ const getEventBusConstructProps = (n: AccountName): EventBusProps => {
archiveName: 'OrcaBusMainArchive',
archiveDescription: 'OrcaBus main event bus archive',
archiveRetention: 365,
};

// common config for custom event archiver
const baseUniversalEventArchiverProps: EventBusArchiverProps = {
vpcProps: vpcProps,
archiveBucketName: 'orcabus-universal-events-archive-' + accountIdAlias[n],
lambdaSecurityGroupName: 'OrcaBusSharedEventBusEventArchiveSecurityGroup',
lambdaSecurityGroupName: 'OrcaBusSharedEventBusUniversalEventArchiveSecurityGroup',
bucketRemovalPolicy: RemovalPolicy.DESTROY,
};

switch (n) {
case 'beta':
return {
...baseConfig,
addCustomEventArchiver: true,
bucketRemovalPolicy: RemovalPolicy.DESTROY,
universalEventArchiverProps: baseUniversalEventArchiverProps,
};
case 'gamma':
return {
...baseConfig,
addCustomEventArchiver: true,
bucketRemovalPolicy: RemovalPolicy.DESTROY,
universalEventArchiverProps: baseUniversalEventArchiverProps,
};
case 'prod':
return {
...baseConfig,
addCustomEventArchiver: true,
bucketRemovalPolicy: RemovalPolicy.RETAIN,
universalEventArchiverProps: {
...baseUniversalEventArchiverProps,
bucketRemovalPolicy: RemovalPolicy.RETAIN,
},
};
}
};
Expand Down
29 changes: 12 additions & 17 deletions lib/workload/stateful/stacks/shared/constructs/event-bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { Vpc, VpcLookupOptions, SecurityGroup } from 'aws-cdk-lib/aws-ec2';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { UniversalEventArchiverConstruct } from './custom-event-archiver/construct/universal-event-archiver';

// generic event bus archiver props
export interface EventBusArchiverProps {
vpcProps: VpcLookupOptions;
lambdaSecurityGroupName: string;
archiveBucketName: string;
bucketRemovalPolicy: RemovalPolicy;
}

export interface EventBusProps {
eventBusName: string;
archiveName: string;
Expand All @@ -13,10 +21,7 @@ export interface EventBusProps {

// Optional for custom event archiver
addCustomEventArchiver?: boolean;
vpcProps?: VpcLookupOptions;
lambdaSecurityGroupName?: string;
archiveBucketName?: string;
bucketRemovalPolicy?: RemovalPolicy;
universalEventArchiverProps?: EventBusArchiverProps;
}

export class EventBusConstruct extends Construct {
Expand All @@ -28,7 +33,8 @@ export class EventBusConstruct extends Construct {

// Optional for custom event archiver
if (props.addCustomEventArchiver) {
this.createUniversalEventArchiver(props);
props.universalEventArchiverProps &&
this.createUniversalEventArchiver(props.universalEventArchiverProps);
}
}

Expand All @@ -49,18 +55,7 @@ export class EventBusConstruct extends Construct {
return mainBus;
}

private createUniversalEventArchiver(props: EventBusProps) {
if (
!props.vpcProps ||
!props.archiveBucketName ||
!props.lambdaSecurityGroupName ||
!props.bucketRemovalPolicy
) {
throw new Error(
'VPC, Security Group, Archive Bucket Name and Removal Policy are required for custom event archiver function.'
);
}

private createUniversalEventArchiver(props: EventBusArchiverProps) {
const vpc = Vpc.fromLookup(this, 'MainVpc', props.vpcProps);

// dedicated bucket for archiving all events
Expand Down
11 changes: 6 additions & 5 deletions test/stateful/shared/eventbusConstruct.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { Template, Match } from 'aws-cdk-lib/assertions';
import { getEnvironmentConfig } from '../../../config/config';
import { EventBusConstruct } from '../../../lib/workload/stateful/stacks/shared/constructs/event-bus';
import { vpcProps } from '../../../config/constants';
Expand Down Expand Up @@ -31,11 +31,12 @@ test('Test EventBusConstruct Creation With Custome Events Archiver', () => {
template.hasResourceProperties('AWS::Events::EventBus', {
Name: 'OrcaBusMain',
});
template.hasResourceProperties('AWS::S3::Bucket', {
BucketName: 'event-archive-bucket',
});
template.hasResourceProperties(
'AWS::S3::Bucket',
Match.objectLike({ BucketName: Match.stringLikeRegexp('orcabus-universal-events-') })
);
template.hasResourceProperties('AWS::EC2::SecurityGroup', {
GroupName: 'OrcaBusEventArchiveSecurityGroup',
GroupName: 'OrcaBusSharedEventBusUniversalEventArchiveSecurityGroup',
});
template.hasResourceProperties('AWS::Lambda::Function', {
Handler: 'universal_event_archiver.handler',
Expand Down

0 comments on commit 1b81581

Please sign in to comment.