From acf34a685ff33b76e08cc513e6bc7a9310cf38ab Mon Sep 17 00:00:00 2001 From: Timothy Klopotoski Date: Sat, 9 Oct 2021 06:11:46 -0400 Subject: [PATCH] Delete event rule when deleted from cloudformation template (#4382) --- moto/events/models.py | 7 ++++ .../test_events/test_events_cloudformation.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/moto/events/models.py b/moto/events/models.py index 7a0b800ad..fda7cd628 100644 --- a/moto/events/models.py +++ b/moto/events/models.py @@ -271,6 +271,13 @@ class Rule(CloudFormationModel): new_resource_name, cloudformation_json, region_name ) + @classmethod + def delete_from_cloudformation_json( + cls, resource_name, cloudformation_json, region_name + ): + event_backend = events_backends[region_name] + event_backend.delete_rule(resource_name) + def describe(self): attributes = { "Arn": self.arn, diff --git a/tests/test_events/test_events_cloudformation.py b/tests/test_events/test_events_cloudformation.py index 954ee8811..eaa1ce37d 100644 --- a/tests/test_events/test_events_cloudformation.py +++ b/tests/test_events/test_events_cloudformation.py @@ -1,8 +1,10 @@ +import pytest import copy from string import Template import boto3 import json +from botocore.exceptions import ClientError from moto import mock_cloudformation, mock_events import sure # noqa @@ -60,6 +62,15 @@ rule_template = Template( ) +empty = json.dumps( + { + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "EventBridge Rule Test", + "Resources": {}, + } +) + + @mock_events @mock_cloudformation def test_create_archive(): @@ -154,3 +165,24 @@ def test_create_rule(): response["Arn"].should.equal(rule_arn) response["EventPattern"].should.equal('{"detail-type": ["SomeDetailType"]}') + + +@mock_events +@mock_cloudformation +def test_delete_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}) + cfn_client.create_stack(StackName=stack_name, TemplateBody=template) + + # when + cfn_client.update_stack(StackName=stack_name, TemplateBody=empty) + + # then + events_client = boto3.client("events", region_name="eu-central-1") + + with pytest.raises(ClientError, match="does not exist") as e: + + events_client.describe_rule(Name=name)