Skip to content

Commit ea10f0d

Browse files
jogoldElad Ben-Israel
authored andcommitted
feat(events): make target optional in onXxx() methods (#2921)
Make `target` optional in `OnEventOptions` so that it is no required when calling a `onXxx()` method. This allows to use "preconfigured" rules in other constructs. Update `awslint:events-method-signature` to enforce optional `options` parameter in `onXxx()` method signatures. Closes #2913
1 parent 661d28c commit ea10f0d

File tree

12 files changed

+107
-32
lines changed

12 files changed

+107
-32
lines changed

packages/@aws-cdk/aws-cloudtrail/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class Trail extends Resource {
234234
*
235235
* Be sure to filter the event further down using an event pattern.
236236
*/
237-
public onCloudTrailEvent(id: string, options: events.OnEventOptions): events.Rule {
237+
public onCloudTrailEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
238238
const rule = new events.Rule(this, id, options);
239239
rule.addTarget(options.target);
240240
rule.addEventPattern({

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ abstract class ProjectBase extends Resource implements IProject {
202202
*
203203
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
204204
*/
205-
public onEvent(id: string, options: events.OnEventOptions): events.Rule {
205+
public onEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
206206
const rule = new events.Rule(this, id, options);
207207
rule.addTarget(options.target);
208208
rule.addEventPattern({
@@ -239,7 +239,7 @@ abstract class ProjectBase extends Resource implements IProject {
239239
*
240240
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
241241
*/
242-
public onStateChange(id: string, options: events.OnEventOptions) {
242+
public onStateChange(id: string, options: events.OnEventOptions = {}) {
243243
const rule = this.onEvent(id, options);
244244
rule.addEventPattern({
245245
detailType: ['CodeBuild Build State Change'],
@@ -253,7 +253,7 @@ abstract class ProjectBase extends Resource implements IProject {
253253
*
254254
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
255255
*/
256-
public onPhaseChange(id: string, options: events.OnEventOptions) {
256+
public onPhaseChange(id: string, options: events.OnEventOptions = {}) {
257257
const rule = this.onEvent(id, options);
258258
rule.addEventPattern({
259259
detailType: ['CodeBuild Build Phase Change'],
@@ -267,7 +267,7 @@ abstract class ProjectBase extends Resource implements IProject {
267267
* To access fields from the event in the event target input,
268268
* use the static fields on the `StateChangeEvent` class.
269269
*/
270-
public onBuildStarted(id: string, options: events.OnEventOptions) {
270+
public onBuildStarted(id: string, options: events.OnEventOptions = {}) {
271271
const rule = this.onStateChange(id, options);
272272
rule.addEventPattern({
273273
detail: {
@@ -283,7 +283,7 @@ abstract class ProjectBase extends Resource implements IProject {
283283
* To access fields from the event in the event target input,
284284
* use the static fields on the `StateChangeEvent` class.
285285
*/
286-
public onBuildFailed(id: string, options: events.OnEventOptions) {
286+
public onBuildFailed(id: string, options: events.OnEventOptions = {}) {
287287
const rule = this.onStateChange(id, options);
288288
rule.addEventPattern({
289289
detail: {
@@ -299,7 +299,7 @@ abstract class ProjectBase extends Resource implements IProject {
299299
* To access fields from the event in the event target input,
300300
* use the static fields on the `StateChangeEvent` class.
301301
*/
302-
public onBuildSucceeded(id: string, options: events.OnEventOptions) {
302+
public onBuildSucceeded(id: string, options: events.OnEventOptions = {}) {
303303
const rule = this.onStateChange(id, options);
304304
rule.addEventPattern({
305305
detail: {

packages/@aws-cdk/aws-codecommit/lib/repository.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
117117
* Defines a CloudWatch event rule which triggers for repository events. Use
118118
* `rule.addEventPattern(pattern)` to specify a filter.
119119
*/
120-
public onEvent(id: string, options: events.OnEventOptions) {
120+
public onEvent(id: string, options: events.OnEventOptions = {}) {
121121
const rule = new events.Rule(this, id, options);
122122
rule.addEventPattern({
123123
source: [ 'aws.codecommit' ],
@@ -131,7 +131,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
131131
* Defines a CloudWatch event rule which triggers when a "CodeCommit
132132
* Repository State Change" event occurs.
133133
*/
134-
public onStateChange(id: string, options: events.OnEventOptions) {
134+
public onStateChange(id: string, options: events.OnEventOptions = {}) {
135135
const rule = this.onEvent(id, options);
136136
rule.addEventPattern({
137137
detailType: [ 'CodeCommit Repository State Change' ],
@@ -143,7 +143,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
143143
* Defines a CloudWatch event rule which triggers when a reference is
144144
* created (i.e. a new branch/tag is created) to the repository.
145145
*/
146-
public onReferenceCreated(id: string, options: events.OnEventOptions) {
146+
public onReferenceCreated(id: string, options: events.OnEventOptions = {}) {
147147
const rule = this.onStateChange(id, options);
148148
rule.addEventPattern({ detail: { event: [ 'referenceCreated' ] } });
149149
return rule;
@@ -153,7 +153,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
153153
* Defines a CloudWatch event rule which triggers when a reference is
154154
* updated (i.e. a commit is pushed to an existing or new branch) from the repository.
155155
*/
156-
public onReferenceUpdated(id: string, options: events.OnEventOptions) {
156+
public onReferenceUpdated(id: string, options: events.OnEventOptions = {}) {
157157
const rule = this.onStateChange(id, options);
158158
rule.addEventPattern({ detail: { event: [ 'referenceCreated', 'referenceUpdated' ] } });
159159
return rule;
@@ -163,7 +163,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
163163
* Defines a CloudWatch event rule which triggers when a reference is
164164
* delete (i.e. a branch/tag is deleted) from the repository.
165165
*/
166-
public onReferenceDeleted(id: string, options: events.OnEventOptions) {
166+
public onReferenceDeleted(id: string, options: events.OnEventOptions = {}) {
167167
const rule = this.onStateChange(id, options);
168168
rule.addEventPattern({ detail: { event: [ 'referenceDeleted' ] } });
169169
return rule;
@@ -172,7 +172,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
172172
/**
173173
* Defines a CloudWatch event rule which triggers when a pull request state is changed.
174174
*/
175-
public onPullRequestStateChange(id: string, options: events.OnEventOptions) {
175+
public onPullRequestStateChange(id: string, options: events.OnEventOptions = {}) {
176176
const rule = this.onEvent(id, options);
177177
rule.addEventPattern({ detailType: [ 'CodeCommit Pull Request State Change' ] });
178178
return rule;
@@ -181,7 +181,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
181181
/**
182182
* Defines a CloudWatch event rule which triggers when a comment is made on a pull request.
183183
*/
184-
public onCommentOnPullRequest(id: string, options: events.OnEventOptions) {
184+
public onCommentOnPullRequest(id: string, options: events.OnEventOptions = {}) {
185185
const rule = this.onEvent(id, options);
186186
rule.addEventPattern({ detailType: [ 'CodeCommit Comment on Pull Request' ] });
187187
return rule;
@@ -190,7 +190,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
190190
/**
191191
* Defines a CloudWatch event rule which triggers when a comment is made on a commit.
192192
*/
193-
public onCommentOnCommit(id: string, options: events.OnEventOptions) {
193+
public onCommentOnCommit(id: string, options: events.OnEventOptions = {}) {
194194
const rule = this.onEvent(id, options);
195195
rule.addEventPattern({ detailType: [ 'CodeCommit Comment on Commit' ] });
196196
return rule;
@@ -199,7 +199,7 @@ abstract class RepositoryBase extends Resource implements IRepository {
199199
/**
200200
* Defines a CloudWatch event rule which triggers when a commit is pushed to a branch.
201201
*/
202-
public onCommit(id: string, options: OnCommitOptions) {
202+
public onCommit(id: string, options: OnCommitOptions = {}) {
203203
const rule = this.onReferenceUpdated(id, options);
204204
if (options.branches) {
205205
rule.addEventPattern({ detail: { referenceName: options.branches }});

packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ abstract class PipelineBase extends Resource implements IPipeline {
114114
* @param id Identifier for this event handler.
115115
* @param options Additional options to pass to the event rule.
116116
*/
117-
public onEvent(id: string, options: events.OnEventOptions): events.Rule {
117+
public onEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
118118
const rule = new events.Rule(this, id, options);
119119
rule.addTarget(options.target);
120120
rule.addEventPattern({
@@ -131,7 +131,7 @@ abstract class PipelineBase extends Resource implements IPipeline {
131131
* @param id Identifier for this event handler.
132132
* @param options Additional options to pass to the event rule.
133133
*/
134-
public onStateChange(id: string, options: events.OnEventOptions): events.Rule {
134+
public onStateChange(id: string, options: events.OnEventOptions = {}): events.Rule {
135135
const rule = this.onEvent(id, options);
136136
rule.addEventPattern({
137137
detailType: [ 'CodePipeline Pipeline Execution State Change' ],

packages/@aws-cdk/aws-config/lib/rule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ abstract class RuleBase extends Resource implements IRule {
6565
* Defines a CloudWatch event rule which triggers for rule events. Use
6666
* `rule.addEventPattern(pattern)` to specify a filter.
6767
*/
68-
public onEvent(id: string, options: events.OnEventOptions) {
68+
public onEvent(id: string, options: events.OnEventOptions = {}) {
6969
const rule = new events.Rule(this, id, options);
7070
rule.addEventPattern({
7171
source: ['aws.config'],
@@ -80,7 +80,7 @@ abstract class RuleBase extends Resource implements IRule {
8080
/**
8181
* Defines a CloudWatch event rule which triggers for rule compliance events.
8282
*/
83-
public onComplianceChange(id: string, options: events.OnEventOptions): events.Rule {
83+
public onComplianceChange(id: string, options: events.OnEventOptions = {}): events.Rule {
8484
const rule = this.onEvent(id, options);
8585
rule.addEventPattern({
8686
detailType: [ 'Config Rules Compliance Change' ],
@@ -91,7 +91,7 @@ abstract class RuleBase extends Resource implements IRule {
9191
/**
9292
* Defines a CloudWatch event rule which triggers for rule re-evaluation status events.
9393
*/
94-
public onReEvaluationStatus(id: string, options: events.OnEventOptions): events.Rule {
94+
public onReEvaluationStatus(id: string, options: events.OnEventOptions = {}): events.Rule {
9595
const rule = this.onEvent(id, options);
9696
rule.addEventPattern({
9797
detailType: [ 'Config Rules Re-evaluation Status' ],

packages/@aws-cdk/aws-ecr/lib/repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export abstract class RepositoryBase extends Resource implements IRepository {
133133
* @param id The id of the rule
134134
* @param options Options for adding the rule
135135
*/
136-
public onCloudTrailEvent(id: string, options: events.OnEventOptions): events.Rule {
136+
public onCloudTrailEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
137137
const rule = new events.Rule(this, id, options);
138138
rule.addTarget(options.target);
139139
rule.addEventPattern({
@@ -158,7 +158,7 @@ export abstract class RepositoryBase extends Resource implements IRepository {
158158
* @param id The id of the rule
159159
* @param options Options for adding the rule
160160
*/
161-
public onCloudTrailImagePushed(id: string, options: OnCloudTrailImagePushedOptions): events.Rule {
161+
public onCloudTrailImagePushed(id: string, options: OnCloudTrailImagePushedOptions = {}): events.Rule {
162162
const rule = this.onCloudTrailEvent(id, options);
163163
rule.addEventPattern({
164164
detail: {

packages/@aws-cdk/aws-events/lib/on-event-options.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import { IRuleTarget } from "./target";
77
export interface OnEventOptions {
88
/**
99
* The target to register for the event
10+
*
11+
* @default - No target is added to the rule. Use `addTarget()` to add a target.
1012
*/
11-
readonly target: IRuleTarget;
13+
readonly target?: IRuleTarget;
1214

1315
/**
1416
* A description of the rule's purpose.
17+
*
18+
* @default - No description
1519
*/
1620
readonly description?: string;
1721

@@ -29,8 +33,10 @@ export interface OnEventOptions {
2933
* filtering. The filtering implied by what you pass here is added
3034
* on top of that filtering.
3135
*
36+
* @default - No additional filtering based on an event pattern.
37+
*
3238
* @see
3339
* http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CloudWatchEventsandEventPatterns.html
3440
*/
3541
readonly eventPattern?: EventPattern;
36-
}
42+
}

packages/@aws-cdk/aws-rds/lib/instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
116116
* Defines a CloudWatch event rule which triggers for instance events. Use
117117
* `rule.addEventPattern(pattern)` to specify a filter.
118118
*/
119-
public onEvent(id: string, options: events.OnEventOptions) {
119+
public onEvent(id: string, options: events.OnEventOptions = {}) {
120120
const rule = new events.Rule(this, id, options);
121121
rule.addEventPattern({
122122
source: ['aws.rds'],

packages/@aws-cdk/aws-rds/test/test.instance.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,68 @@ export = {
313313
// WHEN
314314
instance.onEvent('InstanceEvent', { target: new targets.LambdaFunction(fn) });
315315

316+
// THEN
317+
expect(stack).to(haveResource('AWS::Events::Rule', {
318+
EventPattern: {
319+
source: [
320+
'aws.rds'
321+
],
322+
resources: [
323+
{
324+
'Fn::Join': [
325+
'',
326+
[
327+
'arn:',
328+
{
329+
Ref: 'AWS::Partition'
330+
},
331+
':rds:',
332+
{
333+
Ref: 'AWS::Region'
334+
},
335+
':',
336+
{
337+
Ref: 'AWS::AccountId'
338+
},
339+
':db:',
340+
{
341+
Ref: 'InstanceC1063A87'
342+
}
343+
]
344+
]
345+
}
346+
]
347+
},
348+
Targets: [
349+
{
350+
Arn: {
351+
'Fn::GetAtt': [
352+
'Function76856677',
353+
'Arn'
354+
],
355+
},
356+
Id: 'Function'
357+
}
358+
]
359+
}));
360+
361+
test.done();
362+
},
363+
364+
'on event without target'(test: Test) {
365+
// GIVEN
366+
const stack = new cdk.Stack();
367+
const vpc = new ec2.Vpc(stack, 'VPC');
368+
const instance = new rds.DatabaseInstance(stack, 'Instance', {
369+
engine: rds.DatabaseInstanceEngine.Mysql,
370+
instanceClass: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Small),
371+
masterUsername: 'admin',
372+
vpc
373+
});
374+
375+
// WHEN
376+
instance.onEvent('InstanceEvent');
377+
316378
// THEN
317379
expect(stack).to(haveResource('AWS::Events::Rule', {
318380
EventPattern: {

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ abstract class BucketBase extends Resource implements IBucket {
301301
* @param id The id of the rule
302302
* @param options Options for adding the rule
303303
*/
304-
public onCloudTrailEvent(id: string, options: OnCloudTrailBucketEventOptions): events.Rule {
304+
public onCloudTrailEvent(id: string, options: OnCloudTrailBucketEventOptions = {}): events.Rule {
305305
const rule = new events.Rule(this, id, options);
306306
rule.addTarget(options.target);
307307
rule.addEventPattern({
@@ -326,7 +326,7 @@ abstract class BucketBase extends Resource implements IBucket {
326326
* @param id The id of the rule
327327
* @param options Options for adding the rule
328328
*/
329-
public onCloudTrailPutObject(id: string, options: OnCloudTrailBucketEventOptions): events.Rule {
329+
public onCloudTrailPutObject(id: string, options: OnCloudTrailBucketEventOptions = {}): events.Rule {
330330
const rule = this.onCloudTrailEvent(id, options);
331331
rule.addEventPattern({
332332
detail: {

0 commit comments

Comments
 (0)