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