OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006-2010 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2010, Eucalyptus Systems, Inc. |
| 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 """ |
| 24 Represents an EC2 Object |
| 25 """ |
| 26 from boto.ec2.tag import TagSet |
| 27 |
| 28 class EC2Object(object): |
| 29 |
| 30 def __init__(self, connection=None): |
| 31 self.connection = connection |
| 32 if self.connection and hasattr(self.connection, 'region'): |
| 33 self.region = connection.region |
| 34 else: |
| 35 self.region = None |
| 36 |
| 37 def startElement(self, name, attrs, connection): |
| 38 return None |
| 39 |
| 40 def endElement(self, name, value, connection): |
| 41 setattr(self, name, value) |
| 42 |
| 43 |
| 44 class TaggedEC2Object(EC2Object): |
| 45 """ |
| 46 Any EC2 resource that can be tagged should be represented |
| 47 by a Python object that subclasses this class. This class |
| 48 has the mechanism in place to handle the tagSet element in |
| 49 the Describe* responses. If tags are found, it will create |
| 50 a TagSet object and allow it to parse and collect the tags |
| 51 into a dict that is stored in the "tags" attribute of the |
| 52 object. |
| 53 """ |
| 54 |
| 55 def __init__(self, connection=None): |
| 56 EC2Object.__init__(self, connection) |
| 57 self.tags = TagSet() |
| 58 |
| 59 def startElement(self, name, attrs, connection): |
| 60 if name == 'tagSet': |
| 61 return self.tags |
| 62 else: |
| 63 return None |
| 64 |
| 65 def add_tag(self, key, value=''): |
| 66 """ |
| 67 Add a tag to this object. Tag's are stored by AWS and can be used |
| 68 to organize and filter resources. Adding a tag involves a round-trip |
| 69 to the EC2 service. |
| 70 |
| 71 :type key: str |
| 72 :param key: The key or name of the tag being stored. |
| 73 |
| 74 :type value: str |
| 75 :param value: An optional value that can be stored with the tag. |
| 76 If you want only the tag name and no value, the |
| 77 value should be the empty string. |
| 78 """ |
| 79 status = self.connection.create_tags([self.id], {key : value}) |
| 80 if self.tags is None: |
| 81 self.tags = TagSet() |
| 82 self.tags[key] = value |
| 83 |
| 84 def remove_tag(self, key, value=None): |
| 85 """ |
| 86 Remove a tag from this object. Removing a tag involves a round-trip |
| 87 to the EC2 service. |
| 88 |
| 89 :type key: str |
| 90 :param key: The key or name of the tag being stored. |
| 91 |
| 92 :type value: str |
| 93 :param value: An optional value that can be stored with the tag. |
| 94 If a value is provided, it must match the value |
| 95 currently stored in EC2. If not, the tag will not |
| 96 be removed. If a value of None is provided, all |
| 97 tags with the specified name will be deleted. |
| 98 NOTE: There is an important distinction between |
| 99 a value of '' and a value of None. |
| 100 """ |
| 101 if value: |
| 102 tags = {key : value} |
| 103 else: |
| 104 tags = [key] |
| 105 status = self.connection.delete_tags([self.id], tags) |
| 106 if key in self.tags: |
| 107 del self.tags[key] |
OLD | NEW |