From b4f02f34363f1d4c09b14fb04bdda86b553b6aac Mon Sep 17 00:00:00 2001 From: Timothy Klopotoski Date: Wed, 18 Aug 2021 15:31:29 -0400 Subject: [PATCH] Translate Cloudformation API data to EventBridge API data (#4188) --- moto/events/models.py | 4 ++ .../test_events/test_events_cloudformation.py | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/moto/events/models.py b/moto/events/models.py index ff2c2c70c..0c3fa52c2 100644 --- a/moto/events/models.py +++ b/moto/events/models.py @@ -236,6 +236,10 @@ class Rule(CloudFormationModel): ): properties = cloudformation_json["Properties"] properties.setdefault("EventBusName", "default") + + if "EventPattern" in properties: + properties["EventPattern"] = json.dumps(properties["EventPattern"]) + event_name = resource_name event_pattern = properties.get("EventPattern") diff --git a/tests/test_events/test_events_cloudformation.py b/tests/test_events/test_events_cloudformation.py index b22f37837..954ee8811 100644 --- a/tests/test_events/test_events_cloudformation.py +++ b/tests/test_events/test_events_cloudformation.py @@ -35,6 +35,31 @@ archive_template = Template( ) +rule_template = Template( + json.dumps( + { + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "EventBridge Rule Test", + "Resources": { + "Rule": { + "Type": "AWS::Events::Rule", + "Properties": { + "Name": "${rule_name}", + "EventPattern": {"detail-type": ["SomeDetailType"]}, + }, + } + }, + "Outputs": { + "Arn": { + "Description": "Rule Arn", + "Value": {"Fn::GetAtt": ["Rule", "Arn"]}, + } + }, + } + ) +) + + @mock_events @mock_cloudformation def test_create_archive(): @@ -105,3 +130,27 @@ def test_delete_archive(): events_client = boto3.client("events", region_name="eu-central-1") response = events_client.list_archives(NamePrefix="test")["Archives"] response.should.have.length_of(0) + + +@mock_events +@mock_cloudformation +def test_create_rule(): + # given + cfn_client = boto3.client("cloudformation", region_name="eu-central-1") + name = "test-rule" + stack_name = "test-stack" + template = rule_template.substitute({"rule_name": name}) + + # when + cfn_client.create_stack(StackName=stack_name, TemplateBody=template) + + # then + rule_arn = "arn:aws:events:eu-central-1:{0}:rule/{1}".format(ACCOUNT_ID, name) + stack = cfn_client.describe_stacks(StackName=stack_name)["Stacks"][0] + stack["Outputs"][0]["OutputValue"].should.equal(rule_arn) + + events_client = boto3.client("events", region_name="eu-central-1") + response = events_client.describe_rule(Name=name) + + response["Arn"].should.equal(rule_arn) + response["EventPattern"].should.equal('{"detail-type": ["SomeDetailType"]}')