blob: 9799e55227ac761ebf8790b821b89fd9d5b66156 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Matthew Treinish9854d5b2012-09-20 10:22:13 -04002# 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
16import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Matthew Treinish9854d5b2012-09-20 10:22:13 -040018
19from lxml import etree
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000020from xml.sax import saxutils
Matthew Treinish9854d5b2012-09-20 10:22:13 -040021
vponomaryov960eeb42014-02-22 18:25:25 +020022from tempest.common import rest_client
Matthew Treinish28f164c2014-03-04 18:55:06 +000023from tempest.common import xml_utils as common
Matthew Treinish684d8992014-01-30 16:27:40 +000024from tempest import config
Matthew Treinish9854d5b2012-09-20 10:22:13 -040025from tempest import exceptions
Matthew Treinish9854d5b2012-09-20 10:22:13 -040026
Matthew Treinish684d8992014-01-30 16:27:40 +000027CONF = config.CONF
28
Attila Fazekasd38f7162014-03-05 19:28:40 +010029VOLUME_NS_BASE = 'http://docs.openstack.org/volume/ext/'
30VOLUME_HOST_NS = VOLUME_NS_BASE + 'volume_host_attribute/api/v1'
31VOLUME_MIG_STATUS_NS = VOLUME_NS_BASE + 'volume_mig_status_attribute/api/v1'
32VOLUMES_TENANT_NS = VOLUME_NS_BASE + 'volume_tenant_attribute/api/v1'
33
Matthew Treinish9854d5b2012-09-20 10:22:13 -040034
vponomaryov960eeb42014-02-22 18:25:25 +020035class VolumesClientXML(rest_client.RestClient):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040036 """
37 Client class to send CRUD Volume API requests to a Cinder endpoint
38 """
vponomaryov960eeb42014-02-22 18:25:25 +020039 TYPE = "xml"
Matthew Treinish9854d5b2012-09-20 10:22:13 -040040
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000041 def __init__(self, auth_provider):
42 super(VolumesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000043 self.service = CONF.volume.catalog_type
44 self.build_interval = CONF.compute.build_interval
45 self.build_timeout = CONF.compute.build_timeout
Matthew Treinish9854d5b2012-09-20 10:22:13 -040046
Attila Fazekasd38f7162014-03-05 19:28:40 +010047 def _translate_attributes_to_json(self, volume):
48 volume_host_attr = '{' + VOLUME_HOST_NS + '}host'
49 volume_mig_stat_attr = '{' + VOLUME_MIG_STATUS_NS + '}migstat'
50 volume_mig_name_attr = '{' + VOLUME_MIG_STATUS_NS + '}name_id'
51 volume_tenant_id_attr = '{' + VOLUMES_TENANT_NS + '}tenant_id'
52 if volume_host_attr in volume:
53 volume['os-vol-host-attr:host'] = volume.pop(volume_host_attr)
54 if volume_mig_stat_attr in volume:
55 volume['os-vol-mig-status-attr:migstat'] = volume.pop(
56 volume_mig_stat_attr)
57 if volume_mig_name_attr in volume:
58 volume['os-vol-mig-status-attr:name_id'] = volume.pop(
59 volume_mig_name_attr)
60 if volume_tenant_id_attr in volume:
61 volume['os-vol-tenant-attr:tenant_id'] = volume.pop(
62 volume_tenant_id_attr)
63
Matthew Treinish9854d5b2012-09-20 10:22:13 -040064 def _parse_volume(self, body):
65 vol = dict((attr, body.get(attr)) for attr in body.keys())
66
67 for child in body.getchildren():
68 tag = child.tag
69 if tag.startswith("{"):
70 ns, tag = tag.split("}", 1)
71 if tag == 'metadata':
72 vol['metadata'] = dict((meta.get('key'),
Attila Fazekas786236c2013-01-31 16:06:51 +010073 meta.text) for meta in
74 child.getchildren())
Matthew Treinish9854d5b2012-09-20 10:22:13 -040075 else:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000076 vol[tag] = common.xml_to_json(child)
Attila Fazekasd38f7162014-03-05 19:28:40 +010077 self._translate_attributes_to_json(vol)
78 self._check_if_bootable(vol)
Attila Fazekas786236c2013-01-31 16:06:51 +010079 return vol
Matthew Treinish9854d5b2012-09-20 10:22:13 -040080
anju tiwari789449a2013-08-29 16:56:17 +053081 def get_attachment_from_volume(self, volume):
82 """Return the element 'attachment' from input volumes."""
83 return volume['attachments']['attachment']
84
Nayna Patel5e76be12013-08-19 12:10:16 +000085 def _check_if_bootable(self, volume):
86 """
87 Check if the volume is bootable, also change the value
88 of 'bootable' from string to boolean.
89 """
John Griffithf55f69e2013-09-19 14:10:57 -060090
91 # NOTE(jdg): Version 1 of Cinder API uses lc strings
92 # We should consider being explicit in this check to
93 # avoid introducing bugs like: LP #1227837
94
95 if volume['bootable'].lower() == 'true':
Nayna Patel5e76be12013-08-19 12:10:16 +000096 volume['bootable'] = True
John Griffithf55f69e2013-09-19 14:10:57 -060097 elif volume['bootable'].lower() == 'false':
Nayna Patel5e76be12013-08-19 12:10:16 +000098 volume['bootable'] = False
99 else:
100 raise ValueError(
101 'bootable flag is supposed to be either True or False,'
102 'it is %s' % volume['bootable'])
103 return volume
104
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400105 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500106 """List all the volumes created."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400107 url = 'volumes'
108
109 if params:
110 url += '?%s' % urllib.urlencode(params)
111
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200112 resp, body = self.get(url)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400113 body = etree.fromstring(body)
114 volumes = []
115 if body is not None:
116 volumes += [self._parse_volume(vol) for vol in list(body)]
117 return resp, volumes
118
119 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500120 """List all the details of volumes."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400121 url = 'volumes/detail'
122
123 if params:
124 url += '?%s' % urllib.urlencode(params)
125
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200126 resp, body = self.get(url)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400127 body = etree.fromstring(body)
128 volumes = []
129 if body is not None:
130 volumes += [self._parse_volume(vol) for vol in list(body)]
131 return resp, volumes
132
Attila Fazekasb8aa7592013-01-26 01:25:45 +0100133 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500134 """Returns the details of a single volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400135 url = "volumes/%s" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200136 resp, body = self.get(url)
Nayna Patel5e76be12013-08-19 12:10:16 +0000137 body = self._parse_volume(etree.fromstring(body))
Nayna Patel5e76be12013-08-19 12:10:16 +0000138 return resp, body
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400139
Jerry Cai9733d0e2014-03-19 15:50:49 +0800140 def create_volume(self, size=None, **kwargs):
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400141 """Creates a new Volume.
142
Jerry Cai9733d0e2014-03-19 15:50:49 +0800143 :param size: Size of volume in GB.
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400144 :param display_name: Optional Volume Name.
145 :param metadata: An optional dictionary of values for metadata.
Attila Fazekas786236c2013-01-31 16:06:51 +0100146 :param volume_type: Optional Name of volume_type for the volume
147 :param snapshot_id: When specified the volume is created from
148 this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +0200149 :param imageRef: When specified the volume is created from this
150 image
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400151 """
Jerry Cai9733d0e2014-03-19 15:50:49 +0800152 # for bug #1293885:
153 # If no size specified, read volume size from CONF
154 if size is None:
155 size = CONF.volume.volume_size
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200156 # NOTE(afazekas): it should use a volume namespace
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000157 volume = common.Element("volume", xmlns=common.XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400158
Attila Fazekas786236c2013-01-31 16:06:51 +0100159 if 'metadata' in kwargs:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000160 _metadata = common.Element('metadata')
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400161 volume.append(_metadata)
Attila Fazekas786236c2013-01-31 16:06:51 +0100162 for key, value in kwargs['metadata'].items():
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000163 meta = common.Element('meta')
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400164 meta.add_attr('key', key)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000165 meta.append(common.Text(value))
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400166 _metadata.append(meta)
Attila Fazekas786236c2013-01-31 16:06:51 +0100167 attr_to_add = kwargs.copy()
168 del attr_to_add['metadata']
169 else:
170 attr_to_add = kwargs
171
172 for key, value in attr_to_add.items():
173 volume.add_attr(key, value)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400174
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000175 resp, body = self.post('volumes', str(common.Document(volume)))
176 body = common.xml_to_json(etree.fromstring(body))
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400177 return resp, body
178
QingXin Meng611768a2013-09-18 00:51:33 -0700179 def update_volume(self, volume_id, **kwargs):
180 """Updates the Specified Volume."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000181 put_body = common.Element("volume", xmlns=common.XMLNS_11, **kwargs)
QingXin Meng611768a2013-09-18 00:51:33 -0700182
183 resp, body = self.put('volumes/%s' % volume_id,
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000184 str(common.Document(put_body)))
185 body = common.xml_to_json(etree.fromstring(body))
QingXin Meng611768a2013-09-18 00:51:33 -0700186 return resp, body
187
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400188 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500189 """Deletes the Specified Volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400190 return self.delete("volumes/%s" % str(volume_id))
191
192 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500193 """Waits for a Volume to reach a given status."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400194 resp, body = self.get_volume(volume_id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400195 volume_status = body['status']
196 start = int(time.time())
197
198 while volume_status != status:
199 time.sleep(self.build_interval)
200 resp, body = self.get_volume(volume_id)
201 volume_status = body['status']
202 if volume_status == 'error':
203 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
204
205 if int(time.time()) - start >= self.build_timeout:
206 message = 'Volume %s failed to reach %s status within '\
Attila Fazekas786236c2013-01-31 16:06:51 +0100207 'the required time (%s s).' % (volume_id,
208 status,
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400209 self.build_timeout)
210 raise exceptions.TimeoutException(message)
211
212 def is_resource_deleted(self, id):
213 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100214 self.get_volume(id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400215 except exceptions.NotFound:
216 return True
217 return False
anju tiwari789449a2013-08-29 16:56:17 +0530218
219 def attach_volume(self, volume_id, instance_uuid, mountpoint):
220 """Attaches a volume to a given instance on a given mountpoint."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000221 post_body = common.Element("os-attach",
222 instance_uuid=instance_uuid,
223 mountpoint=mountpoint
224 )
anju tiwari789449a2013-08-29 16:56:17 +0530225 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000226 resp, body = self.post(url, str(common.Document(post_body)))
anju tiwari789449a2013-08-29 16:56:17 +0530227 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000228 body = common.xml_to_json(etree.fromstring(body))
anju tiwari789449a2013-08-29 16:56:17 +0530229 return resp, body
230
231 def detach_volume(self, volume_id):
232 """Detaches a volume from an instance."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000233 post_body = common.Element("os-detach")
anju tiwari789449a2013-08-29 16:56:17 +0530234 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000235 resp, body = self.post(url, str(common.Document(post_body)))
anju tiwari789449a2013-08-29 16:56:17 +0530236 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000237 body = common.xml_to_json(etree.fromstring(body))
anju tiwari789449a2013-08-29 16:56:17 +0530238 return resp, body
239
Ryan Hsua67f4632013-08-29 16:03:06 -0700240 def upload_volume(self, volume_id, image_name, disk_format):
anju tiwari789449a2013-08-29 16:56:17 +0530241 """Uploads a volume in Glance."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000242 post_body = common.Element("os-volume_upload_image",
243 image_name=image_name,
244 disk_format=disk_format)
anju tiwari789449a2013-08-29 16:56:17 +0530245 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000246 resp, body = self.post(url, str(common.Document(post_body)))
247 volume = common.xml_to_json(etree.fromstring(body))
anju tiwari789449a2013-08-29 16:56:17 +0530248 return resp, volume
wanghao5b981752013-10-22 11:41:41 +0800249
250 def extend_volume(self, volume_id, extend_size):
251 """Extend a volume."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000252 post_body = common.Element("os-extend",
253 new_size=extend_size)
wanghao5b981752013-10-22 11:41:41 +0800254 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000255 resp, body = self.post(url, str(common.Document(post_body)))
wanghao5b981752013-10-22 11:41:41 +0800256 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000257 body = common.xml_to_json(etree.fromstring(body))
wanghao5b981752013-10-22 11:41:41 +0800258 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800259
260 def reset_volume_status(self, volume_id, status):
261 """Reset the Specified Volume's Status."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000262 post_body = common.Element("os-reset_status",
263 status=status
264 )
wanghaoaa1f2f92013-10-10 11:30:37 +0800265 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000266 resp, body = self.post(url, str(common.Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800267 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000268 body = common.xml_to_json(etree.fromstring(body))
wanghaoaa1f2f92013-10-10 11:30:37 +0800269 return resp, body
270
271 def volume_begin_detaching(self, volume_id):
272 """Volume Begin Detaching."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000273 post_body = common.Element("os-begin_detaching")
wanghaoaa1f2f92013-10-10 11:30:37 +0800274 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000275 resp, body = self.post(url, str(common.Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800276 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000277 body = common.xml_to_json(etree.fromstring(body))
wanghaoaa1f2f92013-10-10 11:30:37 +0800278 return resp, body
279
280 def volume_roll_detaching(self, volume_id):
281 """Volume Roll Detaching."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000282 post_body = common.Element("os-roll_detaching")
wanghaoaa1f2f92013-10-10 11:30:37 +0800283 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000284 resp, body = self.post(url, str(common.Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800285 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000286 body = common.xml_to_json(etree.fromstring(body))
wanghaoaa1f2f92013-10-10 11:30:37 +0800287 return resp, body
zhangyanzi6b632432013-10-24 19:08:50 +0800288
289 def reserve_volume(self, volume_id):
290 """Reserves a volume."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000291 post_body = common.Element("os-reserve")
zhangyanzi6b632432013-10-24 19:08:50 +0800292 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000293 resp, body = self.post(url, str(common.Document(post_body)))
zhangyanzi6b632432013-10-24 19:08:50 +0800294 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000295 body = common.xml_to_json(etree.fromstring(body))
zhangyanzi6b632432013-10-24 19:08:50 +0800296 return resp, body
297
298 def unreserve_volume(self, volume_id):
299 """Restore a reserved volume ."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000300 post_body = common.Element("os-unreserve")
zhangyanzi6b632432013-10-24 19:08:50 +0800301 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000302 resp, body = self.post(url, str(common.Document(post_body)))
zhangyanzi6b632432013-10-24 19:08:50 +0800303 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000304 body = common.xml_to_json(etree.fromstring(body))
zhangyanzi6b632432013-10-24 19:08:50 +0800305 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800306
307 def create_volume_transfer(self, vol_id, display_name=None):
308 """Create a volume transfer."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000309 post_body = common.Element("transfer",
310 volume_id=vol_id)
wingwjcbd82dc2013-10-22 16:38:39 +0800311 if display_name:
312 post_body.add_attr('name', display_name)
313 resp, body = self.post('os-volume-transfer',
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000314 str(common.Document(post_body)))
315 volume = common.xml_to_json(etree.fromstring(body))
wingwjcbd82dc2013-10-22 16:38:39 +0800316 return resp, volume
317
318 def get_volume_transfer(self, transfer_id):
319 """Returns the details of a volume transfer."""
320 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200321 resp, body = self.get(url)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000322 volume = common.xml_to_json(etree.fromstring(body))
wingwjcbd82dc2013-10-22 16:38:39 +0800323 return resp, volume
324
325 def list_volume_transfers(self, params=None):
326 """List all the volume transfers created."""
327 url = 'os-volume-transfer'
328 if params:
329 url += '?%s' % urllib.urlencode(params)
330
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200331 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800332 body = etree.fromstring(body)
333 volumes = []
334 if body is not None:
335 volumes += [self._parse_volume_transfer(vol) for vol in list(body)]
336 return resp, volumes
337
338 def _parse_volume_transfer(self, body):
339 vol = dict((attr, body.get(attr)) for attr in body.keys())
340 for child in body.getchildren():
341 tag = child.tag
342 if tag.startswith("{"):
343 tag = tag.split("}", 1)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000344 vol[tag] = common.xml_to_json(child)
wingwjcbd82dc2013-10-22 16:38:39 +0800345 return vol
346
347 def delete_volume_transfer(self, transfer_id):
348 """Delete a volume transfer."""
349 return self.delete("os-volume-transfer/%s" % str(transfer_id))
350
351 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
352 """Accept a volume transfer."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000353 post_body = common.Element("accept", auth_key=transfer_auth_key)
wingwjcbd82dc2013-10-22 16:38:39 +0800354 url = 'os-volume-transfer/%s/accept' % transfer_id
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000355 resp, body = self.post(url, str(common.Document(post_body)))
356 volume = common.xml_to_json(etree.fromstring(body))
wingwjcbd82dc2013-10-22 16:38:39 +0800357 return resp, volume
zhangyanziaa180072013-11-21 12:31:26 +0800358
359 def update_volume_readonly(self, volume_id, readonly):
360 """Update the Specified Volume readonly."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000361 post_body = common.Element("os-update_readonly_flag",
362 readonly=readonly)
zhangyanziaa180072013-11-21 12:31:26 +0800363 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000364 resp, body = self.post(url, str(common.Document(post_body)))
zhangyanziaa180072013-11-21 12:31:26 +0800365 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000366 body = common.xml_to_json(etree.fromstring(body))
zhangyanziaa180072013-11-21 12:31:26 +0800367 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800368
369 def force_delete_volume(self, volume_id):
370 """Force Delete Volume."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000371 post_body = common.Element("os-force_delete")
wanghao9d3d6cb2013-11-12 15:10:10 +0800372 url = 'volumes/%s/action' % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000373 resp, body = self.post(url, str(common.Document(post_body)))
wanghao9d3d6cb2013-11-12 15:10:10 +0800374 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000375 body = common.xml_to_json(etree.fromstring(body))
wanghao9d3d6cb2013-11-12 15:10:10 +0800376 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800377
378 def _metadata_body(self, meta):
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000379 post_body = common.Element('metadata')
huangtianhua0ff41682013-12-16 14:49:31 +0800380 for k, v in meta.items():
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000381 data = common.Element('meta', key=k)
Ryan McNair9acfcf72014-01-27 21:06:48 +0000382 # Escape value to allow for special XML chars
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000383 data.append(common.Text(saxutils.escape(v)))
huangtianhua0ff41682013-12-16 14:49:31 +0800384 post_body.append(data)
385 return post_body
386
387 def _parse_key_value(self, node):
388 """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
389 data = {}
390 for node in node.getchildren():
391 data[node.get('key')] = node.text
392 return data
393
394 def create_volume_metadata(self, volume_id, metadata):
395 """Create metadata for the volume."""
396 post_body = self._metadata_body(metadata)
397 resp, body = self.post('volumes/%s/metadata' % volume_id,
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000398 str(common.Document(post_body)))
huangtianhua0ff41682013-12-16 14:49:31 +0800399 body = self._parse_key_value(etree.fromstring(body))
400 return resp, body
401
402 def get_volume_metadata(self, volume_id):
403 """Get metadata of the volume."""
404 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200405 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800406 body = self._parse_key_value(etree.fromstring(body))
407 return resp, body
408
409 def update_volume_metadata(self, volume_id, metadata):
410 """Update metadata for the volume."""
411 put_body = self._metadata_body(metadata)
412 url = "volumes/%s/metadata" % str(volume_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000413 resp, body = self.put(url, str(common.Document(put_body)))
huangtianhua0ff41682013-12-16 14:49:31 +0800414 body = self._parse_key_value(etree.fromstring(body))
415 return resp, body
416
417 def update_volume_metadata_item(self, volume_id, id, meta_item):
418 """Update metadata item for the volume."""
419 for k, v in meta_item.items():
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000420 put_body = common.Element('meta', key=k)
421 put_body.append(common.Text(v))
huangtianhua0ff41682013-12-16 14:49:31 +0800422 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000423 resp, body = self.put(url, str(common.Document(put_body)))
424 body = common.xml_to_json(etree.fromstring(body))
huangtianhua0ff41682013-12-16 14:49:31 +0800425 return resp, body
426
427 def delete_volume_metadata_item(self, volume_id, id):
428 """Delete metadata item for the volume."""
429 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
430 return self.delete(url)