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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# Copyright (c) 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Volume interface (v3 extension)."""
import warnings
from cinderclient import api_versions
from cinderclient.apiclient import base as common_base
from cinderclient import base
from cinderclient.v2 import volumes
class Volume(volumes.Volume):
def upload_to_image(self, force, image_name, container_format,
disk_format, visibility=None,
protected=None):
"""Upload a volume to image service as an image.
:param force: Boolean to enables or disables upload of a volume that
is attached to an instance.
:param image_name: The new image name.
:param container_format: Container format type.
:param disk_format: Disk format type.
:param visibility: The accessibility of image (allowed for
3.1-latest).
:param protected: Boolean to decide whether prevents image from being
deleted (allowed for 3.1-latest).
"""
if self.manager.api_version >= api_versions.APIVersion("3.1"):
visibility = 'private' if visibility is None else visibility
protected = False if protected is None else protected
return self.manager.upload_to_image(self, force, image_name,
container_format, disk_format,
visibility, protected)
return self.manager.upload_to_image(self, force, image_name,
container_format, disk_format)
def revert_to_snapshot(self, snapshot):
"""Revert a volume to a snapshot."""
self.manager.revert_to_snapshot(self, snapshot)
def migrate_volume(self, host, force_host_copy, lock_volume, cluster=None):
"""Migrate the volume to a new host."""
return self.manager.migrate_volume(self, host, force_host_copy,
lock_volume, cluster)
def manage(self, host, ref, name=None, description=None,
volume_type=None, availability_zone=None, metadata=None,
bootable=False, cluster=None):
"""Manage an existing volume."""
return self.manager.manage(host=host, ref=ref, name=name,
description=description,
volume_type=volume_type,
availability_zone=availability_zone,
metadata=metadata, bootable=bootable,
cluster=cluster)
class VolumeManager(volumes.VolumeManager):
resource_class = Volume
def create(self, size, consistencygroup_id=None,
group_id=None, snapshot_id=None,
source_volid=None, name=None, description=None,
volume_type=None, user_id=None,
project_id=None, availability_zone=None,
metadata=None, imageRef=None, scheduler_hints=None,
multiattach=False, backup_id=None):
"""Create a volume.
:param size: Size of volume in GB
:param consistencygroup_id: ID of the consistencygroup
:param group_id: ID of the group
:param snapshot_id: ID of the snapshot
:param name: Name of the volume
:param description: Description of the volume
:param volume_type: Type of volume
:param user_id: User id derived from context (IGNORED)
:param project_id: Project id derived from context (IGNORED)
:param availability_zone: Availability Zone to use
:param metadata: Optional metadata to set on volume creation
:param imageRef: reference to an image stored in glance
:param source_volid: ID of source volume to clone from
:param scheduler_hints: (optional extension) arbitrary key-value pairs
specified by the client to help boot an instance
:param multiattach: Allow the volume to be attached to more than
one instance (deprecated)
:param backup_id: ID of the backup
:rtype: :class:`Volume`
"""
if metadata is None:
volume_metadata = {}
else:
volume_metadata = metadata
if multiattach:
warnings.warn('The ``multiattach`` volume create flag is '
'deprecated and will be removed in a future '
'release. Multiattach capability is now controlled '
'using volume type extra specs.',
DeprecationWarning)
body = {'volume': {'size': size,
'consistencygroup_id': consistencygroup_id,
'snapshot_id': snapshot_id,
'name': name,
'description': description,
'volume_type': volume_type,
'availability_zone': availability_zone,
'metadata': volume_metadata,
'imageRef': imageRef,
'source_volid': source_volid,
'multiattach': multiattach,
'backup_id': backup_id
}}
if group_id:
body['volume']['group_id'] = group_id
if scheduler_hints:
body['OS-SCH-HNT:scheduler_hints'] = scheduler_hints
return self._create('/volumes', body, 'volume')
@api_versions.wraps('3.40')
def revert_to_snapshot(self, volume, snapshot):
"""Revert a volume to a snapshot.
The snapshot must be the most recent one known to cinder.
:param volume: volume object.
:param snapshot: snapshot object.
"""
return self._action('revert', volume,
info={'snapshot_id': base.getid(snapshot.id)})
@api_versions.wraps('3.12')
def summary(self, all_tenants):
"""Get volumes summary."""
url = "/volumes/summary"
if all_tenants:
url += "?all_tenants=True"
_, body = self.api.client.get(url)
return body
@api_versions.wraps("3.0")
def delete_metadata(self, volume, keys):
"""Delete specified keys from volumes metadata.
:param volume: The :class:`Volume`.
:param keys: A list of keys to be removed.
"""
response_list = []
for k in keys:
resp, body = self._delete("/volumes/%s/metadata/%s" %
(base.getid(volume), k))
response_list.append(resp)
return common_base.ListWithMeta([], response_list)
@api_versions.wraps("3.15") # noqa: F811
def delete_metadata(self, volume, keys):
"""Delete specified keys from volumes metadata.
:param volume: The :class:`Volume`.
:param keys: A list of keys to be removed.
"""
# pylint: disable=function-redefined
data = self._get("/volumes/%s/metadata" % base.getid(volume))
metadata = data._info.get("metadata", {})
if set(keys).issubset(metadata.keys()):
for k in keys:
metadata.pop(k)
body = {'metadata': metadata}
kwargs = {'headers': {'If-Match': data._checksum}}
return self._update("/volumes/%s/metadata" % base.getid(volume),
body, **kwargs)
@api_versions.wraps("3.0")
def upload_to_image(self, volume, force, image_name, container_format,
disk_format):
"""Upload volume to image service as image.
:param volume: The :class:`Volume` to upload.
"""
return self._action('os-volume_upload_image',
volume,
{'force': force,
'image_name': image_name,
'container_format': container_format,
'disk_format': disk_format})
@api_versions.wraps("3.1") # noqa: F811
def upload_to_image(self, volume, force, image_name, container_format,
disk_format, visibility, protected):
"""Upload volume to image service as image.
:param volume: The :class:`Volume` to upload.
"""
# pylint: disable=function-redefined
return self._action('os-volume_upload_image',
volume,
{'force': force,
'image_name': image_name,
'container_format': container_format,
'disk_format': disk_format,
'visibility': visibility,
'protected': protected})
def migrate_volume(self, volume, host, force_host_copy, lock_volume,
cluster=None):
"""Migrate volume to new backend.
The new backend is defined by the host or the cluster (not both).
:param volume: The :class:`Volume` to migrate
:param host: The destination host
:param force_host_copy: Skip driver optimizations
:param lock_volume: Lock the volume and guarantee the migration
to finish
:param cluster: The cluster
"""
body = {'host': host, 'force_host_copy': force_host_copy,
'lock_volume': lock_volume}
if self.api_version.matches('3.16'):
if cluster:
body['cluster'] = cluster
del body['host']
return self._action('os-migrate_volume', volume, body)
def manage(self, host, ref, name=None, description=None,
volume_type=None, availability_zone=None, metadata=None,
bootable=False, cluster=None):
"""Manage an existing volume."""
body = {'volume': {'host': host,
'ref': ref,
'name': name,
'description': description,
'volume_type': volume_type,
'availability_zone': availability_zone,
'metadata': metadata,
'bootable': bootable
}}
if self.api_version.matches('3.16') and cluster:
body['volume']['cluster'] = cluster
return self._create('/os-volume-manage', body, 'volume')
@api_versions.wraps('3.8')
def list_manageable(self, host, detailed=True, marker=None, limit=None,
offset=None, sort=None, cluster=None):
search_opts = {'cluster': cluster} if cluster else {'host': host}
url = self._build_list_url("manageable_volumes", detailed=detailed,
search_opts=search_opts, marker=marker,
limit=limit, offset=offset, sort=sort)
return self._list(url, "manageable-volumes")
@api_versions.wraps("2.0", "3.32")
def get_pools(self, detail):
"""Show pool information for backends."""
query_string = ""
if detail:
query_string = "?detail=True"
return self._get('/scheduler-stats/get_pools%s' % query_string, None)
@api_versions.wraps("3.33") # noqa: F811
def get_pools(self, detail, search_opts):
"""Show pool information for backends."""
# pylint: disable=function-redefined
options = {'detail': detail}
options.update(search_opts)
url = self._build_list_url('scheduler-stats/get_pools', detailed=False,
search_opts=options)
return self._get(url, None)
|