summaryrefslogtreecommitdiff
path: root/novaclient/v2/volumes.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/volumes.py')
-rw-r--r--novaclient/v2/volumes.py165
1 files changed, 165 insertions, 0 deletions
diff --git a/novaclient/v2/volumes.py b/novaclient/v2/volumes.py
new file mode 100644
index 0000000..9b9cf18
--- /dev/null
+++ b/novaclient/v2/volumes.py
@@ -0,0 +1,165 @@
1# Copyright 2011 Denali Systems, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16"""
17Volume interface (1.1 extension).
18"""
19
20import six
21from six.moves.urllib import parse
22
23from novaclient import base
24
25
26class Volume(base.Resource):
27 """
28 A volume is an extra block level storage to the OpenStack instances.
29 """
30 NAME_ATTR = 'display_name'
31
32 def __repr__(self):
33 return "<Volume: %s>" % self.id
34
35 def delete(self):
36 """
37 Delete this volume.
38 """
39 self.manager.delete(self)
40
41
42class VolumeManager(base.ManagerWithFind):
43 """
44 Manage :class:`Volume` resources.
45 """
46 resource_class = Volume
47
48 def create(self, size, snapshot_id=None, display_name=None,
49 display_description=None, volume_type=None,
50 availability_zone=None, imageRef=None):
51 """
52 Create a volume.
53
54 :param size: Size of volume in GB
55 :param snapshot_id: ID of the snapshot
56 :param display_name: Name of the volume
57 :param display_description: Description of the volume
58 :param volume_type: Type of volume
59 :param availability_zone: Availability Zone for volume
60 :rtype: :class:`Volume`
61 :param imageRef: reference to an image stored in glance
62 """
63 body = {'volume': {'size': size,
64 'snapshot_id': snapshot_id,
65 'display_name': display_name,
66 'display_description': display_description,
67 'volume_type': volume_type,
68 'availability_zone': availability_zone,
69 'imageRef': imageRef}}
70 return self._create('/volumes', body, 'volume')
71
72 def get(self, volume_id):
73 """
74 Get a volume.
75
76 :param volume_id: The ID of the volume to get.
77 :rtype: :class:`Volume`
78 """
79 return self._get("/volumes/%s" % volume_id, "volume")
80
81 def list(self, detailed=True, search_opts=None):
82 """
83 Get a list of all volumes.
84
85 :rtype: list of :class:`Volume`
86 """
87 search_opts = search_opts or {}
88
89 qparams = dict((k, v) for (k, v) in six.iteritems(search_opts) if v)
90
91 query_string = '?%s' % parse.urlencode(qparams) if qparams else ''
92
93 if detailed is True:
94 return self._list("/volumes/detail%s" % query_string, "volumes")
95 else:
96 return self._list("/volumes%s" % query_string, "volumes")
97
98 def delete(self, volume):
99 """
100 Delete a volume.
101
102 :param volume: The :class:`Volume` to delete.
103 """
104 self._delete("/volumes/%s" % base.getid(volume))
105
106 def create_server_volume(self, server_id, volume_id, device):
107 """
108 Attach a volume identified by the volume ID to the given server ID
109
110 :param server_id: The ID of the server
111 :param volume_id: The ID of the volume to attach.
112 :param device: The device name
113 :rtype: :class:`Volume`
114 """
115 body = {'volumeAttachment': {'volumeId': volume_id,
116 'device': device}}
117 return self._create("/servers/%s/os-volume_attachments" % server_id,
118 body, "volumeAttachment")
119
120 def update_server_volume(self, server_id, attachment_id, new_volume_id):
121 """
122 Update the volume identified by the attachment ID, that is attached to
123 the given server ID
124
125 :param server_id: The ID of the server
126 :param attachment_id: The ID of the attachment
127 :param new_volume_id: The ID of the new volume to attach
128 :rtype: :class:`Volume`
129 """
130 body = {'volumeAttachment': {'volumeId': new_volume_id}}
131 return self._update("/servers/%s/os-volume_attachments/%s" %
132 (server_id, attachment_id,),
133 body, "volumeAttachment")
134
135 def get_server_volume(self, server_id, attachment_id):
136 """
137 Get the volume identified by the attachment ID, that is attached to
138 the given server ID
139
140 :param server_id: The ID of the server
141 :param attachment_id: The ID of the attachment
142 :rtype: :class:`Volume`
143 """
144 return self._get("/servers/%s/os-volume_attachments/%s" % (server_id,
145 attachment_id,), "volumeAttachment")
146
147 def get_server_volumes(self, server_id):
148 """
149 Get a list of all the attached volumes for the given server ID
150
151 :param server_id: The ID of the server
152 :rtype: list of :class:`Volume`
153 """
154 return self._list("/servers/%s/os-volume_attachments" % server_id,
155 "volumeAttachments")
156
157 def delete_server_volume(self, server_id, attachment_id):
158 """
159 Detach a volume identified by the attachment ID from the given server
160
161 :param server_id: The ID of the server
162 :param attachment_id: The ID of the attachment
163 """
164 self._delete("/servers/%s/os-volume_attachments/%s" %
165 (server_id, attachment_id,))