Skip to content

Commit

Permalink
Merge pull request #705 from gradientedge/fix/optional-rule
Browse files Browse the repository at this point in the history
fix: rule pattern erroring when rule missing
  • Loading branch information
kamaz authored Nov 20, 2024
2 parents 47fbe72 + 18406b5 commit f4963ac
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">=16 <=20",
"pnpm": "=9"
},
"packageManager": "pnpm@8.15.8",
"packageManager": "pnpm@9.14.1+sha256.9978d5f40d4f376b054cf8c000cf8c326284344281e3c9bbf4e3d6f153b0e8de",
"repository": {
"type": "git",
"url": "git+https://github.com/gradientedge/cdk-utils.git"
Expand Down
10 changes: 9 additions & 1 deletion src/lib/aws/construct/event-handler/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class EventHandler extends CommonConstruct {
* @summary Method to create the event rule pattern.
*/
protected createEventRulePattern() {
if (!this.props.eventRule) return
this.handler.rulePattern = this.props.eventRule.eventPattern
}

Expand Down Expand Up @@ -130,7 +131,13 @@ export class EventHandler extends CommonConstruct {
*/
protected createEventArchive() {
/* do not enable for scheduled events */
if (this.props.eventRule.schedule || this.props.eventRuleSchedule || !this.props.eventRuleArchiveEnabled) return
if (
!this.props.eventRule ||
this.props.eventRule.schedule ||
this.props.eventRuleSchedule ||
!this.props.eventRuleArchiveEnabled
)
return
this.handler.archive = new Archive(this, `${this.id}-archive`, {
archiveName: `${this.props.eventRule.ruleName}-${this.props.stage}`.replace(
`${this.node.tryGetContext('stackName')}-`,
Expand All @@ -147,6 +154,7 @@ export class EventHandler extends CommonConstruct {
* @summary Method to create the event rule.
*/
protected createEventRule() {
if (!this.props.eventRule) return
let schedule
if (this.props.eventRuleSchedule) {
schedule = Schedule.expression(this.props.eventRuleSchedule)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/aws/construct/event-handler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EventRuleProps, LogProps, QueueProps, SfnMapProps, SfnStateMachineProps
export interface EventHandlerProps extends CommonStackProps {
eventBusName: string
eventRetentionInDays: number
eventRule: EventRuleProps
eventRule?: EventRuleProps
eventRuleArchiveEnabled: boolean
eventRuleSchedule: string
eventSqs: QueueProps
Expand Down
94 changes: 94 additions & 0 deletions src/test/aws/construct/event-handler-empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as cdk from 'aws-cdk-lib'
import { Template } from 'aws-cdk-lib/assertions'
import { IFunction } from 'aws-cdk-lib/aws-lambda'
import { Succeed } from 'aws-cdk-lib/aws-stepfunctions'
import { Construct } from 'constructs'
import { CommonStack, EventHandler, EventHandlerProps } from '../../../lib'

interface TestStackProps extends EventHandlerProps {
testLambda: any
workflowStepSuccess: any
}

const testStackProps = {
apiSubDomain: 'api',
domainName: 'gradientedge.io',
env: {
account: '123456789',
region: 'eu-west-1',
},
extraContexts: [
'src/test/aws/common/cdkConfig/lambdas.json',
'src/test/aws/common/cdkConfig/logs.json',
'src/test/aws/common/cdkConfig/pipes.json',
'src/test/aws/common/cdkConfig/rules.json',
'src/test/aws/common/cdkConfig/sqs.json',
'src/test/aws/common/cdkConfig/stepFunctions.json',
'src/test/aws/common/cdkConfig/vpc.json',
],
name: 'test-api-stack',
region: 'eu-west-1',
stackName: 'test',
stage: 'test',
stageContextPath: 'src/test/aws/common/cdkEnv',
}

class TestCommonStack extends CommonStack {
declare props: TestStackProps

constructor(parent: cdk.App, name: string, props: cdk.StackProps) {
super(parent, name, props)

this.construct = new TestEventHandler(this, testStackProps.name, this.props)
}

protected determineConstructProps(props: cdk.StackProps) {
return {
...super.determineConstructProps(props),
}
}
}

class TestEventHandler extends EventHandler {
declare props: TestStackProps
workflowStepSuccess: Succeed
testLambda: IFunction

constructor(parent: Construct, id: string, props: TestStackProps) {
super(parent, id, props)
this.props = props
this.id = 'test'
this.initResources()
}

public initResources() {
super.initResources()
}
}

const app = new cdk.App({ context: testStackProps })
const stack = new TestCommonStack(app, 'test-api-stack', testStackProps)
const template = Template.fromStack(stack)

describe('TestEventHandler empty', () => {
test('is initialised as expected', () => {
/* test if the created stack have the right properties injected */
expect(stack.props).toHaveProperty('region')
expect(stack.props.region).toEqual('eu-west-1')
})
})

describe('TestEventHandler empty', () => {
test('synthesises as expected', () => {
/* test if number of resources are correctly synthesised */
template.resourceCountIs('AWS::IAM::Role', 0)
template.resourceCountIs('AWS::IAM::Policy', 0)
template.resourceCountIs('AWS::Lambda::Function', 0)
template.resourceCountIs('AWS::Lambda::Permission', 0)
template.resourceCountIs('AWS::SQS::Queue', 0)
template.resourceCountIs('AWS::SQS::QueuePolicy', 0)
template.resourceCountIs('AWS::Logs::LogGroup', 0)
template.resourceCountIs('AWS::StepFunctions::StateMachine', 0)
template.resourceCountIs('AWS::Events::Rule', 0)
})
})

0 comments on commit f4963ac

Please sign in to comment.