blob: b90c65a8ff8e4637d44cf2b5b5bcde9e5d5ebbd8 [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
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Matthew Treinish9854d5b2012-09-20 10:22:13 -040020
21from lxml import etree
22
23from tempest.common.rest_client import RestClientXML
24from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050025from tempest.services.compute.xml.common import xml_to_json
26from tempest.services.compute.xml.common import XMLNS_11
27from tempest.services.compute.xml.common import Element
28from tempest.services.compute.xml.common import Text
29from tempest.services.compute.xml.common import Document
Matthew Treinish9854d5b2012-09-20 10:22:13 -040030
31
32class VolumesClientXML(RestClientXML):
33 """
34 Client class to send CRUD Volume API requests to a Cinder endpoint
35 """
36
37 def __init__(self, config, username, password, auth_url, tenant_name=None):
38 super(VolumesClientXML, self).__init__(config, username, password,
39 auth_url, tenant_name)
40 self.service = self.config.compute.catalog_type
41 self.build_interval = self.config.compute.build_interval
42 self.build_timeout = self.config.compute.build_timeout
43
44 def _parse_volume(self, body):
45 vol = dict((attr, body.get(attr)) for attr in body.keys())
46
47 for child in body.getchildren():
48 tag = child.tag
49 if tag.startswith("{"):
50 ns, tag = tag.split("}", 1)
51 if tag == 'metadata':
52 vol['metadata'] = dict((meta.get('key'),
53 meta.text) for meta in list(child))
54 else:
55 vol[tag] = xml_to_json(child)
56 return vol
57
58 def list_volumes(self, params=None):
59 """List all the volumes created"""
60 url = 'volumes'
61
62 if params:
63 url += '?%s' % urllib.urlencode(params)
64
65 resp, body = self.get(url, self.headers)
66 body = etree.fromstring(body)
67 volumes = []
68 if body is not None:
69 volumes += [self._parse_volume(vol) for vol in list(body)]
70 return resp, volumes
71
72 def list_volumes_with_detail(self, params=None):
73 """List all the details of volumes"""
74 url = 'volumes/detail'
75
76 if params:
77 url += '?%s' % urllib.urlencode(params)
78
79 resp, body = self.get(url, self.headers)
80 body = etree.fromstring(body)
81 volumes = []
82 if body is not None:
83 volumes += [self._parse_volume(vol) for vol in list(body)]
84 return resp, volumes
85
Matthew Treinish426326e2012-11-30 13:17:00 -050086 def get_volume(self, volume_id, wait=None):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040087 """Returns the details of a single volume"""
88 url = "volumes/%s" % str(volume_id)
Matthew Treinish426326e2012-11-30 13:17:00 -050089 resp, body = self.get(url, self.headers, wait=wait)
Matthew Treinish9854d5b2012-09-20 10:22:13 -040090 body = etree.fromstring(body)
91 return resp, self._parse_volume(body)
92
93 def create_volume(self, size, display_name=None, metadata=None):
94 """Creates a new Volume.
95
96 :param size: Size of volume in GB. (Required)
97 :param display_name: Optional Volume Name.
98 :param metadata: An optional dictionary of values for metadata.
99 """
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800100 volume = Element("volume", xmlns=XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400101 if display_name:
102 volume.add_attr('display_name', display_name)
103
104 if metadata:
105 _metadata = Element('metadata')
106 volume.append(_metadata)
107 for key, value in metadata.items():
108 meta = Element('meta')
109 meta.add_attr('key', key)
110 meta.append(Text(value))
111 _metadata.append(meta)
112
113 resp, body = self.post('volumes', str(Document(volume)),
114 self.headers)
115 body = xml_to_json(etree.fromstring(body))
116 return resp, body
117
118 def delete_volume(self, volume_id):
119 """Deletes the Specified Volume"""
120 return self.delete("volumes/%s" % str(volume_id))
121
122 def wait_for_volume_status(self, volume_id, status):
123 """Waits for a Volume to reach a given status"""
124 resp, body = self.get_volume(volume_id)
125 volume_name = body['displayName']
126 volume_status = body['status']
127 start = int(time.time())
128
129 while volume_status != status:
130 time.sleep(self.build_interval)
131 resp, body = self.get_volume(volume_id)
132 volume_status = body['status']
133 if volume_status == 'error':
134 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
135
136 if int(time.time()) - start >= self.build_timeout:
137 message = 'Volume %s failed to reach %s status within '\
138 'the required time (%s s).' % (volume_name, status,
139 self.build_timeout)
140 raise exceptions.TimeoutException(message)
141
142 def is_resource_deleted(self, id):
143 try:
Matthew Treinish426326e2012-11-30 13:17:00 -0500144 self.get_volume(id, wait=True)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400145 except exceptions.NotFound:
146 return True
147 return False