forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.py
More file actions
70 lines (53 loc) · 2.19 KB
/
Copy pathvolume.py
File metadata and controls
70 lines (53 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import absolute_import
from linode.errors import UnexpectedResponseError
from linode.objects import Base, Linode, Property, Region
class Volume(Base):
api_endpoint = '/volumes/{id}'
properties = {
'id': Property(identifier=True),
'created': Property(is_datetime=True),
'updated': Property(is_datetime=True),
'linode_id': Property(id_relationship=Linode),
'label': Property(mutable=True, filterable=True),
'size': Property(filterable=True),
'status': Property(filterable=True),
'region': Property(slug_relationship=Region),
}
def attach(self, to_linode, config=None):
"""
Attaches this Volume to the given Linode
"""
result = self._client.post('{}/attach'.format(Volume.api_endpoint), model=self,
data={
"linode_id": to_linode.id if issubclass(type(to_linode), Base) else to_linode,
"config": None if not config else config.id if issubclass(type(config), Base) else config,
})
if not 'id' in result:
raise UnexpectedResponseError('Unexpected response when attaching volume!', json=result)
self._populate(result)
return True
def detach(self):
"""
Detaches this Volume if it is attached
"""
self._client.post('{}/detach'.format(Volume.api_endpoint), model=self)
return True
def resize(self, size):
"""
Resizes this Volume
"""
result = self._client.post('{}/resize'.format(Volume.api_endpoint, model=self,
data={ "size": size }))
self._populate(result.json)
return True
def clone(self, label):
"""
Clones this volume to a new volume in the same region with the given label
:param label: The label for the new volume.
:returns: The new volume object.
"""
result = self._client.post('{}/clone'.format(Volume.api_endpoint),
model=self, data={'label': label})
if not 'id' in result:
raise UnexpectedResponseError('Unexpected response cloning volume!')
return Volume(self._client, result['id'], result)