forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scheduler): ScheduleTargetInput (aws#25663)
This PR contains implementation of ScheduleTargetInput. While a schedule is the main resource in Amazon EventBridge Scheduler, this PR adds ScheduleTargetInput on which ScheduleTargetBase depends. Every Schedule has a target that determines what extra information is sent to the target when the schedule is triggered. Also 4 ContextAttributes can be used that will be resolved at trigger-time. To be able to create sensible unit tests, also the a start is made to add the `Schedule` and the `LambdaInvoke` target as described in the RFC. Implementation is based on RFC: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md Also added a small fix to 2 of the unit tests of the previous PR for this module. Advances aws#23394 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
9 changed files
with
425 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './schedule-expression'; | ||
export * from './schedule-expression'; | ||
export * from './input'; | ||
export * from './schedule'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { DefaultTokenResolver, IResolveContext, Stack, StringConcat, Token, Tokenization } from 'aws-cdk-lib'; | ||
import { ISchedule } from './schedule'; | ||
|
||
/** | ||
* The text, or well-formed JSON, passed to the target of the schedule. | ||
*/ | ||
export abstract class ScheduleTargetInput { | ||
/** | ||
* Pass text to the target, it is possible to embed `ContextAttributes` | ||
* that will be resolved to actual values while the CloudFormation is | ||
* deployed or cdk Tokens that will be resolved when the CloudFormation | ||
* templates are generated by CDK. | ||
* | ||
* The target input value will be a single string that you pass. | ||
* For passing complex values like JSON object to a target use method | ||
* `ScheduleTargetInput.fromObject()` instead. | ||
* | ||
* @param text Text to use as the input for the target | ||
*/ | ||
public static fromText(text: string): ScheduleTargetInput { | ||
return new FieldAwareEventInput(text); | ||
} | ||
|
||
/** | ||
* Pass a JSON object to the target, it is possible to embed `ContextAttributes` and other | ||
* cdk references. | ||
* | ||
* @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); | ||
} | ||
|
||
protected constructor() { | ||
} | ||
|
||
/** | ||
* Return the input properties for this input object | ||
*/ | ||
public abstract bind(schedule: ISchedule): string; | ||
} | ||
|
||
class FieldAwareEventInput extends ScheduleTargetInput { | ||
constructor(private readonly input: any) { | ||
super(); | ||
} | ||
|
||
public bind(schedule: ISchedule): string { | ||
class Replacer extends DefaultTokenResolver { | ||
constructor() { | ||
super(new StringConcat()); | ||
} | ||
|
||
public resolveToken(t: Token, _context: IResolveContext) { | ||
return Token.asString(t); | ||
} | ||
} | ||
|
||
const stack = Stack.of(schedule); | ||
return stack.toJsonString(Tokenization.resolve(this.input, { | ||
scope: schedule, | ||
resolver: new Replacer(), | ||
})); | ||
} | ||
} | ||
|
||
/** | ||
* Represents a field in the event pattern | ||
* | ||
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-schedule-context-attributes.html | ||
*/ | ||
export class ContextAttribute { | ||
/** | ||
* The ARN of the schedule. | ||
*/ | ||
public static get scheduleArn(): string { | ||
return this.fromName('schedule-arn'); | ||
} | ||
|
||
/** | ||
* The time you specified for the schedule to invoke its target, for example, | ||
* 2022-03-22T18:59:43Z. | ||
*/ | ||
public static get scheduledTime(): string { | ||
return this.fromName('scheduled-time'); | ||
} | ||
|
||
/** | ||
* The unique ID that EventBridge Scheduler assigns for each attempted invocation of | ||
* a target, for example, d32c5kddcf5bb8c3. | ||
*/ | ||
public static get executionId(): string { | ||
return this.fromName('execution-id'); | ||
} | ||
|
||
/** | ||
* A counter that identifies the attempt number for the current invocation, for | ||
* example, 1. | ||
*/ | ||
public static get attemptNumber(): string { | ||
return this.fromName('attempt-number'); | ||
} | ||
|
||
/** | ||
* Escape hatch for other ContextAttribute that might be resolved in future. | ||
* | ||
* @param name - name will replace xxx in <aws.scheduler.xxx> | ||
*/ | ||
public static fromName(name: string): string { | ||
return new ContextAttribute(name).toString(); | ||
} | ||
|
||
private constructor(public readonly name: string) { | ||
} | ||
|
||
/** | ||
* Convert the path to the field in the event pattern to JSON | ||
*/ | ||
public toString() { | ||
return `<aws.scheduler.${this.name}>`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './schedule'; | ||
export * from './targets'; |
57 changes: 57 additions & 0 deletions
57
packages/@aws-cdk/aws-scheduler-alpha/lib/private/schedule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Resource } from 'aws-cdk-lib'; | ||
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler'; | ||
import { Construct } from 'constructs'; | ||
import { ISchedule } from '../schedule'; | ||
import { ScheduleExpression } from '../schedule-expression'; | ||
|
||
/** | ||
* DISCLAIMER: WORK IN PROGRESS, INTERFACE MIGHT CHANGE | ||
* | ||
* This unit is not yet finished. Only rudimentary Schedule is implemented in order | ||
* to be able to create some sensible unit tests | ||
*/ | ||
|
||
export interface IScheduleTarget { | ||
bind(_schedule: ISchedule): CfnSchedule.TargetProperty; | ||
} | ||
|
||
/** | ||
* Construction properties for `Schedule`. | ||
*/ | ||
export interface ScheduleProps { | ||
/** | ||
* The expression that defines when the schedule runs. Can be either a `at`, `rate` | ||
* or `cron` expression. | ||
*/ | ||
readonly schedule: ScheduleExpression; | ||
|
||
/** | ||
* The schedule's target details. | ||
*/ | ||
readonly target: IScheduleTarget; | ||
|
||
/** | ||
* The description you specify for the schedule. | ||
* | ||
* @default - no value | ||
*/ | ||
readonly description?: string; | ||
} | ||
|
||
/** | ||
* An EventBridge Schedule | ||
*/ | ||
export class Schedule extends Resource implements ISchedule { | ||
constructor(scope: Construct, id: string, props: ScheduleProps) { | ||
super(scope, id); | ||
|
||
new CfnSchedule(this, 'Resource', { | ||
flexibleTimeWindow: { mode: 'OFF' }, | ||
scheduleExpression: props.schedule.expressionString, | ||
scheduleExpressionTimezone: props.schedule.timeZone?.timezoneName, | ||
target: { | ||
...props.target.bind(this), | ||
}, | ||
}); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/@aws-cdk/aws-scheduler-alpha/lib/private/targets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as iam from 'aws-cdk-lib/aws-iam'; | ||
import * as lambda from 'aws-cdk-lib/aws-lambda'; | ||
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler'; | ||
import { ScheduleTargetInput } from '../input'; | ||
import { ISchedule } from '../schedule'; | ||
|
||
/** | ||
* DISCLAIMER: WORK IN PROGRESS, INTERFACE MIGHT CHANGE | ||
* | ||
* This unit is not yet finished. The LambaInvoke target is only implemented to be able | ||
* to create some sensible unit tests. | ||
*/ | ||
|
||
export namespace targets { | ||
export interface ScheduleTargetBaseProps { | ||
readonly role?: iam.IRole; | ||
readonly input?: ScheduleTargetInput; | ||
} | ||
|
||
abstract class ScheduleTargetBase { | ||
constructor( | ||
private readonly baseProps: ScheduleTargetBaseProps, | ||
protected readonly targetArn: string, | ||
) { | ||
} | ||
|
||
protected abstract addTargetActionToRole(role: iam.IRole): void; | ||
|
||
protected bindBaseTargetConfig(_schedule: ISchedule): CfnSchedule.TargetProperty { | ||
if (typeof this.baseProps.role === undefined) { | ||
throw Error('A role is needed (for now)'); | ||
} | ||
this.addTargetActionToRole(this.baseProps.role!); | ||
return { | ||
arn: this.targetArn, | ||
roleArn: this.baseProps.role!.roleArn, | ||
input: this.baseProps.input?.bind(_schedule), | ||
}; | ||
} | ||
|
||
bind(schedule: ISchedule): CfnSchedule.TargetProperty { | ||
return this.bindBaseTargetConfig(schedule); | ||
} | ||
} | ||
|
||
export class LambdaInvoke extends ScheduleTargetBase { | ||
constructor( | ||
baseProps: ScheduleTargetBaseProps, | ||
private readonly func: lambda.IFunction, | ||
) { | ||
super(baseProps, func.functionArn); | ||
} | ||
|
||
protected addTargetActionToRole(role: iam.IRole): void { | ||
this.func.grantInvoke(role); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IResource } from 'aws-cdk-lib'; | ||
|
||
/** | ||
* Interface representing a created or an imported `Schedule`. | ||
*/ | ||
export interface ISchedule extends IResource { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.