Debian Codegraph @

seamicroclient/v2/volumes.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 """
14 Volume interface.
15 """
16
17 import binascii
18 import os
19
20 from seamicroclient import base
21
22
23 class Volume(base.Resource):
24 HUMAN_ID = True
25
26 def delete(self):
27 self.manager.delete(self)
28
29
30 class VolumeManager(base.ManagerWithFind):
31 resource_class = Volume
32
33 def get(self, volume):
34 """
35 Get a volume.
36
37 :param volume: ID of the :class:`Volume` to get.
38 :rtype: :class:`Volume`
39 """
40 return self._get(base.getid(volume),
41 "/storage/volumes/%s" % base.getid(volume))
42
43 def list(self):
44 """
45 Get a list of volumes.
46
47 :rtype: list of :class:`Volume`
48 """
49 return self._list("/storage/volumes")
50
51 def create(self, size, pool, volume_id=None, **kwargs):
52 """
53 Create a volume of the given size in the given pool.
54
55 :param volume_id: ID of the :class: `Volume` to create
56 :param pool: Object of the :class: `Pool` in which the volume will be
57 created.
58 :param size: Size of the new volume in GB.
59 """
60 create_params = {}
61 if volume_id is None:
62 volume_id = str(binascii.b2a_hex(os.urandom(6)))
63 if pool and volume_id and size:
64 create_params = {'volume-size': str(size)}
65 resource_url = "%s/%s" % (base.getid(pool), volume_id)
66 return self._create(resource_url, create_params)
67
68 def delete(self, volume):
69 self._delete("/storage/volume/%s" % base.getid(volume))
70
71 def _create(self, resource_url, body, **kwargs):
72 """
73 Create a volume
74 """
75 body.update({"action": "create"})
76 self.run_hooks('modify_body_for_action', body, **kwargs)
77 url = '/storage/volumes/%s' % resource_url
78 return self._update(url, body=body)
79
80 def _action(self, action, volume, info=None, **kwargs):
81 """
82 Perform a volume "action" -- .
83 """
84 body = {"action": action}
85 body.update(info)
86 self.run_hooks('modify_body_for_action', body, **kwargs)
87 url = '/storage/volumes/%s' % base.getid(volume)
88 return self.api.client.put(url, body=body)