|
| 1 | +import { DefaultTokenResolver, IResolveContext, Stack, StringConcat, Token, Tokenization } from 'aws-cdk-lib'; |
| 2 | +import { ISchedule } from './schedule'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The text, or well-formed JSON, passed to the target of the schedule. |
| 6 | + */ |
| 7 | +export abstract class ScheduleTargetInput { |
| 8 | + /** |
| 9 | + * Pass text to the target, it is possible to embed `ContextAttributes` |
| 10 | + * that will be resolved to actual values while the CloudFormation is |
| 11 | + * deployed or cdk Tokens that will be resolved when the CloudFormation |
| 12 | + * templates are generated by CDK. |
| 13 | + * |
| 14 | + * The target input value will be a single string that you pass. |
| 15 | + * For passing complex values like JSON object to a target use method |
| 16 | + * `ScheduleTargetInput.fromObject()` instead. |
| 17 | + * |
| 18 | + * @param text Text to use as the input for the target |
| 19 | + */ |
| 20 | + public static fromText(text: string): ScheduleTargetInput { |
| 21 | + return new FieldAwareEventInput(text); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Pass a JSON object to the target, it is possible to embed `ContextAttributes` and other |
| 26 | + * cdk references. |
| 27 | + * |
| 28 | + * @param obj object to use to convert to JSON to use as input for the target |
| 29 | + */ |
| 30 | + public static fromObject(obj: any): ScheduleTargetInput { |
| 31 | + return new FieldAwareEventInput(obj); |
| 32 | + } |
| 33 | + |
| 34 | + protected constructor() { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Return the input properties for this input object |
| 39 | + */ |
| 40 | + public abstract bind(schedule: ISchedule): string; |
| 41 | +} |
| 42 | + |
| 43 | +class FieldAwareEventInput extends ScheduleTargetInput { |
| 44 | + constructor(private readonly input: any) { |
| 45 | + super(); |
| 46 | + } |
| 47 | + |
| 48 | + public bind(schedule: ISchedule): string { |
| 49 | + class Replacer extends DefaultTokenResolver { |
| 50 | + constructor() { |
| 51 | + super(new StringConcat()); |
| 52 | + } |
| 53 | + |
| 54 | + public resolveToken(t: Token, _context: IResolveContext) { |
| 55 | + return Token.asString(t); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const stack = Stack.of(schedule); |
| 60 | + return stack.toJsonString(Tokenization.resolve(this.input, { |
| 61 | + scope: schedule, |
| 62 | + resolver: new Replacer(), |
| 63 | + })); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Represents a field in the event pattern |
| 69 | + * |
| 70 | + * @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-schedule-context-attributes.html |
| 71 | + */ |
| 72 | +export class ContextAttribute { |
| 73 | + /** |
| 74 | + * The ARN of the schedule. |
| 75 | + */ |
| 76 | + public static get scheduleArn(): string { |
| 77 | + return this.fromName('schedule-arn'); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The time you specified for the schedule to invoke its target, for example, |
| 82 | + * 2022-03-22T18:59:43Z. |
| 83 | + */ |
| 84 | + public static get scheduledTime(): string { |
| 85 | + return this.fromName('scheduled-time'); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * The unique ID that EventBridge Scheduler assigns for each attempted invocation of |
| 90 | + * a target, for example, d32c5kddcf5bb8c3. |
| 91 | + */ |
| 92 | + public static get executionId(): string { |
| 93 | + return this.fromName('execution-id'); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * A counter that identifies the attempt number for the current invocation, for |
| 98 | + * example, 1. |
| 99 | + */ |
| 100 | + public static get attemptNumber(): string { |
| 101 | + return this.fromName('attempt-number'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Escape hatch for other ContextAttribute that might be resolved in future. |
| 106 | + * |
| 107 | + * @param name - name will replace xxx in <aws.scheduler.xxx> |
| 108 | + */ |
| 109 | + public static fromName(name: string): string { |
| 110 | + return new ContextAttribute(name).toString(); |
| 111 | + } |
| 112 | + |
| 113 | + private constructor(public readonly name: string) { |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Convert the path to the field in the event pattern to JSON |
| 118 | + */ |
| 119 | + public toString() { |
| 120 | + return `<aws.scheduler.${this.name}>`; |
| 121 | + } |
| 122 | +} |
0 commit comments