From dc7d72fb013bab5340d7af3e9f8432126a06bc17 Mon Sep 17 00:00:00 2001 From: Lora Reames Date: Thu, 28 Dec 2023 21:58:21 +0000 Subject: [PATCH] feat: allow custom schedule for event rule, default is now 1 day --- API.md | 17 +++++++++++++- src/index.ts | 5 +++- test/index.test.ts | 57 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/API.md b/API.md index d395e0e..299057d 100644 --- a/API.md +++ b/API.md @@ -132,7 +132,8 @@ const accountClosureStepFunctionProps: AccountClosureStepFunctionProps = { ... } | **Name** | **Type** | **Description** | | --- | --- | --- | -| privilegedRoleArn | string | *No description.* | +| privilegedRoleArn | string | Arn of privledged role to be assumed by the stepfunction when closing accounts. | +| schedule | aws-cdk-lib.aws_events.Schedule | Custom schedule for the event rule @default 1 day. | --- @@ -144,6 +145,20 @@ public readonly privilegedRoleArn: string; - *Type:* string +Arn of privledged role to be assumed by the stepfunction when closing accounts. + +--- + +##### `schedule`Optional + +```typescript +public readonly schedule: Schedule; +``` + +- *Type:* aws-cdk-lib.aws_events.Schedule + +Custom schedule for the event rule @default 1 day. + --- diff --git a/src/index.ts b/src/index.ts index a478854..65527c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,10 @@ import { CallAwsService } from "aws-cdk-lib/aws-stepfunctions-tasks"; import { Construct } from "constructs"; export interface AccountClosureStepFunctionProps { + /** Arn of privledged role to be assumed by the stepfunction when closing accounts */ readonly privilegedRoleArn: string; // TODO provide example role for repo with all required permissions + /** Custom schedule for the event rule @default 1 day */ + readonly schedule?: Schedule; } export class AccountClosureStepFunction extends Construct { constructor( @@ -153,7 +156,7 @@ export class AccountClosureStepFunction extends Construct { definitionBody: DefinitionBody.fromChainable(definition), }, eventRuleProps: { - schedule: Schedule.rate(Duration.hours(1)), + schedule: props.schedule ?? Schedule.rate(Duration.days(1)), }, }); } diff --git a/test/index.test.ts b/test/index.test.ts index 5cad3f0..82d8670 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,28 +1,51 @@ -import * as cdk from "aws-cdk-lib"; -import { Stack } from "aws-cdk-lib"; +import { Stack, Duration, App } from "aws-cdk-lib"; import { Template } from "aws-cdk-lib/assertions"; +import { Schedule } from "aws-cdk-lib/aws-events"; import { Construct } from "constructs"; import { AccountClosureStepFunction } from "../src/index"; describe("Account Closure Construct", () => { - const app = new cdk.App(); + it("should create step function and event rule", async () => { + const app = new App(); + class TestStack extends Stack { + constructor(scope: Construct, id: string) { + super(scope, id); + new AccountClosureStepFunction( + this, + "AccountClosureStepFunctionConstruct", + { + privilegedRoleArn: "arn:aws:iam::123456789012:role/PrivilegedRole", + } + ); + } + } + const testStack = new TestStack(app, "TestStack"); + const template = Template.fromStack(testStack); + + template.hasResource("AWS::StepFunctions::StateMachine", {}); + template.hasResourceProperties("AWS::Events::Rule", { + ScheduleExpression: "rate(1 day)", + }); + }); - class TestStack extends Stack { - constructor(scope: Construct, id: string) { - super(scope, id); - new AccountClosureStepFunction( - this, - "AccountClosureStepFunctionConstruct", - { - privilegedRoleArn: "arn:aws:iam::123456789012:role/PrivilegedRole", - } - ); + it("should allow customization of schedule for event rule", async () => { + const app = new App(); + class TestStack extends Stack { + constructor(scope: Construct, id: string) { + super(scope, id); + new AccountClosureStepFunction( + this, + "AccountClosureStepFunctionConstruct", + { + privilegedRoleArn: "arn:aws:iam::123456789012:role/PrivilegedRole", + schedule: Schedule.rate(Duration.hours(1)), + } + ); + } } - } - const testStack = new TestStack(app, "TestStack"); - const template = Template.fromStack(testStack); + const testStack = new TestStack(app, "TestStack"); + const template = Template.fromStack(testStack); - it("should create step function and event rule", async () => { template.hasResource("AWS::StepFunctions::StateMachine", {}); template.hasResourceProperties("AWS::Events::Rule", { ScheduleExpression: "rate(1 hour)",