blob: 06cfcfbf8a8dc4b17bada775428d74b889c3db41 [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
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Matthew Treinish4e086902012-08-17 17:52:22 -040020
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from lxml import etree
22
Matthew Treinish4e086902012-08-17 17:52:22 -040023from tempest.common.rest_client import RestClientXML
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest import exceptions
25from tempest.services.compute.xml.common import Document
dwallecke62b9f02012-10-10 23:34:42 -050026from tempest.services.compute.xml.common import Element
27from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050028from tempest.services.compute.xml.common import xml_to_json
29from tempest.services.compute.xml.common import XMLNS_11
Matthew Treinish4e086902012-08-17 17:52:22 -040030
31
32class VolumesExtensionsClientXML(RestClientXML):
33
34 def __init__(self, config, username, password, auth_url, tenant_name=None):
35 super(VolumesExtensionsClientXML, self).__init__(config,
36 username, password,
37 auth_url, tenant_name)
38 self.service = self.config.compute.catalog_type
39 self.build_interval = self.config.compute.build_interval
40 self.build_timeout = self.config.compute.build_timeout
41
42 def _parse_volume(self, body):
43 vol = dict((attr, body.get(attr)) for attr in body.keys())
44
45 for child in body.getchildren():
46 tag = child.tag
47 if tag.startswith("{"):
48 ns, tag = tag.split("}", 1)
49 if tag == 'metadata':
50 vol['metadata'] = dict((meta.get('key'),
51 meta.text) for meta in list(child))
52 else:
53 vol[tag] = xml_to_json(child)
54 return vol
55
56 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """List all the volumes created."""
Matthew Treinish4e086902012-08-17 17:52:22 -040058 url = 'os-volumes'
59
60 if params:
61 url += '?%s' % urllib.urlencode(params)
62
63 resp, body = self.get(url, self.headers)
64 body = etree.fromstring(body)
65 volumes = []
66 if body is not None:
67 volumes += [self._parse_volume(vol) for vol in list(body)]
68 return resp, volumes
69
70 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050071 """List all the details of volumes."""
Matthew Treinish4e086902012-08-17 17:52:22 -040072 url = 'os-volumes/detail'
73
74 if params:
75 url += '?%s' % urllib.urlencode(params)
76
77 resp, body = self.get(url, self.headers)
78 body = etree.fromstring(body)
79 volumes = []
80 if body is not None:
81 volumes += [self._parse_volume(vol) for vol in list(body)]
82 return resp, volumes
83
Attila Fazekasb8aa7592013-01-26 01:25:45 +010084 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Returns the details of a single volume."""
Matthew Treinish4e086902012-08-17 17:52:22 -040086 url = "os-volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010087 resp, body = self.get(url, self.headers)
Matthew Treinish4e086902012-08-17 17:52:22 -040088 body = etree.fromstring(body)
89 return resp, self._parse_volume(body)
90
91 def create_volume(self, size, display_name=None, metadata=None):
92 """Creates a new Volume.
93
94 :param size: Size of volume in GB. (Required)
95 :param display_name: Optional Volume Name.
96 :param metadata: An optional dictionary of values for metadata.
97 """
98 volume = Element("volume",
99 xmlns=XMLNS_11,
100 size=size)
101 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('os-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):
Sean Daguef237ccb2013-01-04 15:19:14 -0500119 """Deletes the Specified Volume."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400120 return self.delete("os-volumes/%s" % str(volume_id))
121
122 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500123 """Waits for a Volume to reach a given status."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400124 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:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100144 self.get_volume(id)
Matthew Treinish4e086902012-08-17 17:52:22 -0400145 except exceptions.NotFound:
146 return True
147 return False