From c95254a2843fac342e702f7708cce63274a053d0 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 5 Feb 2020 11:58:52 -0500 Subject: [PATCH] delete tags when their resource is deleted --- moto/events/models.py | 2 ++ moto/utilities/tagging_service.py | 3 +++ tests/test_utilities/test_tagging_service.py | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/moto/events/models.py b/moto/events/models.py index 695cfb17a..82723ac6c 100644 --- a/moto/events/models.py +++ b/moto/events/models.py @@ -143,6 +143,8 @@ class EventsBackend(BaseBackend): def delete_rule(self, name): self.rules_order.pop(self.rules_order.index(name)) + arn = self.rules.get(name).arn + self.tagger.delete_all_tags_for_resource(arn) return self.rules.pop(name) is not None def describe_rule(self, name): diff --git a/moto/utilities/tagging_service.py b/moto/utilities/tagging_service.py index 8c7a86f1d..c56fd2306 100644 --- a/moto/utilities/tagging_service.py +++ b/moto/utilities/tagging_service.py @@ -12,6 +12,9 @@ class TaggingService: result.append({self.keyName: k, self.valueName: v}) return {self.tagName: result} + def delete_all_tags_for_resource(self, arn): + del self.tags[arn] + def tag_resource(self, arn, tags): if arn not in self.tags: self.tags[arn] = {} diff --git a/tests/test_utilities/test_tagging_service.py b/tests/test_utilities/test_tagging_service.py index 0d7db3e25..249e903fe 100644 --- a/tests/test_utilities/test_tagging_service.py +++ b/tests/test_utilities/test_tagging_service.py @@ -19,6 +19,7 @@ def test_create_tag(): expected.should.be.equal(actual) + def test_create_tag_without_value(): svc = TaggingService() tags = [{"Key": "key_key"}] @@ -28,6 +29,7 @@ def test_create_tag_without_value(): expected.should.be.equal(actual) + def test_delete_tag_using_names(): svc = TaggingService() tags = [{"Key": "key_key", "Value": "value_value"}] @@ -37,6 +39,19 @@ def test_delete_tag_using_names(): {"Tags": []}.should.be.equal(result) + +def test_delete_all_tags_for_resource(): + svc = TaggingService() + tags = [{"Key": "key_key", "Value": "value_value"}] + tags2 = [{"Key": "key_key2", "Value": "value_value2"}] + svc.tag_resource("arn", tags) + svc.tag_resource("arn", tags2) + svc.delete_all_tags_for_resource("arn") + result = svc.list_tags_for_resource("arn") + + {"Tags": []}.should.be.equal(result) + + def test_list_empty_delete(): svc = TaggingService() svc.untag_resource_using_names("arn", ["key_key"]) @@ -44,6 +59,7 @@ def test_list_empty_delete(): {"Tags": []}.should.be.equal(result) + def test_delete_tag_using_tags(): svc = TaggingService() tags = [{"Key": "key_key", "Value": "value_value"}] @@ -61,4 +77,3 @@ def test_extract_tag_names(): expected = ["key1", "key2"] expected.should.be.equal(actual) -