Skip to content

Commit

Permalink
Get rid of generics
Browse files Browse the repository at this point in the history
  • Loading branch information
bracki committed Feb 12, 2021
1 parent 70bcfb7 commit f7d8775
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-event-sources/lib/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface DynamoEventSourceProps extends StreamEventSourceProps {
/**
* Use an Amazon DynamoDB stream as an event source for AWS Lambda.
*/
export class DynamoEventSource extends StreamEventSource<DynamoEventSourceProps> {
export class DynamoEventSource extends StreamEventSource {
private _eventSourceMappingId?: string = undefined;

constructor(private readonly table: dynamodb.ITable, props: DynamoEventSourceProps) {
Expand Down
46 changes: 26 additions & 20 deletions packages/@aws-cdk/aws-lambda-event-sources/lib/kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,32 @@ export interface SelfManagedKafkaEventSourceProps extends KafkaEventSourceProps
/**
* Use a MSK cluster as a streaming source for AWS Lambda
*/
export class ManagedKafkaEventSource extends StreamEventSource<ManagedKafkaEventSourceProps> {
export class ManagedKafkaEventSource extends StreamEventSource {
// This is to work around JSII inheritance problems
private innerProps: ManagedKafkaEventSourceProps;

constructor(props: ManagedKafkaEventSourceProps) {
super(props);
this.innerProps = props;
}

public bind(target: lambda.IFunction) {
target.addEventSourceMapping(
`KafkaEventSource:${this.props.clusterArn}${this.props.topic}`,
`KafkaEventSource:${this.innerProps.clusterArn}${this.innerProps.topic}`,
this.enrichMappingOptions({
eventSourceArn: this.props.clusterArn,
startingPosition: this.props.startingPosition,
sourceAccessConfigurations: [{ type: lambda.SourceAccessConfigurationType.SASL_SCRAM_512_AUTH, uri: this.props.secret.secretArn }],
kafkaTopic: this.props.topic,
eventSourceArn: this.innerProps.clusterArn,
startingPosition: this.innerProps.startingPosition,
sourceAccessConfigurations: [{ type: lambda.SourceAccessConfigurationType.SASL_SCRAM_512_AUTH, uri: this.innerProps.secret.secretArn }],
kafkaTopic: this.innerProps.topic,
}),
);

this.props.secret.grantRead(target);
this.innerProps.secret.grantRead(target);

target.addToRolePolicy(new iam.PolicyStatement(
{
actions: ['kafka:DescribeCluster', 'kafka:GetBootstrapBrokers', 'kafka:ListScramSecrets'],
resources: [this.props.clusterArn],
resources: [this.innerProps.clusterArn],
},
));

Expand All @@ -104,44 +107,47 @@ export class ManagedKafkaEventSource extends StreamEventSource<ManagedKafkaEvent
/**
* Use a self hosted Kafka installation as a streaming source for AWS Lambda.
*/
export class SelfManagedKafkaEventSource extends StreamEventSource<SelfManagedKafkaEventSourceProps> {
export class SelfManagedKafkaEventSource extends StreamEventSource {
// This is to work around JSII inheritance problems
private innerProps: SelfManagedKafkaEventSourceProps;

constructor(props: SelfManagedKafkaEventSourceProps) {
super(props);
if ((props.securityGroup !== undefined && props.vpcSubnets == undefined) ||
(props.securityGroup == undefined && props.vpcSubnets !== undefined )) {
throw new Error('both subnets and securityGroup must be set');
}
this.innerProps = props;
}

public bind(target: lambda.IFunction) {
let authenticationMethod;
if (this.props.authenticationMethod == undefined || this.props.authenticationMethod == 'SASL_SCRAM_512_AUTH') {
if (this.innerProps.authenticationMethod == undefined || this.innerProps.authenticationMethod == 'SASL_SCRAM_512_AUTH') {
authenticationMethod = lambda.SourceAccessConfigurationType.SASL_SCRAM_512_AUTH;
} else {
authenticationMethod = lambda.SourceAccessConfigurationType.SASL_SCRAM_256_AUTH;
}
let sourceAccessConfigurations = [{ type: authenticationMethod, uri: this.props.secret.secretArn }];
if (this.props.vpcSubnets !== undefined && this.props.securityGroup !== undefined) {
let sourceAccessConfigurations = [{ type: authenticationMethod, uri: this.innerProps.secret.secretArn }];
if (this.innerProps.vpcSubnets !== undefined && this.innerProps.securityGroup !== undefined) {
sourceAccessConfigurations.push({
type: lambda.SourceAccessConfigurationType.VPC_SECURITY_GROUP,
uri: this.props.securityGroup.securityGroupId,
uri: this.innerProps.securityGroup.securityGroupId,
},
);
this.props.vpc?.selectSubnets(this.props.vpcSubnets).subnetIds.forEach((id) => {
this.innerProps.vpc?.selectSubnets(this.innerProps.vpcSubnets).subnetIds.forEach((id) => {
sourceAccessConfigurations.push({ type: lambda.SourceAccessConfigurationType.VPC_SUBNET, uri: id });
});
}
const idHash = crypto.createHash('md5').update(JSON.stringify(this.props.bootstrapServers)).digest('hex');
const idHash = crypto.createHash('md5').update(JSON.stringify(this.innerProps.bootstrapServers)).digest('hex');
target.addEventSourceMapping(
`KafkaEventSource:${idHash}:${this.props.topic}`,
`KafkaEventSource:${idHash}:${this.innerProps.topic}`,
this.enrichMappingOptions({
kafkaBootstrapServers: this.props.bootstrapServers,
kafkaTopic: this.props.topic,
startingPosition: this.props.startingPosition,
kafkaBootstrapServers: this.innerProps.bootstrapServers,
kafkaTopic: this.innerProps.topic,
startingPosition: this.innerProps.startingPosition,
sourceAccessConfigurations,
}),
);
this.props.secret.grantRead(target);
this.innerProps.secret.grantRead(target);
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-event-sources/lib/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface KinesisEventSourceProps extends StreamEventSourceProps {
/**
* Use an Amazon Kinesis stream as an event source for AWS Lambda.
*/
export class KinesisEventSource extends StreamEventSource<KinesisEventSourceProps> {
export class KinesisEventSource extends StreamEventSource {
private _eventSourceMappingId?: string = undefined;

constructor(readonly stream: kinesis.IStream, props: KinesisEventSourceProps) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export interface StreamEventSourceProps {
/**
* Use an stream as an event source for AWS Lambda.
*/
export abstract class StreamEventSource<T extends StreamEventSourceProps> implements lambda.IEventSource {
protected constructor(protected readonly props: T) {
export abstract class StreamEventSource implements lambda.IEventSource {
protected constructor(protected readonly props: StreamEventSourceProps) {
}

public abstract bind(_target: lambda.IFunction): void;
Expand Down

0 comments on commit f7d8775

Please sign in to comment.