PageRenderTime 93ms CodeModel.GetById 85ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Python | 126 lines | 88 code | 6 blank | 32 comment | 1 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
 23class BlockDeviceType(object):
 24    """
 25    Represents parameters for a block device.
 26    """
 27
 28    def __init__(self,
 29                 connection=None,
 30                 ephemeral_name=None,
 31                 no_device=False,
 32                 volume_id=None,
 33                 snapshot_id=None,
 34                 status=None,
 35                 attach_time=None,
 36                 delete_on_termination=False,
 37                 size=None):
 38        self.connection = connection
 39        self.ephemeral_name = ephemeral_name
 40        self.no_device = no_device
 41        self.volume_id = volume_id
 42        self.snapshot_id = snapshot_id
 43        self.status = status
 44        self.attach_time = attach_time
 45        self.delete_on_termination = delete_on_termination
 46        self.size = size
 47
 48    def startElement(self, name, attrs, connection):
 49        pass
 50
 51    def endElement(self, name, value, connection):
 52        if name =='volumeId':
 53            self.volume_id = value
 54        elif name == 'virtualName':
 55            self.ephemeral_name = value
 56        elif name =='NoDevice':
 57            self.no_device = (value == 'true')
 58        elif name =='snapshotId':
 59            self.snapshot_id = value
 60        elif name == 'volumeSize':
 61            self.size = int(value)
 62        elif name == 'status':
 63            self.status = value
 64        elif name == 'attachTime':
 65            self.attach_time = value
 66        elif name == 'deleteOnTermination':
 67            self.delete_on_termination = (value == 'true')
 68        else:
 69            setattr(self, name, value)
 70
 71# for backwards compatibility
 72EBSBlockDeviceType = BlockDeviceType
 73
 74class BlockDeviceMapping(dict):
 75    """
 76    Represents a collection of BlockDeviceTypes when creating ec2 instances.
 77
 78    Example: 
 79    dev_sda1 = BlockDeviceType()
 80    dev_sda1.size = 100   # change root volume to 100GB instead of default for ami
 81    bdm = BlockDeviceMapping()
 82    bdm['/dev/sda1'] = dev_sda1
 83    reservation = image.run(..., block_device_map=bdm, ...)
 84    """
 85
 86    def __init__(self, connection=None):
 87        """
 88        :type connection: :class:`boto.ec2.EC2Connection`
 89        :param connection: Optional connection.
 90        """
 91        dict.__init__(self)
 92        self.connection = connection
 93        self.current_name = None
 94        self.current_value = None
 95
 96    def startElement(self, name, attrs, connection):
 97        if name == 'ebs' or name == 'virtualName':
 98            self.current_value = BlockDeviceType(self)
 99            return self.current_value
100
101    def endElement(self, name, value, connection):
102        if name == 'device' or name == 'deviceName':
103            self.current_name = value
104        elif name == 'item':
105            self[self.current_name] = self.current_value
106
107    def build_list_params(self, params, prefix=''):
108        i = 1
109        for dev_name in self:
110            pre = '%sBlockDeviceMapping.%d' % (prefix, i)
111            params['%s.DeviceName' % pre] = dev_name
112            block_dev = self[dev_name]
113            if block_dev.ephemeral_name:
114                params['%s.VirtualName' % pre] = block_dev.ephemeral_name
115            else:
116                if block_dev.no_device:
117                    params['%s.Ebs.NoDevice' % pre] = 'true'
118                if block_dev.snapshot_id:
119                    params['%s.Ebs.SnapshotId' % pre] = block_dev.snapshot_id
120                if block_dev.size:
121                    params['%s.Ebs.VolumeSize' % pre] = block_dev.size
122                if block_dev.delete_on_termination:
123                    params['%s.Ebs.DeleteOnTermination' % pre] = 'true'
124                else:
125                    params['%s.Ebs.DeleteOnTermination' % pre] = 'false'
126            i += 1