blob: a43fc21daa488f66d223fc10765045853462c804 [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 Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest import exceptions
24from tempest.services.compute.xml.common import Document
dwallecke62b9f02012-10-10 23:34:42 -050025from tempest.services.compute.xml.common import Element
26from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050027from tempest.services.compute.xml.common import xml_to_json
28from tempest.services.compute.xml.common import XMLNS_11
Matthew Treinish4e086902012-08-17 17:52:22 -040029
Matthew Treinish684d8992014-01-30 16:27:40 +000030CONF = config.CONF
31
Matthew Treinish4e086902012-08-17 17:52:22 -040032
33class VolumesExtensionsClientXML(RestClientXML):
34
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000035 def __init__(self, auth_provider):
36 super(VolumesExtensionsClientXML, self).__init__(
37 auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000038 self.service = CONF.compute.catalog_type
39 self.build_interval = CONF.compute.build_interval
40 self.build_timeout = CONF.compute.build_timeout
Matthew Treinish4e086902012-08-17 17:52:22 -040041
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
vponomaryovf4c27f92014-02-18 10:56:42 +020063 resp, body = self.get(url)
Matthew Treinish4e086902012-08-17 17:52:22 -040064 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
vponomaryovf4c27f92014-02-18 10:56:42 +020077 resp, body = self.get(url)
Matthew Treinish4e086902012-08-17 17:52:22 -040078 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020087 resp, body = self.get(url)
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
vponomaryovf4c27f92014-02-18 10:56:42 +0200113 resp, body = self.post('os-volumes', str(Document(volume)))
Matthew Treinish4e086902012-08-17 17:52:22 -0400114 body = xml_to_json(etree.fromstring(body))
115 return resp, body
116
117 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500118 """Deletes the Specified Volume."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400119 return self.delete("os-volumes/%s" % str(volume_id))
120
121 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500122 """Waits for a Volume to reach a given status."""
Matthew Treinish4e086902012-08-17 17:52:22 -0400123 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:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100143 self.get_volume(id)
Matthew Treinish4e086902012-08-17 17:52:22 -0400144 except exceptions.NotFound:
145 return True
146 return False