Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scheduler-alpha): scheduler input always get transformed to string with extra double quotes #31894

Merged
merged 23 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ff1e462
reduce key permissions. pending test changes
samson-keung Oct 24, 2024
9025add
update integ test to have less KMS permissions
samson-keung Oct 24, 2024
fb485c7
revise schedule integ test to have assertion
samson-keung Oct 24, 2024
d6d4a7d
Integ assertion is updated to poll for max 10 messages from SQS
samson-keung Oct 24, 2024
6f0f045
Add missing permission to Schedule to send messages to queue
samson-keung Oct 24, 2024
88dad4f
make assertion wait time less. 5 minutes
samson-keung Oct 24, 2024
1447087
update schedule time so that they do not get invoked
samson-keung Oct 24, 2024
5b81a3e
fix input fromText has extra quotations
samson-keung Oct 25, 2024
37091ff
update snapshot
samson-keung Oct 25, 2024
cc45044
update integ test
samson-keung Oct 25, 2024
4e980a2
update SNS target integ test
samson-keung Oct 25, 2024
3001fb0
update test with input logic update
samson-keung Oct 25, 2024
c8ef60d
update sqs target integ test
samson-keung Oct 25, 2024
6d0c9c3
update LambdaInvoke integ test
samson-keung Oct 25, 2024
9fc50c5
revert reducing key permissions
samson-keung Oct 25, 2024
d8992ee
revert integ.schedule.ts change
samson-keung Oct 25, 2024
3f3e132
fix integ.schedule test
samson-keung Oct 25, 2024
fabef86
fix integ.schedule test
samson-keung Oct 25, 2024
7c67ac0
fix integ.schedule test
samson-keung Oct 25, 2024
455a092
update integ
samson-keung Oct 26, 2024
550cc14
add unit test for input
samson-keung Oct 26, 2024
404f0aa
Merge branch 'main' into scheduler-reduce-key-permissions
samson-keung Oct 28, 2024
09521ad
Merge branch 'main' into scheduler-reduce-key-permissions
mergify[bot] Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class ScheduleTargetInput {
* @param text Text to use as the input for the target
*/
public static fromText(text: string): ScheduleTargetInput {
return new FieldAwareEventInput(text);
return new FieldAwareEventInput(text, false);
}

/**
Expand All @@ -28,7 +28,7 @@ export abstract class ScheduleTargetInput {
* @param obj object to use to convert to JSON to use as input for the target
*/
public static fromObject(obj: any): ScheduleTargetInput {
return new FieldAwareEventInput(obj);
return new FieldAwareEventInput(obj, true);
}

protected constructor() {
Expand All @@ -41,7 +41,7 @@ export abstract class ScheduleTargetInput {
}

class FieldAwareEventInput extends ScheduleTargetInput {
constructor(private readonly input: any) {
constructor(private readonly input: any, private readonly toJsonString: boolean) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably a better implementation approach but since this is only used internal/private in the class, I will fix the behaviour for now, then improve the design for easier readability and maintainability in subsequent PRs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine honestly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a similar class in aws-events where the second parameter is the inputType and the bind method handles the input accordingly. Could we do something similar here?

super();
}

Expand All @@ -57,10 +57,11 @@ class FieldAwareEventInput extends ScheduleTargetInput {
}

const stack = Stack.of(schedule);
return stack.toJsonString(Tokenization.resolve(this.input, {
const inputString = Tokenization.resolve(this.input, {
scope: schedule,
resolver: new Replacer(),
}));
});
return this.toJsonString ? stack.toJsonString(inputString) : inputString;
}
}

Expand Down
26 changes: 21 additions & 5 deletions packages/@aws-cdk/aws-scheduler-alpha/test/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,44 @@ describe('schedule target input', () => {
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: '"test"',
Input: 'test',
},
},
});
});

test('create an input from text with a ref inside', () => {
test('create an input from text concatenated from literal string with a token', () => {
new Schedule(stack, 'MyScheduleDummy', {
schedule: expr,
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(stack.account)),
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(`ac-${stack.account}`)),
});

Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: {
'Fn::Join': ['', ['"', { Ref: 'AWS::AccountId' }, '"']],
'Fn::Join': ['', ['ac-', { Ref: 'AWS::AccountId' }]],
},
},
},
});
});

test('create an input from text with a ref inside', () => {
new Schedule(stack, 'MyScheduleDummy', {
schedule: expr,
target: new SomeLambdaTarget(func, ScheduleTargetInput.fromText(stack.account)),
});

Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: { Ref: 'AWS::AccountId' },
},
},
});
});

test('create an input from object', () => {
const input = ScheduleTargetInput.fromObject({
test: 'test',
Expand Down Expand Up @@ -115,7 +131,7 @@ describe('schedule target input', () => {
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
Target: {
Input: '"Test=<aws.scheduler.schedule-arn>"',
Input: 'Test=<aws.scheduler.schedule-arn>',
},
},
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SomeLambdaTarget implements scheduler.IScheduleTarget {
return {
arn: this.fn.functionArn,
role: this.role,
input: scheduler.ScheduleTargetInput.fromText('Input Text'),
input: scheduler.ScheduleTargetInput.fromObject('Input Text'),
retryPolicy: {
maximumEventAgeInSeconds: 180,
maximumRetryAttempts: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const importedLambdaTagValue = 'importedLambdaTagValue';
new scheduler.Schedule(scheduleStack, 'ScheduleWithImportedLambda', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new LambdaInvoke(importedFunc, {
input: scheduler.ScheduleTargetInput.fromText(importedLambdaTagValue),
input: scheduler.ScheduleTargetInput.fromObject(importedLambdaTagValue),
}),
});

Expand All @@ -88,7 +88,7 @@ const sameStackLambdaTagValue = 'sameStackLambdaTagValue';
new scheduler.Schedule(scheduleStack, 'ScheduleWithSameStackLambda', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new LambdaInvoke(sameStackFunc, {
input: scheduler.ScheduleTargetInput.fromText(sameStackLambdaTagValue),
input: scheduler.ScheduleTargetInput.fromObject(sameStackLambdaTagValue),
}),
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"Arn": {
"Ref": "TopicBFC7AF6E"
},
"Input": "\"Hello, Scheduler!\"",
"Input": "Hello, Scheduler!",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 86400,
"MaximumRetryAttempts": 185
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading