blob: ba5921ea28b8bfcededa451750d5b6846040a974 [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
Masayuki Igawabfa07602015-01-20 18:47:17 +090020from tempest_lib import exceptions as lib_exc
21
ghanshyamaa93b4b2015-03-20 11:03:44 +090022from tempest.api_schema.response.compute.v2_1 import volumes as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000023from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest import exceptions
Matthew Treinish684d8992014-01-30 16:27:40 +000025
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053026
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000027class VolumesExtensionsClientJSON(service_client.ServiceClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053028
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010029 def __init__(self, auth_provider, service, region,
30 default_volume_size=1, **kwargs):
31 super(VolumesExtensionsClientJSON, self).__init__(
32 auth_provider, service, region, **kwargs)
33 self.default_volume_size = default_volume_size
34
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053035 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050036 """List all the volumes created."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053037 url = 'os-volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050038 if params:
39 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053040
chris fattarsi5098fa22012-04-17 13:27:00 -070041 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053042 body = json.loads(body)
Ghanshyame8940da2014-03-24 15:06:30 +090043 self.validate_response(schema.list_volumes, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050044 return service_client.ResponseBodyList(resp, body['volumes'])
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053045
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053046 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """List all the details of volumes."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053048 url = 'os-volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050049 if params:
50 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053051
chris fattarsi5098fa22012-04-17 13:27:00 -070052 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053053 body = json.loads(body)
Ghanshyame8940da2014-03-24 15:06:30 +090054 self.validate_response(schema.list_volumes, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050055 return service_client.ResponseBodyList(resp, body['volumes'])
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053056
Attila Fazekasb8aa7592013-01-26 01:25:45 +010057 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050058 """Returns the details of a single volume."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053059 url = "os-volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010060 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053061 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090062 self.validate_response(schema.create_get_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050063 return service_client.ResponseBody(resp, body['volume'])
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053064
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010065 def create_volume(self, size=None, **kwargs):
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053066 """
67 Creates a new Volume.
68 size(Required): Size of volume in GB.
69 Following optional keyword arguments are accepted:
70 display_name: Optional Volume Name.
71 metadata: A dictionary of values to be used as metadata.
72 """
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010073 if size is None:
74 size = self.default_volume_size
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053075 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)
David Kranz3ebc7212015-02-10 12:19:19 -050084 return service_client.ResponseBody(resp, body['volume'])
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053085
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)
David Kranz3ebc7212015-02-10 12:19:19 -050090 return service_client.ResponseBody(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."""
David Kranz3ebc7212015-02-10 12:19:19 -050094 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)
David Kranz3ebc7212015-02-10 12:19:19 -0500100 body = self.get_volume(volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530101 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)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900115 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -0400116 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'