Skip to content

Commit

Permalink
fix(scheduler): schedule not added to group with unspecified name (#2…
Browse files Browse the repository at this point in the history
…7927)

Closes #27885.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
msambol authored Nov 20, 2023
1 parent 7447594 commit cfa2d76
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 15 deletions.
16 changes: 9 additions & 7 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/group.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as iam from 'aws-cdk-lib/aws-iam';
import { CfnScheduleGroup } from 'aws-cdk-lib/aws-scheduler';
import { Arn, ArnFormat, Aws, IResource, PhysicalName, RemovalPolicy, Resource, Stack } from 'aws-cdk-lib/core';
import { Arn, ArnFormat, Aws, IResource, Names, RemovalPolicy, Resource, Stack } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';

export interface GroupProps {
Expand Down Expand Up @@ -338,21 +338,23 @@ export class Group extends GroupBase {
public readonly groupArn: string;

public constructor(scope: Construct, id: string, props: GroupProps) {
super(scope, id, {
physicalName: props.groupName ?? PhysicalName.GENERATE_IF_NEEDED,
super(scope, id);

this.groupName = props.groupName ?? Names.uniqueResourceName(this, {
maxLength: 64,
separator: '-',
});

const group = new CfnScheduleGroup(this, 'Resource', {
name: this.physicalName,
name: this.groupName,
});

group.applyRemovalPolicy(props.removalPolicy);

this.groupArn = this.getResourceArnAttribute(group.attrArn, {
service: 'scheduler',
resource: 'schedule-group',
resourceName: this.physicalName,
resourceName: this.groupName,
});
this.groupName = this.physicalName;
}
}
}
27 changes: 26 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/test/group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ describe('Schedule Group', () => {
});
});

test('adds schedules to the group with unspecified name', () => {
const group = new Group(stack, 'TestGroup', {});
const role = iam.Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/someRole');

const schedule1 = new Schedule(stack, 'MyScheduleDummy1', {
schedule: expr,
group: group,
target: new SomeLambdaTarget(func, role),
});
const schedule2 = new Schedule(stack, 'MyScheduleDummy2', {
schedule: expr,
group: group,
target: new SomeLambdaTarget(func, role),
});

expect(schedule1.group).toEqual(group);
expect(schedule2.group).toEqual(group);

Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
Properties: {
GroupName: group.groupName,
},
});
});

test('grantReadSchedules', () => {
// GIVEN
const props: GroupProps = {
Expand Down Expand Up @@ -285,4 +310,4 @@ describe('Schedule Group', () => {
Namespace: 'AWS/Scheduler',
});
});
});
});

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 @@ -98,6 +98,22 @@
]
}
},
"NamedGroupA3ABC879": {
"Type": "AWS::Scheduler::ScheduleGroup",
"Properties": {
"Name": "TestGroup"
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"UnnamedGroupBE3E48EE": {
"Type": "AWS::Scheduler::ScheduleGroup",
"Properties": {
"Name": "awscdkschedulerschedule-UnnamedGroup-97DBE50D"
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"DefaultSchedule597B0B2C": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
Expand Down Expand Up @@ -128,6 +144,68 @@
}
}
},
"NamedGroupScheduleD7EEFEBC": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"FlexibleTimeWindow": {
"Mode": "OFF"
},
"GroupName": "TestGroup",
"ScheduleExpression": "rate(12 hours)",
"ScheduleExpressionTimezone": "Etc/UTC",
"State": "ENABLED",
"Target": {
"Arn": {
"Fn::GetAtt": [
"Function76856677",
"Arn"
]
},
"Input": "\"Input Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 180,
"MaximumRetryAttempts": 3
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
"Arn"
]
}
}
}
},
"UnnamedGroupSchedule19260E9B": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"FlexibleTimeWindow": {
"Mode": "OFF"
},
"GroupName": "awscdkschedulerschedule-UnnamedGroup-97DBE50D",
"ScheduleExpression": "rate(12 hours)",
"ScheduleExpressionTimezone": "Etc/UTC",
"State": "ENABLED",
"Target": {
"Arn": {
"Fn::GetAtt": [
"Function76856677",
"Arn"
]
},
"Input": "\"Input Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 180,
"MaximumRetryAttempts": 3
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
"Arn"
]
}
}
}
},
"DisabledScheduleA1DF7F0F": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
Expand Down

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

Loading

0 comments on commit cfa2d76

Please sign in to comment.