blob: ef5f3e9892b546f2831d198c5c92b68da7e904ac [file] [log] [blame]
Matthew Treinish9854d5b2012-09-20 10:22:13 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
3# Copyright 2012 IBM
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import time
19
20from lxml import etree
21
22from tempest.common.rest_client import RestClientXML
23from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050024from tempest.services.compute.xml.common import xml_to_json
25from tempest.services.compute.xml.common import XMLNS_11
26from tempest.services.compute.xml.common import Element
27from tempest.services.compute.xml.common import Text
28from tempest.services.compute.xml.common import Document
Matthew Treinish9854d5b2012-09-20 10:22:13 -040029
30
31class VolumesClientXML(RestClientXML):
32 """
33 Client class to send CRUD Volume API requests to a Cinder endpoint
34 """
35
36 def __init__(self, config, username, password, auth_url, tenant_name=None):
37 super(VolumesClientXML, self).__init__(config, username, password,
38 auth_url, tenant_name)
39 self.service = self.config.compute.catalog_type
40 self.build_interval = self.config.compute.build_interval
41 self.build_timeout = self.config.compute.build_timeout
42
43 def _parse_volume(self, body):
44 vol = dict((attr, body.get(attr)) for attr in body.keys())
45
46 for child in body.getchildren():
47 tag = child.tag
48 if tag.startswith("{"):
49 ns, tag = tag.split("}", 1)
50 if tag == 'metadata':
51 vol['metadata'] = dict((meta.get('key'),
52 meta.text) for meta in list(child))
53 else:
54 vol[tag] = xml_to_json(child)
55 return vol
56
57 def list_volumes(self, params=None):
58 """List all the volumes created"""
59 url = 'volumes'
60
61 if params:
62 url += '?%s' % urllib.urlencode(params)
63
64 resp, body = self.get(url, self.headers)
65 body = etree.fromstring(body)
66 volumes = []
67 if body is not None:
68 volumes += [self._parse_volume(vol) for vol in list(body)]
69 return resp, volumes
70
71 def list_volumes_with_detail(self, params=None):
72 """List all the details of volumes"""
73 url = 'volumes/detail'
74
75 if params:
76 url += '?%s' % urllib.urlencode(params)
77
78 resp, body = self.get(url, self.headers)
79 body = etree.fromstring(body)
80 volumes = []
81 if body is not None:
82 volumes += [self._parse_volume(vol) for vol in list(body)]
83 return resp, volumes
84
85 def get_volume(self, volume_id):
86 """Returns the details of a single volume"""
87 url = "volumes/%s" % str(volume_id)
88 resp, body = self.get(url, self.headers)
89 body = etree.fromstring(body)
90 return resp, self._parse_volume(body)
91
92 def create_volume(self, size, display_name=None, metadata=None):
93 """Creates a new Volume.
94
95 :param size: Size of volume in GB. (Required)
96 :param display_name: Optional Volume Name.
97 :param metadata: An optional dictionary of values for metadata.
98 """
Zhongyue Luoe0884a32012-09-25 17:24:17 +080099 volume = Element("volume", xmlns=XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400100 if display_name:
101 volume.add_attr('display_name', display_name)
102
103 if metadata:
104 _metadata = Element('metadata')
105 volume.append(_metadata)
106 for key, value in metadata.items():
107 meta = Element('meta')
108 meta.add_attr('key', key)
109 meta.append(Text(value))
110 _metadata.append(meta)
111
112 resp, body = self.post('volumes', str(Document(volume)),
113 self.headers)
114 body = xml_to_json(etree.fromstring(body))
115 return resp, body
116
117 def delete_volume(self, volume_id):
118 """Deletes the Specified Volume"""
119 return self.delete("volumes/%s" % str(volume_id))
120
121 def wait_for_volume_status(self, volume_id, status):
122 """Waits for a Volume to reach a given status"""
123 resp, body = self.get_volume(volume_id)
124 volume_name = body['displayName']
125 volume_status = body['status']
126 start = int(time.time())
127
128 while volume_status != status:
129 time.sleep(self.build_interval)
130 resp, body = self.get_volume(volume_id)
131 volume_status = body['status']
132 if volume_status == 'error':
133 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
134
135 if int(time.time()) - start >= self.build_timeout:
136 message = 'Volume %s failed to reach %s status within '\
137 'the required time (%s s).' % (volume_name, status,
138 self.build_timeout)
139 raise exceptions.TimeoutException(message)
140
141 def is_resource_deleted(self, id):
142 try:
143 self.get_volume(id)
144 except exceptions.NotFound:
145 return True
146 return False