Delete event rule when deleted from cloudformation template (#4382)

This commit is contained in:
Timothy Klopotoski 2021-10-09 06:11:46 -04:00 committed by GitHub
parent 6ff03f3974
commit acf34a685f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -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,

View File

@ -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)