1# Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
2# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.  All Rights Reserved
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23
24class Tag(object):
25    """
26    A name/value tag on an AutoScalingGroup resource.
27
28    :ivar key: The key of the tag.
29    :ivar value: The value of the tag.
30    :ivar propagate_at_launch: Boolean value which specifies whether the
31        new tag will be applied to instances launched after the tag is created.
32    :ivar resource_id: The name of the autoscaling group.
33    :ivar resource_type: The only supported resource type at this time
34        is "auto-scaling-group".
35    """
36
37    def __init__(self, connection=None, key=None, value=None,
38                 propagate_at_launch=False, resource_id=None,
39                 resource_type='auto-scaling-group'):
40        self.connection = connection
41        self.key = key
42        self.value = value
43        self.propagate_at_launch = propagate_at_launch
44        self.resource_id = resource_id
45        self.resource_type = resource_type
46
47    def __repr__(self):
48        return 'Tag(%s=%s)' % (self.key, self.value)
49
50    def startElement(self, name, attrs, connection):
51        pass
52
53    def endElement(self, name, value, connection):
54        if name == 'Key':
55            self.key = value
56        elif name == 'Value':
57            self.value = value
58        elif name == 'PropagateAtLaunch':
59            if value.lower() == 'true':
60                self.propagate_at_launch = True
61            else:
62                self.propagate_at_launch = False
63        elif name == 'ResourceId':
64            self.resource_id = value
65        elif name == 'ResourceType':
66            self.resource_type = value
67
68    def build_params(self, params, i):
69        """
70        Populates a dictionary with the name/value pairs necessary
71        to identify this Tag in a request.
72        """
73        prefix = 'Tags.member.%d.' % i
74        params[prefix + 'ResourceId'] = self.resource_id
75        params[prefix + 'ResourceType'] = self.resource_type
76        params[prefix + 'Key'] = self.key
77        params[prefix + 'Value'] = self.value
78        if self.propagate_at_launch:
79            params[prefix + 'PropagateAtLaunch'] = 'true'
80        else:
81            params[prefix + 'PropagateAtLaunch'] = 'false'
82
83    def delete(self):
84        return self.connection.delete_tags([self])
85