~blamar/+junk/openstack-api-arrrg

« back to all changes in this revision

Viewing changes to vendor/boto/boto/ec2/volume.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 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
"""
 
23
Represents an EC2 Elastic Block Storage Volume
 
24
"""
 
25
from boto.ec2.ec2object import EC2Object
 
26
 
 
27
class Volume(EC2Object):
 
28
    
 
29
    def __init__(self, connection=None):
 
30
        EC2Object.__init__(self, connection)
 
31
        self.id = None
 
32
        self.create_time = None
 
33
        self.status = None
 
34
        self.size = None
 
35
        self.snapshot_id = None
 
36
        self.attach_data = None
 
37
        self.zone = None
 
38
 
 
39
    def __repr__(self):
 
40
        return 'Volume:%s' % self.id
 
41
 
 
42
    def startElement(self, name, attrs, connection):
 
43
        if name == 'attachmentSet':
 
44
            self.attach_data = AttachmentSet()
 
45
            return self.attach_data
 
46
        else:
 
47
            return None
 
48
 
 
49
    def endElement(self, name, value, connection):
 
50
        if name == 'volumeId':
 
51
            self.id = value
 
52
        elif name == 'createTime':
 
53
            self.create_time = value
 
54
        elif name == 'status':
 
55
            if value != '':
 
56
                self.status = value
 
57
        elif name == 'size':
 
58
            self.size = int(value)
 
59
        elif name == 'snapshotId':
 
60
            self.snapshot_id = value
 
61
        elif name == 'availabilityZone':
 
62
            self.zone = value
 
63
        else:
 
64
            setattr(self, name, value)
 
65
 
 
66
    def _update(self, updated):
 
67
        self.__dict__.update(updated.__dict__)
 
68
 
 
69
    def update(self):
 
70
        rs = self.connection.get_all_volumes([self.id])
 
71
        if len(rs) > 0:
 
72
            self._update(rs[0])
 
73
        return self.status
 
74
 
 
75
    def delete(self):
 
76
        """
 
77
        Delete this EBS volume.
 
78
 
 
79
        :rtype: bool
 
80
        :return: True if successful
 
81
        """
 
82
        return self.connection.delete_volume(self.id)
 
83
 
 
84
    def attach(self, instance_id, device):
 
85
        """
 
86
        Attach this EBS volume to an EC2 instance.
 
87
 
 
88
        :type instance_id: str
 
89
        :param instance_id: The ID of the EC2 instance to which it will
 
90
                            be attached.
 
91
 
 
92
        :type device: str
 
93
        :param device: The device on the instance through which the
 
94
                       volume will be exposted (e.g. /dev/sdh)
 
95
 
 
96
        :rtype: bool
 
97
        :return: True if successful
 
98
        """
 
99
        return self.connection.attach_volume(self.id, instance_id, device)
 
100
 
 
101
    def detach(self, force=False):
 
102
        """
 
103
        Detach this EBS volume from an EC2 instance.
 
104
 
 
105
        :type force: bool
 
106
        :param force: Forces detachment if the previous detachment attempt did
 
107
                      not occur cleanly.  This option can lead to data loss or
 
108
                      a corrupted file system. Use this option only as a last
 
109
                      resort to detach a volume from a failed instance. The
 
110
                      instance will not have an opportunity to flush file system
 
111
                      caches nor file system meta data. If you use this option,
 
112
                      you must perform file system check and repair procedures.
 
113
 
 
114
        :rtype: bool
 
115
        :return: True if successful
 
116
        """
 
117
        instance_id = None
 
118
        if self.attach_data:
 
119
            instance_id = self.attach_data.instance_id
 
120
        device = None
 
121
        if self.attach_data:
 
122
            device = self.attach_data.device
 
123
        return self.connection.detach_volume(self.id, instance_id, device, force)
 
124
 
 
125
    def create_snapshot(self, description=None):
 
126
        """
 
127
        Create a snapshot of this EBS Volume.
 
128
 
 
129
        :type description: str
 
130
        :param description: A description of the snapshot.  Limited to 256 characters.
 
131
        
 
132
        :rtype: bool
 
133
        :return: True if successful
 
134
        """
 
135
        return self.connection.create_snapshot(self.id, description)
 
136
 
 
137
    def volume_state(self):
 
138
        """
 
139
        Returns the state of the volume.  Same value as the status attribute.
 
140
        """
 
141
        return self.status
 
142
 
 
143
    def attachment_state(self):
 
144
        """
 
145
        Get the attachment state.
 
146
        """
 
147
        state = None
 
148
        if self.attach_data:
 
149
            state = self.attach_data.status
 
150
        return state
 
151
 
 
152
    def snapshots(self, owner=None, restorable_by=None):
 
153
        """
 
154
        Get all snapshots related to this volume.  Note that this requires
 
155
        that all available snapshots for the account be retrieved from EC2
 
156
        first and then the list is filtered client-side to contain only
 
157
        those for this volume.
 
158
 
 
159
        :type owner: str
 
160
        :param owner: If present, only the snapshots owned by the specified user
 
161
                      will be returned.  Valid values are:
 
162
                      self | amazon | AWS Account ID
 
163
 
 
164
        :type restorable_by: str
 
165
        :param restorable_by: If present, only the snapshots that are restorable
 
166
                              by the specified account id will be returned.
 
167
 
 
168
        :rtype: list of L{boto.ec2.snapshot.Snapshot}
 
169
        :return: The requested Snapshot objects
 
170
        
 
171
        """
 
172
        rs = self.connection.get_all_snapshots(owner=owner,
 
173
                                               restorable_by=restorable_by)
 
174
        mine = []
 
175
        for snap in rs:
 
176
            if snap.volume_id == self.id:
 
177
                mine.append(snap)
 
178
        return mine
 
179
 
 
180
class AttachmentSet(object):
 
181
    
 
182
    def __init__(self):
 
183
        self.id = None
 
184
        self.instance_id = None
 
185
        self.status = None
 
186
        self.attach_time = None
 
187
        self.device = None
 
188
 
 
189
    def __repr__(self):
 
190
        return 'AttachmentSet:%s' % self.id
 
191
 
 
192
    def startElement(self, name, attrs, connection):
 
193
        pass
 
194
    
 
195
    def endElement(self, name, value, connection):
 
196
        if name == 'volumeId':
 
197
            self.id = value
 
198
        elif name == 'instanceId':
 
199
            self.instance_id = value
 
200
        elif name == 'status':
 
201
            self.status = value
 
202
        elif name == 'attachTime':
 
203
            self.attach_time = value
 
204
        elif name == 'device':
 
205
            self.device = value
 
206
        else:
 
207
            setattr(self, name, value)
 
208