PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/ec2/blockdevicemapping.py

#
Python | 126 lines | 88 code | 6 blank | 32 comment | 9 complexity | 672513bb9f2eac1b106ece5113911f32 MD5 | raw file
  1. # Copyright (c) 2009 Mitch Garnaat http://garnaat.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. #
  22. class BlockDeviceType(object):
  23. """
  24. Represents parameters for a block device.
  25. """
  26. def __init__(self,
  27. connection=None,
  28. ephemeral_name=None,
  29. no_device=False,
  30. volume_id=None,
  31. snapshot_id=None,
  32. status=None,
  33. attach_time=None,
  34. delete_on_termination=False,
  35. size=None):
  36. self.connection = connection
  37. self.ephemeral_name = ephemeral_name
  38. self.no_device = no_device
  39. self.volume_id = volume_id
  40. self.snapshot_id = snapshot_id
  41. self.status = status
  42. self.attach_time = attach_time
  43. self.delete_on_termination = delete_on_termination
  44. self.size = size
  45. def startElement(self, name, attrs, connection):
  46. pass
  47. def endElement(self, name, value, connection):
  48. if name =='volumeId':
  49. self.volume_id = value
  50. elif name == 'virtualName':
  51. self.ephemeral_name = value
  52. elif name =='NoDevice':
  53. self.no_device = (value == 'true')
  54. elif name =='snapshotId':
  55. self.snapshot_id = value
  56. elif name == 'volumeSize':
  57. self.size = int(value)
  58. elif name == 'status':
  59. self.status = value
  60. elif name == 'attachTime':
  61. self.attach_time = value
  62. elif name == 'deleteOnTermination':
  63. self.delete_on_termination = (value == 'true')
  64. else:
  65. setattr(self, name, value)
  66. # for backwards compatibility
  67. EBSBlockDeviceType = BlockDeviceType
  68. class BlockDeviceMapping(dict):
  69. """
  70. Represents a collection of BlockDeviceTypes when creating ec2 instances.
  71. Example:
  72. dev_sda1 = BlockDeviceType()
  73. dev_sda1.size = 100 # change root volume to 100GB instead of default for ami
  74. bdm = BlockDeviceMapping()
  75. bdm['/dev/sda1'] = dev_sda1
  76. reservation = image.run(..., block_device_map=bdm, ...)
  77. """
  78. def __init__(self, connection=None):
  79. """
  80. :type connection: :class:`boto.ec2.EC2Connection`
  81. :param connection: Optional connection.
  82. """
  83. dict.__init__(self)
  84. self.connection = connection
  85. self.current_name = None
  86. self.current_value = None
  87. def startElement(self, name, attrs, connection):
  88. if name == 'ebs' or name == 'virtualName':
  89. self.current_value = BlockDeviceType(self)
  90. return self.current_value
  91. def endElement(self, name, value, connection):
  92. if name == 'device' or name == 'deviceName':
  93. self.current_name = value
  94. elif name == 'item':
  95. self[self.current_name] = self.current_value
  96. def build_list_params(self, params, prefix=''):
  97. i = 1
  98. for dev_name in self:
  99. pre = '%sBlockDeviceMapping.%d' % (prefix, i)
  100. params['%s.DeviceName' % pre] = dev_name
  101. block_dev = self[dev_name]
  102. if block_dev.ephemeral_name:
  103. params['%s.VirtualName' % pre] = block_dev.ephemeral_name
  104. else:
  105. if block_dev.no_device:
  106. params['%s.Ebs.NoDevice' % pre] = 'true'
  107. if block_dev.snapshot_id:
  108. params['%s.Ebs.SnapshotId' % pre] = block_dev.snapshot_id
  109. if block_dev.size:
  110. params['%s.Ebs.VolumeSize' % pre] = block_dev.size
  111. if block_dev.delete_on_termination:
  112. params['%s.Ebs.DeleteOnTermination' % pre] = 'true'
  113. else:
  114. params['%s.Ebs.DeleteOnTermination' % pre] = 'false'
  115. i += 1