PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/ec2/ec2object.py

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