Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 1 | # 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 Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 16 | import urllib |
| 17 | |
| 18 | from lxml import etree |
| 19 | |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 20 | from tempest.common import xml_utils as common |
Zhi Kun Liu | 6e6cf83 | 2014-05-08 17:25:22 +0800 | [diff] [blame] | 21 | from tempest.services.volume.xml import volumes_client |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 22 | |
| 23 | |
Zhi Kun Liu | 6e6cf83 | 2014-05-08 17:25:22 +0800 | [diff] [blame] | 24 | class VolumesV2ClientXML(volumes_client.BaseVolumesClientXML): |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 25 | """ |
Zhi Kun Liu | 6e6cf83 | 2014-05-08 17:25:22 +0800 | [diff] [blame] | 26 | Client class to send CRUD Volume API V2 requests to a Cinder endpoint |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | def __init__(self, auth_provider): |
Zhi Kun Liu | 8cc3c84 | 2014-01-07 10:44:34 +0800 | [diff] [blame] | 30 | super(VolumesV2ClientXML, self).__init__(auth_provider) |
| 31 | |
| 32 | self.api_version = "v2" |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 33 | |
| 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 Takada | 4d41c2f | 2014-03-07 11:58:31 +0000 | [diff] [blame] | 46 | vol[tag] = common.xml_to_json(child) |
Zhi Kun Liu | 6e6cf83 | 2014-05-08 17:25:22 +0800 | [diff] [blame] | 47 | self._translate_attributes_to_json(vol) |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 48 | return vol |
| 49 | |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 50 | 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 57 | resp, body = self.get(url) |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 58 | 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 Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 69 | resp, body = self.get(url) |
Zhi Kun Liu | 4be2f60 | 2014-02-08 11:52:05 +0800 | [diff] [blame] | 70 | body = self._parse_volume(etree.fromstring(body)) |
| 71 | body = self._check_if_bootable(body) |
| 72 | return resp, body |