blob: ecbfb19c532d592c292953d434f086233ba74094 [file] [log] [blame]
Matthew Treinish9854d5b2012-09-20 10:22:13 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2012 IBM Corp.
Matthew Treinish9854d5b2012-09-20 10:22:13 -04004# 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
anju tiwari789449a2013-08-29 16:56:17 +053059 def get_attachment_from_volume(self, volume):
60 """Return the element 'attachment' from input volumes."""
61 return volume['attachments']['attachment']
62
Matthew Treinish9854d5b2012-09-20 10:22:13 -040063 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050064 """List all the volumes created."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040065 url = 'volumes'
66
67 if params:
68 url += '?%s' % urllib.urlencode(params)
69
70 resp, body = self.get(url, self.headers)
71 body = etree.fromstring(body)
72 volumes = []
73 if body is not None:
74 volumes += [self._parse_volume(vol) for vol in list(body)]
75 return resp, volumes
76
77 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050078 """List all the details of volumes."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040079 url = 'volumes/detail'
80
81 if params:
82 url += '?%s' % urllib.urlencode(params)
83
84 resp, body = self.get(url, self.headers)
85 body = etree.fromstring(body)
86 volumes = []
87 if body is not None:
88 volumes += [self._parse_volume(vol) for vol in list(body)]
89 return resp, volumes
90
Attila Fazekasb8aa7592013-01-26 01:25:45 +010091 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050092 """Returns the details of a single volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040093 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010094 resp, body = self.get(url, self.headers)
Matthew Treinish9854d5b2012-09-20 10:22:13 -040095 body = etree.fromstring(body)
96 return resp, self._parse_volume(body)
97
Attila Fazekas786236c2013-01-31 16:06:51 +010098 def create_volume(self, size, **kwargs):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040099 """Creates a new Volume.
100
101 :param size: Size of volume in GB. (Required)
102 :param display_name: Optional Volume Name.
103 :param metadata: An optional dictionary of values for metadata.
Attila Fazekas786236c2013-01-31 16:06:51 +0100104 :param volume_type: Optional Name of volume_type for the volume
105 :param snapshot_id: When specified the volume is created from
106 this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +0200107 :param imageRef: When specified the volume is created from this
108 image
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400109 """
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200110 # NOTE(afazekas): it should use a volume namespace
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800111 volume = Element("volume", xmlns=XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400112
Attila Fazekas786236c2013-01-31 16:06:51 +0100113 if 'metadata' in kwargs:
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400114 _metadata = Element('metadata')
115 volume.append(_metadata)
Attila Fazekas786236c2013-01-31 16:06:51 +0100116 for key, value in kwargs['metadata'].items():
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400117 meta = Element('meta')
118 meta.add_attr('key', key)
119 meta.append(Text(value))
120 _metadata.append(meta)
Attila Fazekas786236c2013-01-31 16:06:51 +0100121 attr_to_add = kwargs.copy()
122 del attr_to_add['metadata']
123 else:
124 attr_to_add = kwargs
125
126 for key, value in attr_to_add.items():
127 volume.add_attr(key, value)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400128
129 resp, body = self.post('volumes', str(Document(volume)),
130 self.headers)
131 body = xml_to_json(etree.fromstring(body))
132 return resp, body
133
134 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500135 """Deletes the Specified Volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400136 return self.delete("volumes/%s" % str(volume_id))
137
138 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500139 """Waits for a Volume to reach a given status."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400140 resp, body = self.get_volume(volume_id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400141 volume_status = body['status']
142 start = int(time.time())
143
144 while volume_status != status:
145 time.sleep(self.build_interval)
146 resp, body = self.get_volume(volume_id)
147 volume_status = body['status']
148 if volume_status == 'error':
149 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
150
151 if int(time.time()) - start >= self.build_timeout:
152 message = 'Volume %s failed to reach %s status within '\
Attila Fazekas786236c2013-01-31 16:06:51 +0100153 'the required time (%s s).' % (volume_id,
154 status,
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400155 self.build_timeout)
156 raise exceptions.TimeoutException(message)
157
158 def is_resource_deleted(self, id):
159 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100160 self.get_volume(id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400161 except exceptions.NotFound:
162 return True
163 return False
anju tiwari789449a2013-08-29 16:56:17 +0530164
165 def attach_volume(self, volume_id, instance_uuid, mountpoint):
166 """Attaches a volume to a given instance on a given mountpoint."""
167 post_body = Element("os-attach",
168 instance_uuid=instance_uuid,
169 mountpoint=mountpoint
170 )
171 url = 'volumes/%s/action' % str(volume_id)
172 resp, body = self.post(url, str(Document(post_body)), self.headers)
173 if body:
174 body = xml_to_json(etree.fromstring(body))
175 return resp, body
176
177 def detach_volume(self, volume_id):
178 """Detaches a volume from an instance."""
179 post_body = Element("os-detach")
180 url = 'volumes/%s/action' % str(volume_id)
181 resp, body = self.post(url, str(Document(post_body)), self.headers)
182 if body:
183 body = xml_to_json(etree.fromstring(body))
184 return resp, body
185
Ryan Hsua67f4632013-08-29 16:03:06 -0700186 def upload_volume(self, volume_id, image_name, disk_format):
anju tiwari789449a2013-08-29 16:56:17 +0530187 """Uploads a volume in Glance."""
188 post_body = Element("os-volume_upload_image",
Ryan Hsua67f4632013-08-29 16:03:06 -0700189 image_name=image_name,
190 disk_format=disk_format)
anju tiwari789449a2013-08-29 16:56:17 +0530191 url = 'volumes/%s/action' % str(volume_id)
192 resp, body = self.post(url, str(Document(post_body)), self.headers)
193 volume = xml_to_json(etree.fromstring(body))
194 return resp, volume