blob: cb6cefcfedda5e83fdfb8de68c185f0a69d92472 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Matthew Treinish4e086902012-08-17 17:52:22 -04002# 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
Matthew Treinish4e086902012-08-17 17:52:22 -040016import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Matthew Treinish4e086902012-08-17 17:52:22 -040018
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019from lxml import etree
20
Matthew Treinish4e086902012-08-17 17:52:22 -040021from tempest.common.rest_client import RestClientXML
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest import exceptions
23from tempest.services.compute.xml.common import Document
dwallecke62b9f02012-10-10 23:34:42 -050024from tempest.services.compute.xml.common import Element
25from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest.services.compute.xml.common import xml_to_json
27from tempest.services.compute.xml.common import XMLNS_11
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):
Sean Daguef237ccb2013-01-04 15:19:14 -050055 """List all the volumes created."""
Matthew Treinish4e086902012-08-17 17:52:22 -040056 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):
Sean Daguef237ccb2013-01-04 15:19:14 -050069 """List all the details of volumes."""
Matthew Treinish4e086902012-08-17 17:52:22 -040070 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
Attila Fazekasb8aa7592013-01-26 01:25:45 +010082 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050083 """Returns the details of a single volume."""
Matthew Treinish4e086902012-08-17 17:52:22 -040084 url = "os-volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010085 resp, body = self.get(url, self.headers)
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):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Deletes the Specified Volume."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400118 return self.delete("os-volumes/%s" % str(volume_id))
119
120 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500121 """Waits for a Volume to reach a given status."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400122 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:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100142 self.get_volume(id)
Matthew Treinish4e086902012-08-17 17:52:22 -0400143 except exceptions.NotFound:
144 return True
145 return False