From b1f76b5e78d243874cbdb190ef3922505a2f30e9 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Wed, 12 Jun 2019 14:52:57 +0200 Subject: [PATCH 1/5] chore: fix build issue introduced by parallel PRs --- packages/@aws-cdk/aws-lambda/test/test.alias.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda/test/test.alias.ts b/packages/@aws-cdk/aws-lambda/test/test.alias.ts index 2ffac353d2b20..c641ad1e6c970 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.alias.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.alias.ts @@ -45,7 +45,7 @@ export = { const fn = new lambda.Function(stack, 'MyLambda', { code: new lambda.InlineCode('hello()'), handler: 'index.hello', - runtime: lambda.Runtime.NodeJS810, + runtime: lambda.Runtime.Nodejs810, }); new lambda.Alias(stack, 'Alias', { From 1f6860a14c827c20985b0c4418934c6d3e1b7b82 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Wed, 12 Jun 2019 14:59:37 +0200 Subject: [PATCH 2/5] Found another one! --- .../aws-cloudfront/test/integ.cloudfront-lambda-association.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-lambda-association.ts b/packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-lambda-association.ts index d02adde784341..231e42ffab952 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-lambda-association.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-lambda-association.ts @@ -14,7 +14,7 @@ const sourceBucket = new s3.Bucket(stack, 'Bucket', { const lambdaFunction = new lambda.Function(stack, 'Lambda', { code: lambda.Code.inline('foo'), handler: 'index.handler', - runtime: lambda.Runtime.NodeJS810 + runtime: lambda.Runtime.Nodejs810 }); const lambdaVersion = new lambda.Version(stack, 'LambdaVersion', { From fc44fcf785c209ceeae92a35d1c877ce1fecb4e8 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Wed, 12 Jun 2019 16:29:32 +0200 Subject: [PATCH 3/5] refactor(events): clean up Events APIs Biggest change is modeling the schedule types. BREAKING CHANGE: * **events**: `EventPattern.detail` is now a map. * **events**: `scheduleExpression: string` is now `schedule: Schedule`. --- .../lib/ecs/scheduled-ecs-task.ts | 2 +- .../@aws-cdk/aws-events/lib/event-pattern.ts | 19 ++- packages/@aws-cdk/aws-events/lib/index.ts | 1 + packages/@aws-cdk/aws-events/lib/rule.ts | 5 +- packages/@aws-cdk/aws-events/lib/schedule.ts | 132 ++++++++++++++++++ .../@aws-cdk/aws-events/test/test.input.ts | 10 +- .../@aws-cdk/aws-events/test/test.rule.ts | 22 +-- .../@aws-cdk/aws-events/test/test.schedule.ts | 32 +++++ 8 files changed, 205 insertions(+), 18 deletions(-) create mode 100644 packages/@aws-cdk/aws-events/lib/schedule.ts create mode 100644 packages/@aws-cdk/aws-events/test/test.schedule.ts diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts index 6f28112031fab..ff239cf2b2f91 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts @@ -21,7 +21,7 @@ export interface ScheduledEc2TaskProps { * * @see http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html */ - readonly scheduleExpression: string; + readonly schedule: events.Schedule; /** * The CMD value to pass to the container. A string with commands delimited by commas. diff --git a/packages/@aws-cdk/aws-events/lib/event-pattern.ts b/packages/@aws-cdk/aws-events/lib/event-pattern.ts index 49e2e2ea3f96b..bdd586f7e0e78 100644 --- a/packages/@aws-cdk/aws-events/lib/event-pattern.ts +++ b/packages/@aws-cdk/aws-events/lib/event-pattern.ts @@ -30,12 +30,16 @@ export interface EventPattern { /** * By default, this is set to 0 (zero) in all events. + * + * @default - No filtering on version */ readonly version?: string[]; /** * A unique value is generated for every event. This can be helpful in * tracing events as they move through rules to targets, and are processed. + * + * @default - No filtering on id */ readonly id?: string[]; @@ -44,6 +48,8 @@ export interface EventPattern { * that appear in the detail field. * * Represents the "detail-type" event field. + * + * @default - No filtering on detail type */ readonly detailType?: string[]; @@ -58,11 +64,14 @@ export interface EventPattern { * CloudFront is aws.cloudfront. * * @see http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces + * @default - No filtering on source */ readonly source?: string[]; /** * The 12-digit number identifying an AWS account. + * + * @default - No filtering on account */ readonly account?: string[]; @@ -71,11 +80,15 @@ export interface EventPattern { * the event. If the event spans a time interval, the service might choose * to report the start time, so this value can be noticeably before the time * the event is actually received. + * + * @default - No filtering on time */ readonly time?: string[]; /** * Identifies the AWS region where the event originated. + * + * @default - No filtering on region */ readonly region?: string[]; @@ -88,12 +101,16 @@ export interface EventPattern { * instance ARNs, Auto Scaling events include ARNs for both instances and * Auto Scaling groups, but API calls with AWS CloudTrail do not include * resource ARNs. + * + * @default - No filtering on resource */ readonly resources?: string[]; /** * A JSON object, whose content is at the discretion of the service * originating the event. + * + * @default - No filtering on detail */ - readonly detail?: any; + readonly detail?: {[key: string]: any}; } diff --git a/packages/@aws-cdk/aws-events/lib/index.ts b/packages/@aws-cdk/aws-events/lib/index.ts index 0409be7ae29d5..c7815e08b1c64 100644 --- a/packages/@aws-cdk/aws-events/lib/index.ts +++ b/packages/@aws-cdk/aws-events/lib/index.ts @@ -3,6 +3,7 @@ export * from './rule'; export * from './rule-ref'; export * from './target'; export * from './event-pattern'; +export * from './schedule'; export * from './on-event-options'; // AWS::Events CloudFormation Resources: diff --git a/packages/@aws-cdk/aws-events/lib/rule.ts b/packages/@aws-cdk/aws-events/lib/rule.ts index 3a3993ac3ae4c..af428b9929e75 100644 --- a/packages/@aws-cdk/aws-events/lib/rule.ts +++ b/packages/@aws-cdk/aws-events/lib/rule.ts @@ -2,6 +2,7 @@ import { Construct, Lazy, Resource } from '@aws-cdk/cdk'; import { EventPattern } from './event-pattern'; import { CfnRule } from './events.generated'; import { IRule } from './rule-ref'; +import { Schedule } from './schedule'; import { IRuleTarget } from './target'; import { mergeEventPattern } from './util'; @@ -39,7 +40,7 @@ export interface RuleProps { * * @default - None. */ - readonly scheduleExpression?: string; + readonly schedule?: Schedule; /** * Describes which events CloudWatch Events routes to the specified target. @@ -104,7 +105,7 @@ export class Rule extends Resource implements IRule { this.ruleArn = resource.ruleArn; this.addEventPattern(props.eventPattern); - this.scheduleExpression = props.scheduleExpression; + this.scheduleExpression = props.schedule && props.schedule.expression; for (const target of props.targets || []) { this.addTarget(target); diff --git a/packages/@aws-cdk/aws-events/lib/schedule.ts b/packages/@aws-cdk/aws-events/lib/schedule.ts new file mode 100644 index 0000000000000..bf1007775fc42 --- /dev/null +++ b/packages/@aws-cdk/aws-events/lib/schedule.ts @@ -0,0 +1,132 @@ +/** + * Schedule for scheduled event rules + */ +export abstract class Schedule { + /** + * Construct a schedule from a literal schedule expression + * + * @param expression The expression to use. Must be in a format that Cloudwatch Events will recognize + */ + public static fromExpression(expression: string): Schedule { + return new LiteralSchedule(expression); + } + + /** + * Construct a schedule from an interval and a time unit + */ + public static fromRate(interval: number, unit: TimeUnit = TimeUnit.Hour): Schedule { + const unitStr = interval !== 1 ? `${unit}s` : unit; + + return new LiteralSchedule(`rate(${interval} ${unitStr})`); + } + + /** + * Create a schedule from a set of cron fields + */ + public static fromCron(options: CronOptions): Schedule { + if (options.weekDay !== undefined && options.day !== undefined) { + throw new Error(`Cannot supply both 'day' and 'weekDay', use at most one`); + } + + const minute = ifUndef(options.minute, '*'); + const hour = ifUndef(options.hour, '*'); + const month = ifUndef(options.month, '*'); + const year = ifUndef(options.year, '*'); + + // Weekday defaults to '?' if not supplied. If it is supplied, day must become '?' + const day = ifUndef(options.day, options.weekDay !== undefined ? '?' : '*'); + const weekDay = ifUndef(options.weekDay, '?'); + + return new LiteralSchedule(`cron(${minute} ${hour} ${day} ${month} ${weekDay} ${year})`); + } + + /** + * Retrieve the expression for this schedule + */ + public abstract readonly expression: string; + + protected constructor() { + } +} + +/** + * What unit to interpret the rate in + */ +export enum TimeUnit { + /** + * The rate is in minutes + */ + Minute = 'minute', + + /** + * The rate is in hours + */ + Hour = 'hour', + + /** + * The rate is in days + */ + Day = 'day' +} + +/** + * Options to configure a cron expression + * + * All fields are strings so you can use complex expresions. Absence of + * a field implies '*' or '?', whichever one is appropriate. + * + * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions + */ +export interface CronOptions { + /** + * The minute to run this rule at + * + * @default - Every minute + */ + readonly minute?: string; + + /** + * The hour to run this rule at + * + * @default - Every hour + */ + readonly hour?: string; + + /** + * The day of the month to run this rule at + * + * @default - Every day of the month + */ + readonly day?: string; + + /** + * The month to run this rule at + * + * @default - Every month + */ + readonly month?: string; + + /** + * The year to run this rule at + * + * @default - Every year + */ + readonly year?: string; + + /** + * The day of the week to run this rule at + * + * @default - Any day of the week + */ + readonly weekDay?: string; +} + +class LiteralSchedule extends Schedule { + constructor(public readonly expression: string) { + super(); + } +} + +function ifUndef(x: T | undefined, def: T): T { + return x === undefined ? def : x; +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events/test/test.input.ts b/packages/@aws-cdk/aws-events/test/test.input.ts index 4fe918f32e245..980c11a7d1abb 100644 --- a/packages/@aws-cdk/aws-events/test/test.input.ts +++ b/packages/@aws-cdk/aws-events/test/test.input.ts @@ -2,7 +2,7 @@ import { expect, haveResourceLike } from '@aws-cdk/assert'; import cdk = require('@aws-cdk/cdk'); import { Stack } from '@aws-cdk/cdk'; import { Test } from 'nodeunit'; -import { IRuleTarget, RuleTargetInput } from '../lib'; +import { IRuleTarget, RuleTargetInput, Schedule, TimeUnit } from '../lib'; import { Rule } from '../lib/rule'; export = { @@ -11,7 +11,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)' + schedule: Schedule.fromRate(1, TimeUnit.Minute), }); // WHEN @@ -34,7 +34,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)' + schedule: Schedule.fromRate(1, TimeUnit.Minute), }); // WHEN @@ -56,7 +56,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)' + schedule: Schedule.fromRate(1, TimeUnit.Minute), }); // WHEN @@ -78,7 +78,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)' + schedule: Schedule.fromRate(1, TimeUnit.Minute), }); const world = cdk.Lazy.stringValue({ produce: () => 'world' }); diff --git a/packages/@aws-cdk/aws-events/test/test.rule.ts b/packages/@aws-cdk/aws-events/test/test.rule.ts index 5ed5cba222f5b..e0788c503abde 100644 --- a/packages/@aws-cdk/aws-events/test/test.rule.ts +++ b/packages/@aws-cdk/aws-events/test/test.rule.ts @@ -4,7 +4,7 @@ import { ServicePrincipal } from '@aws-cdk/aws-iam'; import cdk = require('@aws-cdk/cdk'); import { Stack } from '@aws-cdk/cdk'; import { Test } from 'nodeunit'; -import { EventField, IRule, IRuleTarget, RuleTargetInput } from '../lib'; +import { EventField, IRule, IRuleTarget, RuleTargetInput, Schedule, TimeUnit } from '../lib'; import { Rule } from '../lib/rule'; // tslint:disable:object-literal-key-quotes @@ -14,7 +14,7 @@ export = { const stack = new cdk.Stack(); new Rule(stack, 'MyRule', { - scheduleExpression: 'rate(10 minutes)' + schedule: Schedule.fromRate(10, TimeUnit.Minute), }); expect(stack).toMatch({ @@ -37,8 +37,8 @@ export = { // WHEN new Rule(stack, 'MyRule', { - ruleName: 'PhysicalName', - scheduleExpression: 'rate(10 minutes)' + ruleName: 'PhysicalName', + schedule: Schedule.fromRate(10, TimeUnit.Minute), }); // THEN @@ -174,7 +174,7 @@ export = { const rule = new Rule(stack, 'EventRule', { targets: [ t1 ], - scheduleExpression: 'rate(5 minutes)' + schedule: Schedule.fromRate(5, TimeUnit.Minute), }); rule.addTarget(t2); @@ -215,7 +215,9 @@ export = { 'input template can contain tokens'(test: Test) { const stack = new cdk.Stack(); - const rule = new Rule(stack, 'EventRule', { scheduleExpression: 'rate(1 minute)' }); + const rule = new Rule(stack, 'EventRule', { + schedule: Schedule.fromRate(1, TimeUnit.Minute), + }); // a plain string should just be stringified (i.e. double quotes added and escaped) rule.addTarget({ @@ -298,7 +300,9 @@ export = { // GIVEN const stack = new cdk.Stack(); - const rule = new Rule(stack, 'EventRule', { scheduleExpression: 'rate(1 minute)' }); + const rule = new Rule(stack, 'EventRule', { + schedule: Schedule.fromRate(1, TimeUnit.Minute), + }); const role = new iam.Role(stack, 'SomeRole', { assumedBy: new ServicePrincipal('nobody') @@ -372,7 +376,7 @@ export = { // WHEN new Rule(stack, 'Rule', { - scheduleExpression: 'foom', + schedule: Schedule.fromExpression('foom'), enabled: false }); @@ -388,7 +392,7 @@ export = { // GIVEN const stack = new cdk.Stack(); const rule = new Rule(stack, 'Rule', { - scheduleExpression: 'foom', + schedule: Schedule.fromExpression('foom'), enabled: false }); rule.addTarget(new SomeTarget()); diff --git a/packages/@aws-cdk/aws-events/test/test.schedule.ts b/packages/@aws-cdk/aws-events/test/test.schedule.ts new file mode 100644 index 0000000000000..06715c2261cd2 --- /dev/null +++ b/packages/@aws-cdk/aws-events/test/test.schedule.ts @@ -0,0 +1,32 @@ +import { Test } from 'nodeunit'; +import events = require('../lib'); + +export = { + 'cron expressions day and dow are mutex: given weekday'(test: Test) { + // Run every 10 minutes Monday through Friday + test.equal('cron(0/10 * ? * MON-FRI *)', events.Schedule.fromCron({ + minute: '0/10', + weekDay: 'MON-FRI' + }).expression); + test.done(); + }, + + 'cron expressions day and dow are mutex: given month day'(test: Test) { + // Run at 8:00 am (UTC) every 1st day of the month + test.equal('cron(0 8 1 * ? *)', events.Schedule.fromCron({ + minute: '0', + hour: '8', + day: '1', + }).expression); + test.done(); + }, + + 'cron expressions day and dow are mutex: given neither'(test: Test) { + // Run at 10:00 am (UTC) every day + test.equal('cron(0 10 * * ? *)', events.Schedule.fromCron({ + minute: '0', + hour: '10', + }).expression); + test.done(); + }, +}; From 61969fc4d373ed3aeb1ee6e3e275db18fbfc33ec Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Wed, 12 Jun 2019 16:42:27 +0200 Subject: [PATCH 4/5] Update usage sites --- .../aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts | 2 +- .../test/ec2/integ.scheduled-ecs-task.lit.ts | 3 ++- .../aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts | 9 +++++---- .../aws-events-targets/test/codebuild/codebuild.test.ts | 4 +++- .../test/codepipeline/integ.pipeline-event-target.ts | 2 +- .../test/codepipeline/pipeline.test.ts | 4 +++- .../test/ecs/event-rule-target.test.ts | 4 ++-- .../test/ecs/integ.event-ec2-task.lit.ts | 2 +- .../test/ecs/integ.event-fargate-task.ts | 2 +- .../aws-events-targets/test/lambda/integ.events.ts | 8 ++++++-- .../aws-events-targets/test/lambda/lambda.test.ts | 8 ++++++-- .../test/sns/integ.sns-event-rule-target.ts | 2 +- .../@aws-cdk/aws-events-targets/test/sns/sns.test.ts | 6 ++++-- .../test/sqs/integ.sqs-event-rule-target.ts | 2 +- .../@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts | 6 ++++-- .../test/stepfunctions/statemachine.test.ts | 2 +- packages/@aws-cdk/aws-events/lib/rule.ts | 2 +- packages/@aws-cdk/aws-events/lib/schedule.ts | 2 +- packages/@aws-cdk/aws-events/test/test.rule.ts | 2 +- 19 files changed, 45 insertions(+), 27 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts index ff239cf2b2f91..b9b851ab7144f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts @@ -106,7 +106,7 @@ export class ScheduledEc2Task extends cdk.Construct { // An EventRule that describes the event trigger (in this case a scheduled run) const eventRule = new events.Rule(this, 'ScheduledEventRule', { - scheduleExpression: props.scheduleExpression, + schedule: props.schedule, }); eventRule.addTarget(eventRuleTarget); } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts index 91e8777fce627..b8a91682509f6 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts @@ -1,5 +1,6 @@ import ec2 = require('@aws-cdk/aws-ec2'); import ecs = require('@aws-cdk/aws-ecs'); +import events = require('@aws-cdk/aws-events'); import cdk = require('@aws-cdk/cdk'); import { ScheduledEc2Task } from '../../lib'; @@ -26,7 +27,7 @@ class EventStack extends cdk.Stack { memoryLimitMiB: 512, cpu: 1, environment: { name: 'TRIGGER', value: 'CloudWatch Events' }, - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); /// !hide } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts index ce3827278080a..f2762b73e7437 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts @@ -1,6 +1,7 @@ import { expect, haveResource } from '@aws-cdk/assert'; import ec2 = require('@aws-cdk/aws-ec2'); import ecs = require('@aws-cdk/aws-ecs'); +import events = require('@aws-cdk/aws-events'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; import { ScheduledEc2Task } from '../../lib'; @@ -19,7 +20,7 @@ export = { cluster, image: ecs.ContainerImage.fromRegistry('henk'), memoryLimitMiB: 512, - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromExpression('rate(1 minute)') }); // THEN @@ -85,7 +86,7 @@ export = { memoryLimitMiB: 512, cpu: 2, environment: { name: 'TRIGGER', value: 'CloudWatch Events' }, - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromExpression('rate(1 minute)') }); // THEN @@ -159,7 +160,7 @@ export = { cluster, image: ecs.ContainerImage.fromRegistry('henk'), memoryReservationMiB: 512, - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromExpression('rate(1 minute)') }); // THEN @@ -208,7 +209,7 @@ export = { image: ecs.ContainerImage.fromRegistry('henk'), memoryReservationMiB: 512, command: ["-c", "4", "amazon.com"], - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromExpression('rate(1 minute)') }); // THEN diff --git a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts index adc6bade85faa..20ba5b4aac824 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts @@ -8,7 +8,9 @@ test('use codebuild project as an eventrule target', () => { // GIVEN const stack = new Stack(); const project = new codebuild.Project(stack, 'MyProject', { source: new codebuild.CodePipelineSource() }); - const rule = new events.Rule(stack, 'Rule', { scheduleExpression: 'rate(1 min)' }); + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.fromExpression('rate(1 min)') + }); // WHEN rule.addTarget(new targets.CodeBuildProject(project)); diff --git a/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts b/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts index 8ba9c871fccf5..c05f646c64843 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts @@ -43,7 +43,7 @@ pipeline.addStage({ }); new events.Rule(stack, 'rule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromExpression('rate(1 minute)'), targets: [new targets.CodePipeline(pipeline)] }); diff --git a/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts b/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts index 38d642de3dbe0..21865cd17c00d 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts @@ -31,7 +31,9 @@ test('use codebuild project as an eventrule target', () => { artifactBounds: { minInputs: 1, maxInputs: 1 , minOutputs: 1, maxOutputs: 1, }})] }); - const rule = new events.Rule(stack, 'rule', { scheduleExpression: 'rate(1 min)' }); + const rule = new events.Rule(stack, 'rule', { + schedule: events.Schedule.fromExpression('rate(1 minute)'), + }); // WHEN rule.addTarget(new targets.CodePipeline(pipeline)); diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index 108df76336b71..e4cc4d0c7ff5e 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -21,7 +21,7 @@ test("Can use EC2 taskdef as EventRule target", () => { }); const rule = new events.Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromExpression('rate(1 min)') }); // WHEN @@ -68,7 +68,7 @@ test("Can use Fargate taskdef as EventRule target", () => { }); const rule = new events.Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromExpression('rate(1 min)') }); // WHEN diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts index 9b5ee8fccfbff..e32eff3cfdb2a 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts @@ -32,7 +32,7 @@ class EventStack extends cdk.Stack { // An Rule that describes the event trigger (in this case a scheduled run) const rule = new events.Rule(this, 'Rule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); // Use EcsTask as the target of the Rule diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts index 8c286054620e1..527077cda6c5e 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -28,7 +28,7 @@ class EventStack extends cdk.Stack { // A rule that describes the event trigger (in this case a scheduled run) const rule = new events.Rule(this, 'Rule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); // Use EcsTask as the target of the Rule diff --git a/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts b/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts index 1cb1ba0c003ae..99b9f80cc80e7 100644 --- a/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts +++ b/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts @@ -13,10 +13,14 @@ const fn = new lambda.Function(stack, 'MyFunc', { code: lambda.Code.inline(`exports.handler = ${handler.toString()}`) }); -const timer = new events.Rule(stack, 'Timer', { scheduleExpression: 'rate(1 minute)' }); +const timer = new events.Rule(stack, 'Timer', { + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), +}); timer.addTarget(new targets.LambdaFunction(fn)); -const timer2 = new events.Rule(stack, 'Timer2', { scheduleExpression: 'rate(2 minutes)' }); +const timer2 = new events.Rule(stack, 'Timer2', { + schedule: events.Schedule.fromRate(2, events.TimeUnit.Minute), +}); timer2.addTarget(new targets.LambdaFunction(fn)); app.synth(); diff --git a/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts b/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts index edef10526eef6..8d86a1f07b501 100644 --- a/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts @@ -8,8 +8,12 @@ test('use lambda as an event rule target', () => { // GIVEN const stack = new cdk.Stack(); const fn = newTestLambda(stack); - const rule1 = new events.Rule(stack, 'Rule', { scheduleExpression: 'rate(1 minute)' }); - const rule2 = new events.Rule(stack, 'Rule2', { scheduleExpression: 'rate(5 minutes)' }); + const rule1 = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + }); + const rule2 = new events.Rule(stack, 'Rule2', { + schedule: events.Schedule.fromRate(5, events.TimeUnit.Minute), + }); // WHEN rule1.addTarget(new targets.LambdaFunction(fn)); diff --git a/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts index f5c8903779af5..af077dd876674 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts @@ -16,7 +16,7 @@ const stack = new cdk.Stack(app, 'aws-cdk-sns-event-target'); const topic = new sns.Topic(stack, 'MyTopic'); const event = new events.Rule(stack, 'EveryMinute', { - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); const queue = new sqs.Queue(stack, 'MyQueue'); diff --git a/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts b/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts index 868255b475e55..f31bc9f8fd73f 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts @@ -9,7 +9,7 @@ test('sns topic as an event rule target', () => { const stack = new Stack(); const topic = new sns.Topic(stack, 'MyTopic'); const rule = new events.Rule(stack, 'MyRule', { - scheduleExpression: 'rate(1 hour)', + schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), }); // WHEN @@ -51,7 +51,9 @@ test('multiple uses of a topic as a target results in a single policy statement' // WHEN for (let i = 0; i < 5; ++i) { - const rule = new events.Rule(stack, `Rule${i}`, { scheduleExpression: 'rate(1 hour)' }); + const rule = new events.Rule(stack, `Rule${i}`, { + schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + }); rule.addTarget(new targets.SnsTopic(topic)); } diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts index c45554ca0c51c..8a3ff5f6e5633 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -13,7 +13,7 @@ const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-cdk-sqs-event-target'); const event = new events.Rule(stack, 'MyRule', { - scheduleExpression: 'rate(1 minute)', + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); const queue = new sqs.Queue(stack, 'MyQueue'); diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts index 5d109a07df444..33ad67cf9e98a 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts @@ -9,7 +9,7 @@ test('sns topic as an event rule target', () => { const stack = new Stack(); const queue = new sqs.Queue(stack, 'MyQueue'); const rule = new events.Rule(stack, 'MyRule', { - scheduleExpression: 'rate(1 hour)', + schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), }); // WHEN @@ -74,7 +74,9 @@ test('multiple uses of a queue as a target results in multi policy statement bec // WHEN for (let i = 0; i < 2; ++i) { - const rule = new events.Rule(stack, `Rule${i}`, { scheduleExpression: 'rate(1 hour)' }); + const rule = new events.Rule(stack, `Rule${i}`, { + schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + }); rule.addTarget(new targets.SqsQueue(queue)); } diff --git a/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts b/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts index 5e29cd47b046c..0122bd42cc459 100644 --- a/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts @@ -8,7 +8,7 @@ test('State machine can be used as Event Rule target', () => { // GIVEN const stack = new cdk.Stack(); const rule = new events.Rule(stack, 'Rule', { - scheduleExpression: 'rate(1 minute)' + schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), }); const stateMachine = new sfn.StateMachine(stack, 'SM', { definition: new sfn.Wait(stack, 'Hello', { duration: sfn.WaitDuration.seconds(10) }) diff --git a/packages/@aws-cdk/aws-events/lib/rule.ts b/packages/@aws-cdk/aws-events/lib/rule.ts index af428b9929e75..ddfdd991c6e53 100644 --- a/packages/@aws-cdk/aws-events/lib/rule.ts +++ b/packages/@aws-cdk/aws-events/lib/rule.ts @@ -190,7 +190,7 @@ export class Rule extends Resource implements IRule { protected validate() { if (Object.keys(this.eventPattern).length === 0 && !this.scheduleExpression) { - return [ `Either 'eventPattern' or 'scheduleExpression' must be defined` ]; + return [ `Either 'eventPattern' or 'schedule' must be defined` ]; } return [ ]; diff --git a/packages/@aws-cdk/aws-events/lib/schedule.ts b/packages/@aws-cdk/aws-events/lib/schedule.ts index bf1007775fc42..00fce7d7360ae 100644 --- a/packages/@aws-cdk/aws-events/lib/schedule.ts +++ b/packages/@aws-cdk/aws-events/lib/schedule.ts @@ -14,7 +14,7 @@ export abstract class Schedule { /** * Construct a schedule from an interval and a time unit */ - public static fromRate(interval: number, unit: TimeUnit = TimeUnit.Hour): Schedule { + public static fromRate(interval: number, unit: TimeUnit): Schedule { const unitStr = interval !== 1 ? `${unit}s` : unit; return new LiteralSchedule(`rate(${interval} ${unitStr})`); diff --git a/packages/@aws-cdk/aws-events/test/test.rule.ts b/packages/@aws-cdk/aws-events/test/test.rule.ts index e0788c503abde..68ec37dd911e0 100644 --- a/packages/@aws-cdk/aws-events/test/test.rule.ts +++ b/packages/@aws-cdk/aws-events/test/test.rule.ts @@ -97,7 +97,7 @@ export = { const app = new cdk.App(); const stack = new cdk.Stack(app, 'MyStack'); new Rule(stack, 'Rule'); - test.throws(() => app.synth(), /Either 'eventPattern' or 'scheduleExpression' must be defined/); + test.throws(() => app.synth(), /Either 'eventPattern' or 'schedule' must be defined/); test.done(); }, From 521360c0fbb403c7c3956038c22c135f55faaacc Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Tue, 18 Jun 2019 11:08:39 +0200 Subject: [PATCH 5/5] Rename factory functions --- .../test/ec2/integ.scheduled-ecs-task.lit.ts | 2 +- .../test/ec2/test.scheduled-ecs-task.ts | 8 +++---- .../test/codebuild/codebuild.test.ts | 2 +- .../integ.pipeline-event-target.ts | 2 +- .../test/codepipeline/pipeline.test.ts | 2 +- .../test/ecs/event-rule-target.test.ts | 4 ++-- .../test/ecs/integ.event-ec2-task.lit.ts | 2 +- .../test/ecs/integ.event-fargate-task.ts | 2 +- .../test/lambda/integ.events.ts | 4 ++-- .../test/lambda/lambda.test.ts | 4 ++-- .../test/sns/integ.sns-event-rule-target.ts | 2 +- .../aws-events-targets/test/sns/sns.test.ts | 4 ++-- .../test/sqs/integ.sqs-event-rule-target.ts | 2 +- .../aws-events-targets/test/sqs/sqs.test.ts | 4 ++-- .../test/stepfunctions/statemachine.test.ts | 2 +- packages/@aws-cdk/aws-events/lib/rule.ts | 2 +- packages/@aws-cdk/aws-events/lib/schedule.ts | 24 +++++++++---------- .../@aws-cdk/aws-events/test/test.input.ts | 8 +++---- .../@aws-cdk/aws-events/test/test.rule.ts | 14 +++++------ .../@aws-cdk/aws-events/test/test.schedule.ts | 12 +++++----- 20 files changed, 53 insertions(+), 53 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts index b8a91682509f6..080e00e1919ee 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.ts @@ -27,7 +27,7 @@ class EventStack extends cdk.Stack { memoryLimitMiB: 512, cpu: 1, environment: { name: 'TRIGGER', value: 'CloudWatch Events' }, - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); /// !hide } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts index 5299a67907df7..d1c4ba987318c 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts @@ -20,7 +20,7 @@ export = { cluster, image: ecs.ContainerImage.fromRegistry('henk'), memoryLimitMiB: 512, - schedule: events.Schedule.fromExpression('rate(1 minute)') + schedule: events.Schedule.expression('rate(1 minute)') }); // THEN @@ -86,7 +86,7 @@ export = { memoryLimitMiB: 512, cpu: 2, environment: { name: 'TRIGGER', value: 'CloudWatch Events' }, - schedule: events.Schedule.fromExpression('rate(1 minute)') + schedule: events.Schedule.expression('rate(1 minute)') }); // THEN @@ -160,7 +160,7 @@ export = { cluster, image: ecs.ContainerImage.fromRegistry('henk'), memoryReservationMiB: 512, - schedule: events.Schedule.fromExpression('rate(1 minute)') + schedule: events.Schedule.expression('rate(1 minute)') }); // THEN @@ -209,7 +209,7 @@ export = { image: ecs.ContainerImage.fromRegistry('henk'), memoryReservationMiB: 512, command: ["-c", "4", "amazon.com"], - schedule: events.Schedule.fromExpression('rate(1 minute)') + schedule: events.Schedule.expression('rate(1 minute)') }); // THEN diff --git a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts index 418a0c9957303..c0c422f747021 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts @@ -9,7 +9,7 @@ test('use codebuild project as an eventrule target', () => { const stack = new Stack(); const project = new codebuild.PipelineProject(stack, 'MyProject'); const rule = new events.Rule(stack, 'Rule', { - schedule: events.Schedule.fromExpression('rate(1 min)') + schedule: events.Schedule.expression('rate(1 min)') }); // WHEN diff --git a/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts b/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts index b396227b99151..32d81c33dc046 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codepipeline/integ.pipeline-event-target.ts @@ -43,7 +43,7 @@ pipeline.addStage({ }); new events.Rule(stack, 'rule', { - schedule: events.Schedule.fromExpression('rate(1 minute)'), + schedule: events.Schedule.expression('rate(1 minute)'), targets: [new targets.CodePipeline(pipeline)] }); diff --git a/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts b/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts index caf72a5d83a4d..52126d994a8fe 100644 --- a/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/codepipeline/pipeline.test.ts @@ -32,7 +32,7 @@ test('use codebuild project as an eventrule target', () => { }); const rule = new events.Rule(stack, 'rule', { - schedule: events.Schedule.fromExpression('rate(1 minute)'), + schedule: events.Schedule.expression('rate(1 minute)'), }); // WHEN diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index 75ad82ea17fde..d5e08d6995d9e 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -21,7 +21,7 @@ test("Can use EC2 taskdef as EventRule target", () => { }); const rule = new events.Rule(stack, 'Rule', { - schedule: events.Schedule.fromExpression('rate(1 min)') + schedule: events.Schedule.expression('rate(1 min)') }); // WHEN @@ -69,7 +69,7 @@ test("Can use Fargate taskdef as EventRule target", () => { }); const rule = new events.Rule(stack, 'Rule', { - schedule: events.Schedule.fromExpression('rate(1 min)') + schedule: events.Schedule.expression('rate(1 min)') }); // WHEN diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts index e32eff3cfdb2a..2fa77dbe3d1e7 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.lit.ts @@ -32,7 +32,7 @@ class EventStack extends cdk.Stack { // An Rule that describes the event trigger (in this case a scheduled run) const rule = new events.Rule(this, 'Rule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); // Use EcsTask as the target of the Rule diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts index 527077cda6c5e..473e451e3911b 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -28,7 +28,7 @@ class EventStack extends cdk.Stack { // A rule that describes the event trigger (in this case a scheduled run) const rule = new events.Rule(this, 'Rule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); // Use EcsTask as the target of the Rule diff --git a/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts b/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts index 99b9f80cc80e7..44c0e12893f54 100644 --- a/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts +++ b/packages/@aws-cdk/aws-events-targets/test/lambda/integ.events.ts @@ -14,12 +14,12 @@ const fn = new lambda.Function(stack, 'MyFunc', { }); const timer = new events.Rule(stack, 'Timer', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); timer.addTarget(new targets.LambdaFunction(fn)); const timer2 = new events.Rule(stack, 'Timer2', { - schedule: events.Schedule.fromRate(2, events.TimeUnit.Minute), + schedule: events.Schedule.rate(2, events.TimeUnit.Minute), }); timer2.addTarget(new targets.LambdaFunction(fn)); diff --git a/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts b/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts index 8d86a1f07b501..3f1dec6280d5f 100644 --- a/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/lambda/lambda.test.ts @@ -9,10 +9,10 @@ test('use lambda as an event rule target', () => { const stack = new cdk.Stack(); const fn = newTestLambda(stack); const rule1 = new events.Rule(stack, 'Rule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); const rule2 = new events.Rule(stack, 'Rule2', { - schedule: events.Schedule.fromRate(5, events.TimeUnit.Minute), + schedule: events.Schedule.rate(5, events.TimeUnit.Minute), }); // WHEN diff --git a/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts index af077dd876674..244d7d9aec39d 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sns/integ.sns-event-rule-target.ts @@ -16,7 +16,7 @@ const stack = new cdk.Stack(app, 'aws-cdk-sns-event-target'); const topic = new sns.Topic(stack, 'MyTopic'); const event = new events.Rule(stack, 'EveryMinute', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); const queue = new sqs.Queue(stack, 'MyQueue'); diff --git a/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts b/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts index f31bc9f8fd73f..389cbe801b559 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sns/sns.test.ts @@ -9,7 +9,7 @@ test('sns topic as an event rule target', () => { const stack = new Stack(); const topic = new sns.Topic(stack, 'MyTopic'); const rule = new events.Rule(stack, 'MyRule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + schedule: events.Schedule.rate(1, events.TimeUnit.Hour), }); // WHEN @@ -52,7 +52,7 @@ test('multiple uses of a topic as a target results in a single policy statement' // WHEN for (let i = 0; i < 5; ++i) { const rule = new events.Rule(stack, `Rule${i}`, { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + schedule: events.Schedule.rate(1, events.TimeUnit.Hour), }); rule.addTarget(new targets.SnsTopic(topic)); } diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts index 8a3ff5f6e5633..6a0f14cd63a10 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/integ.sqs-event-rule-target.ts @@ -13,7 +13,7 @@ const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-cdk-sqs-event-target'); const event = new events.Rule(stack, 'MyRule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); const queue = new sqs.Queue(stack, 'MyQueue'); diff --git a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts index 33ad67cf9e98a..388cf21e60af9 100644 --- a/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts @@ -9,7 +9,7 @@ test('sns topic as an event rule target', () => { const stack = new Stack(); const queue = new sqs.Queue(stack, 'MyQueue'); const rule = new events.Rule(stack, 'MyRule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + schedule: events.Schedule.rate(1, events.TimeUnit.Hour), }); // WHEN @@ -75,7 +75,7 @@ test('multiple uses of a queue as a target results in multi policy statement bec // WHEN for (let i = 0; i < 2; ++i) { const rule = new events.Rule(stack, `Rule${i}`, { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Hour), + schedule: events.Schedule.rate(1, events.TimeUnit.Hour), }); rule.addTarget(new targets.SqsQueue(queue)); } diff --git a/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts b/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts index 0122bd42cc459..902efb7ee5ed3 100644 --- a/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/stepfunctions/statemachine.test.ts @@ -8,7 +8,7 @@ test('State machine can be used as Event Rule target', () => { // GIVEN const stack = new cdk.Stack(); const rule = new events.Rule(stack, 'Rule', { - schedule: events.Schedule.fromRate(1, events.TimeUnit.Minute), + schedule: events.Schedule.rate(1, events.TimeUnit.Minute), }); const stateMachine = new sfn.StateMachine(stack, 'SM', { definition: new sfn.Wait(stack, 'Hello', { duration: sfn.WaitDuration.seconds(10) }) diff --git a/packages/@aws-cdk/aws-events/lib/rule.ts b/packages/@aws-cdk/aws-events/lib/rule.ts index 36dc7117d17a7..8e2b5ef7c6665 100644 --- a/packages/@aws-cdk/aws-events/lib/rule.ts +++ b/packages/@aws-cdk/aws-events/lib/rule.ts @@ -105,7 +105,7 @@ export class Rule extends Resource implements IRule { this.ruleArn = resource.attrArn; this.addEventPattern(props.eventPattern); - this.scheduleExpression = props.schedule && props.schedule.expression; + this.scheduleExpression = props.schedule && props.schedule.expressionString; for (const target of props.targets || []) { this.addTarget(target); diff --git a/packages/@aws-cdk/aws-events/lib/schedule.ts b/packages/@aws-cdk/aws-events/lib/schedule.ts index 00fce7d7360ae..766c3a0bd91ec 100644 --- a/packages/@aws-cdk/aws-events/lib/schedule.ts +++ b/packages/@aws-cdk/aws-events/lib/schedule.ts @@ -7,14 +7,14 @@ export abstract class Schedule { * * @param expression The expression to use. Must be in a format that Cloudwatch Events will recognize */ - public static fromExpression(expression: string): Schedule { + public static expression(expression: string): Schedule { return new LiteralSchedule(expression); } /** * Construct a schedule from an interval and a time unit */ - public static fromRate(interval: number, unit: TimeUnit): Schedule { + public static rate(interval: number, unit: TimeUnit): Schedule { const unitStr = interval !== 1 ? `${unit}s` : unit; return new LiteralSchedule(`rate(${interval} ${unitStr})`); @@ -23,19 +23,19 @@ export abstract class Schedule { /** * Create a schedule from a set of cron fields */ - public static fromCron(options: CronOptions): Schedule { + public static cron(options: CronOptions): Schedule { if (options.weekDay !== undefined && options.day !== undefined) { throw new Error(`Cannot supply both 'day' and 'weekDay', use at most one`); } - const minute = ifUndef(options.minute, '*'); - const hour = ifUndef(options.hour, '*'); - const month = ifUndef(options.month, '*'); - const year = ifUndef(options.year, '*'); + const minute = fallback(options.minute, '*'); + const hour = fallback(options.hour, '*'); + const month = fallback(options.month, '*'); + const year = fallback(options.year, '*'); // Weekday defaults to '?' if not supplied. If it is supplied, day must become '?' - const day = ifUndef(options.day, options.weekDay !== undefined ? '?' : '*'); - const weekDay = ifUndef(options.weekDay, '?'); + const day = fallback(options.day, options.weekDay !== undefined ? '?' : '*'); + const weekDay = fallback(options.weekDay, '?'); return new LiteralSchedule(`cron(${minute} ${hour} ${day} ${month} ${weekDay} ${year})`); } @@ -43,7 +43,7 @@ export abstract class Schedule { /** * Retrieve the expression for this schedule */ - public abstract readonly expression: string; + public abstract readonly expressionString: string; protected constructor() { } @@ -122,11 +122,11 @@ export interface CronOptions { } class LiteralSchedule extends Schedule { - constructor(public readonly expression: string) { + constructor(public readonly expressionString: string) { super(); } } -function ifUndef(x: T | undefined, def: T): T { +function fallback(x: T | undefined, def: T): T { return x === undefined ? def : x; } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events/test/test.input.ts b/packages/@aws-cdk/aws-events/test/test.input.ts index 980c11a7d1abb..e5034d6e6fe47 100644 --- a/packages/@aws-cdk/aws-events/test/test.input.ts +++ b/packages/@aws-cdk/aws-events/test/test.input.ts @@ -11,7 +11,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); // WHEN @@ -34,7 +34,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); // WHEN @@ -56,7 +56,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); // WHEN @@ -78,7 +78,7 @@ export = { // GIVEN const stack = new Stack(); const rule = new Rule(stack, 'Rule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); const world = cdk.Lazy.stringValue({ produce: () => 'world' }); diff --git a/packages/@aws-cdk/aws-events/test/test.rule.ts b/packages/@aws-cdk/aws-events/test/test.rule.ts index 68ec37dd911e0..184ad3cb64479 100644 --- a/packages/@aws-cdk/aws-events/test/test.rule.ts +++ b/packages/@aws-cdk/aws-events/test/test.rule.ts @@ -14,7 +14,7 @@ export = { const stack = new cdk.Stack(); new Rule(stack, 'MyRule', { - schedule: Schedule.fromRate(10, TimeUnit.Minute), + schedule: Schedule.rate(10, TimeUnit.Minute), }); expect(stack).toMatch({ @@ -38,7 +38,7 @@ export = { // WHEN new Rule(stack, 'MyRule', { ruleName: 'PhysicalName', - schedule: Schedule.fromRate(10, TimeUnit.Minute), + schedule: Schedule.rate(10, TimeUnit.Minute), }); // THEN @@ -174,7 +174,7 @@ export = { const rule = new Rule(stack, 'EventRule', { targets: [ t1 ], - schedule: Schedule.fromRate(5, TimeUnit.Minute), + schedule: Schedule.rate(5, TimeUnit.Minute), }); rule.addTarget(t2); @@ -216,7 +216,7 @@ export = { const stack = new cdk.Stack(); const rule = new Rule(stack, 'EventRule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); // a plain string should just be stringified (i.e. double quotes added and escaped) @@ -301,7 +301,7 @@ export = { const stack = new cdk.Stack(); const rule = new Rule(stack, 'EventRule', { - schedule: Schedule.fromRate(1, TimeUnit.Minute), + schedule: Schedule.rate(1, TimeUnit.Minute), }); const role = new iam.Role(stack, 'SomeRole', { @@ -376,7 +376,7 @@ export = { // WHEN new Rule(stack, 'Rule', { - schedule: Schedule.fromExpression('foom'), + schedule: Schedule.expression('foom'), enabled: false }); @@ -392,7 +392,7 @@ export = { // GIVEN const stack = new cdk.Stack(); const rule = new Rule(stack, 'Rule', { - schedule: Schedule.fromExpression('foom'), + schedule: Schedule.expression('foom'), enabled: false }); rule.addTarget(new SomeTarget()); diff --git a/packages/@aws-cdk/aws-events/test/test.schedule.ts b/packages/@aws-cdk/aws-events/test/test.schedule.ts index 06715c2261cd2..bb3bfabfde834 100644 --- a/packages/@aws-cdk/aws-events/test/test.schedule.ts +++ b/packages/@aws-cdk/aws-events/test/test.schedule.ts @@ -4,29 +4,29 @@ import events = require('../lib'); export = { 'cron expressions day and dow are mutex: given weekday'(test: Test) { // Run every 10 minutes Monday through Friday - test.equal('cron(0/10 * ? * MON-FRI *)', events.Schedule.fromCron({ + test.equal('cron(0/10 * ? * MON-FRI *)', events.Schedule.cron({ minute: '0/10', weekDay: 'MON-FRI' - }).expression); + }).expressionString); test.done(); }, 'cron expressions day and dow are mutex: given month day'(test: Test) { // Run at 8:00 am (UTC) every 1st day of the month - test.equal('cron(0 8 1 * ? *)', events.Schedule.fromCron({ + test.equal('cron(0 8 1 * ? *)', events.Schedule.cron({ minute: '0', hour: '8', day: '1', - }).expression); + }).expressionString); test.done(); }, 'cron expressions day and dow are mutex: given neither'(test: Test) { // Run at 10:00 am (UTC) every day - test.equal('cron(0 10 * * ? *)', events.Schedule.fromCron({ + test.equal('cron(0 10 * * ? *)', events.Schedule.cron({ minute: '0', hour: '10', - }).expression); + }).expressionString); test.done(); }, };