blob: 673e365bfd45e9eb01af348e6260076170e669ca [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 = {
76 'size': size,
77 'display_name': kwargs.get('display_name'),
78 'metadata': kwargs.get('metadata'),
Zhongyue Luo30a563f2012-09-30 23:43:50 +090079 }
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053080
81 post_body = json.dumps({'volume': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020082 resp, body = self.post('os-volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053083 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090084 self.validate_response(schema.create_get_volume, resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053085 return resp, body['volume']
86
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053087 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050088 """Deletes the Specified Volume."""
Ghanshyamb4ffd762014-03-27 10:22:36 +090089 resp, body = self.delete("os-volumes/%s" % str(volume_id))
90 self.validate_response(schema.delete_volume, resp, body)
91 return resp, body
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053092
93 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053095 resp, body = self.get_volume(volume_id)
96 volume_name = body['displayName']
97 volume_status = body['status']
98 start = int(time.time())
99
100 while volume_status != status:
101 time.sleep(self.build_interval)
102 resp, body = self.get_volume(volume_id)
103 volume_status = body['status']
104 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530105 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530106
107 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800108 message = ('Volume %s failed to reach %s status within '
109 'the required time (%s s).' %
110 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530111 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400112
113 def is_resource_deleted(self, id):
114 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100115 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400116 except exceptions.NotFound:
117 return True
118 return False