blob: c1bcf6e30857879f079b9ca637996d29900ce4ef [file] [log] [blame]
Zhi Kun Liu4be2f602014-02-08 11:52:05 +08001# Copyright 2012 IBM Corp.
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
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080016import urllib
17
18from lxml import etree
19
Matthew Treinish28f164c2014-03-04 18:55:06 +000020from tempest.common import xml_utils as common
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080021from tempest.services.volume.xml import volumes_client
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080022
23
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080024class VolumesV2ClientXML(volumes_client.BaseVolumesClientXML):
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080025 """
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080026 Client class to send CRUD Volume API V2 requests to a Cinder endpoint
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080027 """
28
29 def __init__(self, auth_provider):
Zhi Kun Liu8cc3c842014-01-07 10:44:34 +080030 super(VolumesV2ClientXML, self).__init__(auth_provider)
31
32 self.api_version = "v2"
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080033
34 def _parse_volume(self, body):
35 vol = dict((attr, body.get(attr)) for attr in body.keys())
36
37 for child in body.getchildren():
38 tag = child.tag
39 if tag.startswith("{"):
40 ns, tag = tag.split("}", 1)
41 if tag == 'metadata':
42 vol['metadata'] = dict((meta.get('key'),
43 meta.text) for meta in
44 child.getchildren())
45 else:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000046 vol[tag] = common.xml_to_json(child)
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080047 self._translate_attributes_to_json(vol)
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080048 return vol
49
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080050 def list_volumes_with_detail(self, params=None):
51 """List all the details of volumes."""
52 url = 'volumes/detail'
53
54 if params:
55 url += '?%s' % urllib.urlencode(params)
56
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020057 resp, body = self.get(url)
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080058 body = etree.fromstring(body)
59 volumes = []
60 if body is not None:
61 volumes += [self._parse_volume(vol) for vol in list(body)]
62 for v in volumes:
63 v = self._check_if_bootable(v)
64 return resp, volumes
65
66 def get_volume(self, volume_id):
67 """Returns the details of a single volume."""
68 url = "volumes/%s" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020069 resp, body = self.get(url)
Zhi Kun Liu4be2f602014-02-08 11:52:05 +080070 body = self._parse_volume(etree.fromstring(body))
71 body = self._check_if_bootable(body)
72 return resp, body