diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index d54c39d71..2dbdc077d 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -440,6 +440,26 @@ class AutoScalingBackend(BaseBackend): self.elb_backend.register_instances(elb.name, group_instance_ids - elb_instace_ids) self.elb_backend.deregister_instances(elb.name, elb_instace_ids - group_instance_ids) + def create_or_update_tags(self, tags): + + for tag in tags: + group_name = tag["resource_id"] + group = self.autoscaling_groups[group_name] + old_tags = group.tags + + new_tags = [] + #if key was in old_tags, update old tag + for old_tag in old_tags: + if old_tag["key"] == tag["key"]: + new_tags.append(tag) + else: + new_tags.append(old_tag) + + #if key was never in old_tag's add it (create tag) + if not any(new_tag['key'] == tag['key'] for new_tag in new_tags): + new_tags.append(tag) + + group.tags = new_tags autoscaling_backends = {} for region, ec2_backend in ec2_backends.items(): diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 79cb6aeee..1840e029c 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -105,6 +105,13 @@ class AutoScalingResponse(BaseResponse): template = self.response_template(SET_DESIRED_CAPACITY_TEMPLATE) return template.render() + def create_or_update_tags(self): + tags = self._get_list_prefix('Tags.member') + + self.autoscaling_backend.create_or_update_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_autoscaling_instances() template = self.response_template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE) diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 2f442c163..ea11b77e1 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -169,6 +169,48 @@ def test_autoscaling_update(): group.vpc_zone_identifier.should.equal('subnet-5678efgh') +@mock_autoscaling +def test_autoscaling_tags_update(): + conn = boto.connect_autoscale() + config = LaunchConfiguration( + name='tester', + image_id='ami-abcd1234', + instance_type='t2.medium', + ) + conn.create_launch_configuration(config) + + group = AutoScalingGroup( + name='tester_group', + availability_zones=['us-east-1c', 'us-east-1b'], + desired_capacity=2, + max_size=2, + min_size=2, + launch_config=config, + vpc_zone_identifier='subnet-1234abcd', + tags=[Tag( + resource_id='tester_group', + key='test_key', + value='test_value', + propagate_at_launch=True + )], + ) + conn.create_auto_scaling_group(group) + + conn.create_or_update_tags(tags=[Tag( + resource_id='tester_group', + key='test_key', + value='new_test_value', + propagate_at_launch=True + ), Tag( + resource_id='tester_group', + key='test_key2', + value='test_value2', + propagate_at_launch=True + )]) + group = conn.get_all_groups()[0] + group.tags.should.have.length_of(2) + + @mock_autoscaling def test_autoscaling_group_delete(): conn = boto.connect_autoscale() @@ -420,6 +462,7 @@ def test_describe_autoscaling_groups_boto3(): response['ResponseMetadata']['HTTPStatusCode'].should.equal(200) response['AutoScalingGroups'][0]['AutoScalingGroupName'].should.equal('test_asg') + @mock_autoscaling def test_update_autoscaling_group_boto3(): client = boto3.client('autoscaling', region_name='us-east-1') @@ -443,3 +486,41 @@ def test_update_autoscaling_group_boto3(): AutoScalingGroupNames=["test_asg"] ) response['AutoScalingGroups'][0]['MinSize'].should.equal(1) + + +@mock_autoscaling +def test_autoscaling_taqs_update_boto3(): + client = boto3.client('autoscaling', region_name='us-east-1') + _ = client.create_launch_configuration( + LaunchConfigurationName='test_launch_configuration' + ) + _ = client.create_auto_scaling_group( + AutoScalingGroupName='test_asg', + LaunchConfigurationName='test_launch_configuration', + MinSize=0, + MaxSize=20, + DesiredCapacity=5, + Tags=[{ + "ResourceId": 'test_asg', + "Key": 'test_key', + "Value": 'test_value', + "PropagateAtLaunch": True + }] + ) + + client.create_or_update_tags(Tags=[{ + "ResourceId": 'test_asg', + "Key": 'test_key', + "Value": 'updated_test_value', + "PropagateAtLaunch": True + }, { + "ResourceId": 'test_asg', + "Key": 'test_key2', + "Value": 'test_value2', + "PropagateAtLaunch": True + }]) + + response = client.describe_auto_scaling_groups( + AutoScalingGroupNames=["test_asg"] + ) + response['AutoScalingGroups'][0]['Tags'].should.have.length_of(2)