From 9bd38f848005340d609e31f83c8192cc44925e40 Mon Sep 17 00:00:00 2001 From: Ray Myers Date: Tue, 3 Aug 2021 02:00:26 -0500 Subject: [PATCH] Add autoscaling delete_tags (#4122) --- IMPLEMENTATION_COVERAGE.md | 2 +- moto/autoscaling/models.py | 8 ++++ moto/autoscaling/responses.py | 7 +++ tests/test_autoscaling/test_autoscaling.py | 52 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 5507c1e36..ad91fac10 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -908,7 +908,7 @@ - [ ] delete_notification_configuration - [X] delete_policy - [ ] delete_scheduled_action -- [ ] delete_tags +- [X] delete_tags - [ ] delete_warm_pool - [ ] describe_account_limits - [ ] describe_adjustment_types diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index e0c74b0a4..2ed874ed2 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -992,6 +992,14 @@ class AutoScalingBackend(BaseBackend): group.tags = new_tags + def delete_tags(self, tags): + for tag_to_delete in tags: + group_name = tag_to_delete["resource_id"] + key_to_delete = tag_to_delete["key"] + group = self.autoscaling_groups[group_name] + old_tags = group.tags + group.tags = [x for x in old_tags if x["key"] != key_to_delete] + def attach_load_balancers(self, group_name, load_balancer_names): group = self.autoscaling_groups[group_name] group.load_balancers.extend( diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 638721a64..e6a309d6f 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -233,6 +233,13 @@ class AutoScalingResponse(BaseResponse): template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() + def delete_tags(self): + tags = self._get_list_prefix("Tags.member") + + self.autoscaling_backend.delete_tags(tags) + template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) + return template.render() + def describe_auto_scaling_instances(self): instance_states = self.autoscaling_backend.describe_auto_scaling_instances( instance_ids=self._get_multi_param("InstanceIds.member") diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 6bd79a141..22788f8e4 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -2640,3 +2640,55 @@ def test_terminate_instance_in_auto_scaling_group_no_decrement(): original_instance_id.shouldnt.be.within( [x["InstanceId"] for x in response["LoadBalancerDescriptions"][0]["Instances"]] ) + + +@mock_autoscaling +@mock_ec2 +def test_delete_tags_by_key(): + mocked_networking = setup_networking() + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="TestLC", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t2.medium", + ) + tag_to_delete = { + "ResourceId": "tag_test_asg", + "ResourceType": "auto-scaling-group", + "PropagateAtLaunch": True, + "Key": "TestDeleteTagKey1", + "Value": "TestTagValue1", + } + tag_to_keep = { + "ResourceId": "tag_test_asg", + "ResourceType": "auto-scaling-group", + "PropagateAtLaunch": True, + "Key": "TestTagKey1", + "Value": "TestTagValue1", + } + client.create_auto_scaling_group( + AutoScalingGroupName="tag_test_asg", + MinSize=1, + MaxSize=2, + LaunchConfigurationName="TestLC", + Tags=[tag_to_delete, tag_to_keep], + VPCZoneIdentifier=mocked_networking["subnet1"], + ) + + client.delete_tags( + Tags=[ + { + "ResourceId": "tag_test_asg", + "ResourceType": "auto-scaling-group", + "PropagateAtLaunch": True, + "Key": "TestDeleteTagKey1", + } + ] + ) + response = client.describe_auto_scaling_groups( + AutoScalingGroupNames=["tag_test_asg"] + ) + group = response["AutoScalingGroups"][0] + tags = group["Tags"] + tags.should.contain(tag_to_keep) + tags.should_not.contain(tag_to_delete)