Skip to content

Commit

Permalink
Update readme, account for tokens in input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Sep 12, 2024
1 parent 23ee039 commit b733d84
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-pipes-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following targets are supported:
1. `targets.SqsTarget`: [Send event source to a Queue](#amazon-sqs)
2. `targets.SfnStateMachine`: [Invoke a State Machine from an event source](#aws-step-functions-state-machine)
3. `targets.LambdaFunction`: [Send event source to a Lambda Function](#aws-lambda-function)
3. `targets.CloudWatchLogsTarget`: [Send event source to a CloudWatch Logs log group](#amazon-cloudwatch-logs-log-group)

### Amazon SQS

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { ILogGroup } from 'aws-cdk-lib/aws-logs';
import { Token } from 'aws-cdk-lib';

/**
* CloudWatch Logs target properties.
Expand Down Expand Up @@ -75,15 +76,15 @@ export class CloudWatchLogsTarget implements ITarget {
}

function validateLogStreamName({ logStreamName }: CloudWatchLogsTargetParameters) {
if (logStreamName !== undefined) {
if (logStreamName !== undefined && !Token.isUnresolved(logStreamName)) {
if (logStreamName.length < 1 || logStreamName.length > 256) {
throw new Error(`Log stream name must be between 1 and 256 characters, received ${logStreamName.length}`);
}
}
}

function validateTimestamp({ timestamp }: CloudWatchLogsTargetParameters) {
if (timestamp !== undefined) {
if (timestamp !== undefined && !Token.isUnresolved(timestamp)) {
if (timestamp.length < 1 || timestamp.length > 256) {
throw new Error(`Timestamp must be between 1 and 256 characters, received ${timestamp.length}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { InputTransformation, Pipe } from '@aws-cdk/aws-pipes-alpha';
import { App, Stack } from 'aws-cdk-lib';
import { App, Lazy, Stack } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
import { TestSource } from './test-classes';
Expand Down Expand Up @@ -143,6 +143,38 @@ describe('CloudWatch Logs source parameters validation', () => {
}).toThrow('Log stream name must be between 1 and 256 characters, received 257');
});

test('validateLogStreamName works with a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const logGroup = new LogGroup(stack, 'MyLogGroup', {});
const logStreamName = Lazy.string({ produce: () => 'log-stream-name' });
const target = new CloudWatchLogsTarget(logGroup, {logStreamName});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// ASSERT
template.hasResourceProperties('AWS::Pipes::Pipe', {
Target: {
'Fn::GetAtt': [
'MyLogGroup5C0DAD85',
'Arn',
],
},
TargetParameters: {
CloudWatchLogsParameters: {
LogStreamName: 'log-stream-name',
},
},
});
});

test('Timestamp must be >= 1 character', () => {
// GIVEN
const app = new App();
Expand Down Expand Up @@ -170,4 +202,36 @@ describe('CloudWatch Logs source parameters validation', () => {
});
}).toThrow('Timestamp must be between 1 and 256 characters, received 257');
});

test('validateTimestamp works with a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const logGroup = new LogGroup(stack, 'MyLogGroup', {});
const timestamp = Lazy.string({ produce: () => 'timestamp' });
const target = new CloudWatchLogsTarget(logGroup, {timestamp});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// ASSERT
template.hasResourceProperties('AWS::Pipes::Pipe', {
Target: {
'Fn::GetAtt': [
'MyLogGroup5C0DAD85',
'Arn',
],
},
TargetParameters: {
CloudWatchLogsParameters: {
Timestamp: 'timestamp',
},
},
});
});
});

0 comments on commit b733d84

Please sign in to comment.