blob: afa6937062848f17cfa14bd87724b21b2e137ceb [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053016import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053017import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050018import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053019
Marc Koderer6fbd74f2014-08-04 09:38:19 +020020from tempest.api_schema.response.compute.v2 import volumes as schema
Haiwei Xuab924622014-03-05 04:33:51 +090021from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest import exceptions
24
Matthew Treinish684d8992014-01-30 16:27:40 +000025CONF = config.CONF
26
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053027
Haiwei Xuab924622014-03-05 04:33:51 +090028class VolumesExtensionsClientJSON(rest_client.RestClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053029
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000030 def __init__(self, auth_provider):
31 super(VolumesExtensionsClientJSON, self).__init__(
32 auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
34 self.build_interval = CONF.volume.build_interval
35 self.build_timeout = CONF.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053036
37 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050038 """List all the volumes created."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053039 url = 'os-volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050040 if params:
41 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053042
chris fattarsi5098fa22012-04-17 13:27:00 -070043 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053044 body = json.loads(body)
Ghanshyame8940da2014-03-24 15:06:30 +090045 self.validate_response(schema.list_volumes, resp, body)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053046 return resp, body['volumes']
47
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053048 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050049 """List all the details of volumes."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053050 url = 'os-volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050051 if params:
52 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053053
chris fattarsi5098fa22012-04-17 13:27:00 -070054 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053055 body = json.loads(body)
Ghanshyame8940da2014-03-24 15:06:30 +090056 self.validate_response(schema.list_volumes, resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053057 return resp, body['volumes']
58
Attila Fazekasb8aa7592013-01-26 01:25:45 +010059 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050060 """Returns the details of a single volume."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053061 url = "os-volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010062 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053063 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090064 self.validate_response(schema.create_get_volume, resp, body)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053065 return resp, body['volume']
66
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053067 def create_volume(self, size, **kwargs):
68 """
69 Creates a new Volume.
70 size(Required): Size of volume in GB.
71 Following optional keyword arguments are accepted:
72 display_name: Optional Volume Name.
73 metadata: A dictionary of values to be used as metadata.
74 """
75 post_body = {
Ken'ichi Ohmichif6bf3d52014-11-27 07:33:55 +000076 'size': size
Zhongyue Luo30a563f2012-09-30 23:43:50 +090077 }
Ken'ichi Ohmichif6bf3d52014-11-27 07:33:55 +000078 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053079
80 post_body = json.dumps({'volume': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020081 resp, body = self.post('os-volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053082 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090083 self.validate_response(schema.create_get_volume, resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053084 return resp, body['volume']
85
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053086 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Deletes the Specified Volume."""
Ghanshyamb4ffd762014-03-27 10:22:36 +090088 resp, body = self.delete("os-volumes/%s" % str(volume_id))
89 self.validate_response(schema.delete_volume, resp, body)
90 return resp, body
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053091
92 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -050093 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053094 resp, body = self.get_volume(volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053095 volume_status = body['status']
96 start = int(time.time())
97
98 while volume_status != status:
99 time.sleep(self.build_interval)
100 resp, body = self.get_volume(volume_id)
101 volume_status = body['status']
102 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530103 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530104
105 if int(time.time()) - start >= self.build_timeout:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200106 message = ('Volume %s failed to reach %s status (current %s) '
107 'within the required time (%s s).' %
108 (volume_id, status, volume_status,
109 self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530110 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400111
112 def is_resource_deleted(self, id):
113 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100114 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400115 except exceptions.NotFound:
116 return True
117 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700118
119 @property
120 def resource_type(self):
121 """Returns the primary type of resource this client works with."""
122 return 'volume'