From 0108abdc6eb641eb64fb601f78d7de44cc6dda5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Dupont=20Rodrigues=20Gon=C3=A7alves?= Date: Wed, 4 Oct 2023 16:08:58 -0700 Subject: [PATCH] add event_pattern trigger feature (#2) * add event_pattern trigger feature * updating rspec tests --- ecs-runtask.cfndsl.rb | 12 ++++-- ecs-runtask.config.yaml | 2 +- spec/event_pattern_spec.rb | 77 +++++++++++++++++++++++++++++++++++ spec/schedule_spec.rb | 5 ++- tests/event_pattern.test.yaml | 25 ++++++++++++ 5 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 spec/event_pattern_spec.rb create mode 100644 tests/event_pattern.test.yaml diff --git a/ecs-runtask.cfndsl.rb b/ecs-runtask.cfndsl.rb index 26f9851..d91bd71 100644 --- a/ecs-runtask.cfndsl.rb +++ b/ecs-runtask.cfndsl.rb @@ -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| @@ -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 diff --git a/ecs-runtask.config.yaml b/ecs-runtask.config.yaml index 10a517e..f4aad0e 100644 --- a/ecs-runtask.config.yaml +++ b/ecs-runtask.config.yaml @@ -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: | { diff --git a/spec/event_pattern_spec.rb b/spec/event_pattern_spec.rb new file mode 100644 index 0000000..6144a2c --- /dev/null +++ b/spec/event_pattern_spec.rb @@ -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 diff --git a/spec/schedule_spec.rb b/spec/schedule_spec.rb index ed157de..1a20b90 100644 --- a/spec/schedule_spec.rb +++ b/spec/schedule_spec.rb @@ -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 @@ -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 diff --git a/tests/event_pattern.test.yaml b/tests/event_pattern.test.yaml new file mode 100644 index 0000000..0437912 --- /dev/null +++ b/tests/event_pattern.test.yaml @@ -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"] + } \ No newline at end of file