-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(scheduler): ScheduleTargetInput #25663
Merged
mergify
merged 15 commits into
aws:main
from
Jacco:feat(scheduler)--ScheduleTargetInput
Jun 29, 2023
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
570f42c
implementation of ScheduleInputTarget
Jacco 689aa8c
Merge branch 'main' into feat(scheduler)--ScheduleTargetInput
kaizencc d9dc10c
Update packages/@aws-cdk/aws-scheduler-alpha/lib/input.ts
Jacco 8af3db1
Update packages/@aws-cdk/aws-scheduler-alpha/test/input.test.ts
Jacco 54d6e0f
Update packages/@aws-cdk/aws-scheduler-alpha/lib/input.ts
Jacco 9c2f8d4
moved stuff to private
Jacco b7a6ba7
Update packages/@aws-cdk/aws-scheduler-alpha/test/input.test.ts
Jacco e363170
readme fix
Jacco 3e3d380
Merge branch 'feat(scheduler)--ScheduleTargetInput' of github.com:Jac…
Jacco 81793ba
back to txt
Jacco d7e4efe
space
Jacco b74d0ad
rosetta adjust
Jacco 0fb91a2
Merge branch 'main' into feat(scheduler)--ScheduleTargetInput
Jacco 76ba280
fromText
Jacco 3e32773
Merge branch 'main' into feat(scheduler)--ScheduleTargetInput
kaizencc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,4 @@ | ||
export * from './schedule-expression'; | ||
export * from './schedule-expression'; | ||
export * from './input'; | ||
export * from './schedule'; | ||
export * from './targets'; |
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,120 @@ | ||
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` and other | ||
* cdk references. | ||
* | ||
* The target input value will be a single string: the string you pass | ||
* here. Do not use this method to pass a complex value like a JSON object to | ||
* a target. Use `ScheduleTargetInput.fromObject()` instead. | ||
Jacco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param text text to use as input for the target | ||
Jacco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
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,63 @@ | ||
import { IResource, Resource } from 'aws-cdk-lib'; | ||
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler'; | ||
import { Construct } from 'constructs'; | ||
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 | ||
*/ | ||
Jacco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Jacco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
|
||
/** | ||
* Interface representing a created or an imported `Schedule`. | ||
*/ | ||
export interface ISchedule extends IResource { | ||
|
||
} | ||
|
||
/** | ||
* 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), | ||
}, | ||
}); | ||
} | ||
} |
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 * 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. | ||
*/ | ||
|
||
Jacco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we specify what is cdk references?