Skip to content

Commit

Permalink
add event_pattern trigger feature (#2)
Browse files Browse the repository at this point in the history
* add event_pattern trigger feature

* updating rspec tests
  • Loading branch information
dupontz committed Oct 4, 2023
1 parent 7d0e81e commit 0108abd
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
12 changes: 8 additions & 4 deletions ecs-runtask.cfndsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
end

schedule = external_parameters.fetch(:schedule, nil)
unless schedule.nil?
event_pattern = external_parameters.fetch(:event_pattern, nil)

if (!schedule.nil? || !event_pattern.nil?)
iam_policies = external_parameters.fetch(:scheduler_iam_policies, {})
policies = []
iam_policies.each do |name,policy|
Expand All @@ -85,11 +87,13 @@
Policies(policies)
end
Events_Rule(:Schedule) do
Name FnSub("${EnvironmentName}-#{component_name}-schedule")
Description FnSub("{EnvironmentName} #{component_name} schedule")
ScheduleExpression schedule
Name FnSub("${EnvironmentName}-#{component_name}-eventrule")
Description FnSub("{EnvironmentName} #{component_name} eventrule")
ScheduleExpression schedule unless schedule.nil?
EventPattern FnSub(event_pattern) unless event_pattern.nil?
Targets [{
Arn: Ref(:StateMachine),
Id: FnSub("{EnvironmentName}-#{component_name}-target"),
RoleArn: FnGetAtt('EventBridgeInvokeRole', 'Arn')
}]
end
Expand Down
2 changes: 1 addition & 1 deletion ecs-runtask.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ scheduler_iam_policies:
action:
- states:StartExecution
resource:
- Fn::Sub: arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:${StateMachine}
- Fn::Sub: ${StateMachine}

state_machine: |
{
Expand Down
77 changes: 77 additions & 0 deletions spec/event_pattern_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'yaml'

describe 'should fail without a task_definition' do

context 'cftest' do
it 'compiles test' do
expect(system("cfhighlander cftest #{@validate} --tests tests/event_pattern.test.yaml")).to be_truthy
end
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/schedule/ecs-runtask.compiled.yaml") }

context 'Resource Task' do
let(:properties) { template["Resources"]["Task"]["Properties"] }

it 'has property RequiresCompatibilities ' do
expect(properties["RequiresCompatibilities"]).to eq(['FARGATE'])
end

it 'has property NetworkMode ' do
expect(properties["NetworkMode"]).to eq('awsvpc')
end

it 'has property CPU ' do
expect(properties["Cpu"]).to eq(256)
end

it 'has property Memory ' do
expect(properties["Memory"]).to eq(512)
end

end

context 'Resource StateMachine' do
let(:properties) { template["Resources"]["StateMachine"]["Properties"] }

it 'has property StateMachineName' do
expect(properties["StateMachineName"]).to eq({"Fn::Sub"=>"${EnvironmentName}-ecs-runtask-RunTask"})
end

it 'has property RoleArn' do
expect(properties["RoleArn"]).to eq({"Fn::GetAtt" => ["StepFunctionRole", "Arn"]})
end

it 'has property DefinitionString' do
expect(properties["DefinitionString"]).not_to be_nil
end
end

context 'Resource Schedule' do
let(:properties) { template["Resources"]["Schedule"]["Properties"] }

it 'has property Name' do
expect(properties["Name"]).to eq({"Fn::Sub"=>"${EnvironmentName}-ecs-runtask-eventrule"})
end

it 'has property Description' do
expect(properties["Description"]).to eq({"Fn::Sub"=>"{EnvironmentName} ecs-runtask eventrule"})
end

it 'has property ScheduleExpression' do
expect(properties["ScheduleExpression"]).to eq('* * * * *')
end

it 'has property Targets' do
expect(properties["Targets"]).to eq([{
"Arn"=>{"Ref"=>"StateMachine"},
"Id"=> {"Fn::Sub"=>"{EnvironmentName}-ecs-runtask-target"},
"RoleArn"=>{"Fn::GetAtt"=>["EventBridgeInvokeRole", "Arn"]}
}])
end

end



end
5 changes: 3 additions & 2 deletions spec/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
let(:properties) { template["Resources"]["Schedule"]["Properties"] }

it 'has property Name' do
expect(properties["Name"]).to eq({"Fn::Sub"=>"${EnvironmentName}-ecs-runtask-schedule"})
expect(properties["Name"]).to eq({"Fn::Sub"=>"${EnvironmentName}-ecs-runtask-eventrule"})
end

it 'has property Description' do
expect(properties["Description"]).to eq({"Fn::Sub"=>"{EnvironmentName} ecs-runtask schedule"})
expect(properties["Description"]).to eq({"Fn::Sub"=>"{EnvironmentName} ecs-runtask eventrule"})
end

it 'has property ScheduleExpression' do
Expand All @@ -65,6 +65,7 @@
it 'has property Targets' do
expect(properties["Targets"]).to eq([{
"Arn"=>{"Ref"=>"StateMachine"},
"Id"=> {"Fn::Sub"=>"{EnvironmentName}-ecs-runtask-target"},
"RoleArn"=>{"Fn::GetAtt"=>["EventBridgeInvokeRole", "Arn"]}
}])
end
Expand Down
25 changes: 25 additions & 0 deletions tests/event_pattern.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

test_metadata:
type: config
name: event_pattern
description: event pattern config test


task_type: FARGATE
network_mode: awsvpc
maximum_availability_zones: 3
cpu: 256
memory: 512

task_definition:
dummy:
image: apline
tag_param: Version


event_pattern: |
{
"source": ["aws.cloudformation"],
"detail-type": ["CloudFormation Stack Status Change"],
"resources": ["arn:aws:cloudformation:us-west-2:123456789012:stack/dev-stack"]
}

0 comments on commit 0108abd

Please sign in to comment.