From 3c38a551b2ba440fdb4d8578a9c6aef98d353224 Mon Sep 17 00:00:00 2001 From: Miles O'Connell Date: Tue, 6 Oct 2015 09:21:26 -0700 Subject: [PATCH 1/2] Adding tags to AutoScalingGroups --- moto/autoscaling/models.py | 4 +++- moto/autoscaling/responses.py | 2 ++ tests/test_autoscaling/test_autoscaling.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index 2c8f425ac..db4fa95a8 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -113,7 +113,8 @@ class FakeAutoScalingGroup(object): def __init__(self, name, availability_zones, desired_capacity, max_size, min_size, launch_config_name, vpc_zone_identifier, default_cooldown, health_check_period, health_check_type, - load_balancers, placement_group, termination_policies, autoscaling_backend): + load_balancers, placement_group, termination_policies, + autoscaling_backend, tags): self.autoscaling_backend = autoscaling_backend self.name = name self.availability_zones = availability_zones @@ -133,6 +134,7 @@ class FakeAutoScalingGroup(object): self.instance_states = [] self.set_desired_capacity(desired_capacity) + self.tags = tags if tags else [] @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name): diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 4f5948b6d..b1fee1a38 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -60,6 +60,7 @@ class AutoScalingResponse(BaseResponse): load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), + tags=self._get_multi_param('Tags.member'), ) template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() @@ -85,6 +86,7 @@ class AutoScalingResponse(BaseResponse): load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), + tags=self._get_multi_param('Tags.member'), ) template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 8b8f8f320..51cbfa521 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -32,6 +32,13 @@ def test_create_autoscaling_group(): placement_group="test_placement", vpc_zone_identifier='subnet-1234abcd', termination_policies=["OldestInstance", "NewestInstance"], + tags=[{ + 'ResourceId': 'tester_group', + 'ResourceType': 'auto-scaling-group', + 'Key': 'test_key', + 'Value': 'test_value', + 'PropagateAtLaunch': True, + }], ) conn.create_auto_scaling_group(group) @@ -50,6 +57,13 @@ def test_create_autoscaling_group(): list(group.load_balancers).should.equal(["test_lb"]) group.placement_group.should.equal("test_placement") list(group.termination_policies).should.equal(["OldestInstance", "NewestInstance"]) + list(group.tags).should.equal([{ + 'ResourceId': 'tester_group', + 'ResourceType': 'auto-scaling-group', + 'Key': 'test_key', + 'Value': 'test_value', + 'PropagateAtLaunch': True, + }]) @mock_autoscaling @@ -88,6 +102,7 @@ def test_create_autoscaling_groups_defaults(): list(group.load_balancers).should.equal([]) group.placement_group.should.equal(None) list(group.termination_policies).should.equal([]) + list(group.tags).should.equal([]) @mock_autoscaling From b3096af0989db4edc1b8f03051c99c48c7ac81e7 Mon Sep 17 00:00:00 2001 From: milesoc Date: Tue, 6 Oct 2015 18:02:38 +0000 Subject: [PATCH 2/2] Set tags in response, fix tests for tags --- moto/autoscaling/models.py | 6 +++-- moto/autoscaling/responses.py | 15 ++++++++--- tests/test_autoscaling/test_autoscaling.py | 29 +++++++++++----------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index db4fa95a8..cb95d0542 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -113,7 +113,7 @@ class FakeAutoScalingGroup(object): def __init__(self, name, availability_zones, desired_capacity, max_size, min_size, launch_config_name, vpc_zone_identifier, default_cooldown, health_check_period, health_check_type, - load_balancers, placement_group, termination_policies, + load_balancers, placement_group, termination_policies, autoscaling_backend, tags): self.autoscaling_backend = autoscaling_backend self.name = name @@ -158,6 +158,7 @@ class FakeAutoScalingGroup(object): load_balancers=load_balancer_names, placement_group=None, termination_policies=properties.get("TerminationPolicies", []), + tags=properties.get("Tags", []), ) return group @@ -263,7 +264,7 @@ class AutoScalingBackend(BaseBackend): launch_config_name, vpc_zone_identifier, default_cooldown, health_check_period, health_check_type, load_balancers, - placement_group, termination_policies): + placement_group, termination_policies, tags): def make_int(value): return int(value) if value is not None else value @@ -288,6 +289,7 @@ class AutoScalingBackend(BaseBackend): placement_group=placement_group, termination_policies=termination_policies, autoscaling_backend=self, + tags=tags, ) self.autoscaling_groups[name] = group return group diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index b1fee1a38..70fda4526 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -60,7 +60,7 @@ class AutoScalingResponse(BaseResponse): load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), - tags=self._get_multi_param('Tags.member'), + tags=self._get_list_prefix('Tags.member'), ) template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() @@ -86,7 +86,6 @@ class AutoScalingResponse(BaseResponse): load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), - tags=self._get_multi_param('Tags.member'), ) template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() @@ -237,7 +236,17 @@ DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE = """ {% for group in groups %} - + + {% for tag in group.tags %} + + {{ tag.resource_type }} + {{ tag.resource_id }} + {{ tag.propagate_at_launch }} + {{ tag.key }} + {{ tag.value }} + + {% endfor %} + {{ group.name }} {{ group.health_check_type }} diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 51cbfa521..41286442d 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import boto from boto.ec2.autoscale.launchconfig import LaunchConfiguration from boto.ec2.autoscale.group import AutoScalingGroup +from boto.ec2.autoscale import Tag import sure # noqa from moto import mock_autoscaling, mock_ec2 @@ -18,6 +19,7 @@ def test_create_autoscaling_group(): ) conn.create_launch_configuration(config) + group = AutoScalingGroup( name='tester_group', availability_zones=['us-east-1c', 'us-east-1b'], @@ -32,13 +34,13 @@ def test_create_autoscaling_group(): placement_group="test_placement", vpc_zone_identifier='subnet-1234abcd', termination_policies=["OldestInstance", "NewestInstance"], - tags=[{ - 'ResourceId': 'tester_group', - 'ResourceType': 'auto-scaling-group', - 'Key': 'test_key', - 'Value': 'test_value', - 'PropagateAtLaunch': True, - }], + tags=[Tag( + resource_id='tester_group', + key='test_key', + value='test_value', + propagate_at_launch=True + ) + ], ) conn.create_auto_scaling_group(group) @@ -57,13 +59,12 @@ def test_create_autoscaling_group(): list(group.load_balancers).should.equal(["test_lb"]) group.placement_group.should.equal("test_placement") list(group.termination_policies).should.equal(["OldestInstance", "NewestInstance"]) - list(group.tags).should.equal([{ - 'ResourceId': 'tester_group', - 'ResourceType': 'auto-scaling-group', - 'Key': 'test_key', - 'Value': 'test_value', - 'PropagateAtLaunch': True, - }]) + len(list(group.tags)).should.equal(1) + tag = list(group.tags)[0] + tag.resource_id.should.equal('tester_group') + tag.key.should.equal('test_key') + tag.value.should.equal('test_value') + tag.propagate_at_launch.should.equal(True) @mock_autoscaling