blob: 4c15256f8aad1d67897498f4298b83acefe04f95 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025from 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 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)
Attila Fazekas786236c2013-01-31 16:06:51 +010040 self.service = self.config.volume.catalog_type
Matthew Treinish9854d5b2012-09-20 10:22:13 -040041 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'),
Attila Fazekas786236c2013-01-31 16:06:51 +010053 meta.text) for meta in
54 child.getchildren())
Matthew Treinish9854d5b2012-09-20 10:22:13 -040055 else:
56 vol[tag] = xml_to_json(child)
Attila Fazekas786236c2013-01-31 16:06:51 +010057 return vol
Matthew Treinish9854d5b2012-09-20 10:22:13 -040058
59 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050060 """List all the volumes created."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040061 url = 'volumes'
62
63 if params:
64 url += '?%s' % urllib.urlencode(params)
65
66 resp, body = self.get(url, self.headers)
67 body = etree.fromstring(body)
68 volumes = []
69 if body is not None:
70 volumes += [self._parse_volume(vol) for vol in list(body)]
71 return resp, volumes
72
73 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050074 """List all the details of volumes."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040075 url = 'volumes/detail'
76
77 if params:
78 url += '?%s' % urllib.urlencode(params)
79
80 resp, body = self.get(url, self.headers)
81 body = etree.fromstring(body)
82 volumes = []
83 if body is not None:
84 volumes += [self._parse_volume(vol) for vol in list(body)]
85 return resp, volumes
86
Attila Fazekasb8aa7592013-01-26 01:25:45 +010087 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050088 """Returns the details of a single volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040089 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010090 resp, body = self.get(url, self.headers)
Matthew Treinish9854d5b2012-09-20 10:22:13 -040091 body = etree.fromstring(body)
92 return resp, self._parse_volume(body)
93
Attila Fazekas786236c2013-01-31 16:06:51 +010094 def create_volume(self, size, **kwargs):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040095 """Creates a new Volume.
96
97 :param size: Size of volume in GB. (Required)
98 :param display_name: Optional Volume Name.
99 :param metadata: An optional dictionary of values for metadata.
Attila Fazekas786236c2013-01-31 16:06:51 +0100100 :param volume_type: Optional Name of volume_type for the volume
101 :param snapshot_id: When specified the volume is created from
102 this snapshot
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400103 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100104 #NOTE(afazekas): it should use a volume namespace
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800105 volume = Element("volume", xmlns=XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400106
Attila Fazekas786236c2013-01-31 16:06:51 +0100107 if 'metadata' in kwargs:
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400108 _metadata = Element('metadata')
109 volume.append(_metadata)
Attila Fazekas786236c2013-01-31 16:06:51 +0100110 for key, value in kwargs['metadata'].items():
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400111 meta = Element('meta')
112 meta.add_attr('key', key)
113 meta.append(Text(value))
114 _metadata.append(meta)
Attila Fazekas786236c2013-01-31 16:06:51 +0100115 attr_to_add = kwargs.copy()
116 del attr_to_add['metadata']
117 else:
118 attr_to_add = kwargs
119
120 for key, value in attr_to_add.items():
121 volume.add_attr(key, value)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400122
123 resp, body = self.post('volumes', str(Document(volume)),
124 self.headers)
125 body = xml_to_json(etree.fromstring(body))
126 return resp, body
127
128 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500129 """Deletes the Specified Volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400130 return self.delete("volumes/%s" % str(volume_id))
131
132 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500133 """Waits for a Volume to reach a given status."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400134 resp, body = self.get_volume(volume_id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400135 volume_status = body['status']
136 start = int(time.time())
137
138 while volume_status != status:
139 time.sleep(self.build_interval)
140 resp, body = self.get_volume(volume_id)
141 volume_status = body['status']
142 if volume_status == 'error':
143 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
144
145 if int(time.time()) - start >= self.build_timeout:
146 message = 'Volume %s failed to reach %s status within '\
Attila Fazekas786236c2013-01-31 16:06:51 +0100147 'the required time (%s s).' % (volume_id,
148 status,
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400149 self.build_timeout)
150 raise exceptions.TimeoutException(message)
151
152 def is_resource_deleted(self, id):
153 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100154 self.get_volume(id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400155 except exceptions.NotFound:
156 return True
157 return False