blob: d9906fd0a228cad1da943a618f1d21e07f0a7da5 [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05004# 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
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053018import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053019import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050020import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053021
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest.common.rest_client import RestClient
23from tempest import exceptions
24
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053025
Matthew Treinish4e086902012-08-17 17:52:22 -040026class VolumesExtensionsClientJSON(RestClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053027
chris fattarsi5098fa22012-04-17 13:27:00 -070028 def __init__(self, config, username, password, auth_url, tenant_name=None):
Matthew Treinish4e086902012-08-17 17:52:22 -040029 super(VolumesExtensionsClientJSON, self).__init__(config, username,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080030 password, auth_url,
31 tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070032 self.service = self.config.compute.catalog_type
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070033 self.build_interval = self.config.volume.build_interval
34 self.build_timeout = self.config.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053035
36 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050037 """List all the volumes created."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053038 url = 'os-volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050039 if params:
40 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053041
chris fattarsi5098fa22012-04-17 13:27:00 -070042 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053043 body = json.loads(body)
44 return resp, body['volumes']
45
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)
54 return resp, body['volumes']
55
Attila Fazekasb8aa7592013-01-26 01:25:45 +010056 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """Returns the details of a single volume."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053058 url = "os-volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010059 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053060 body = json.loads(body)
61 return resp, body['volume']
62
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053063 def create_volume(self, size, **kwargs):
64 """
65 Creates a new Volume.
66 size(Required): Size of volume in GB.
67 Following optional keyword arguments are accepted:
68 display_name: Optional Volume Name.
69 metadata: A dictionary of values to be used as metadata.
70 """
71 post_body = {
72 'size': size,
73 'display_name': kwargs.get('display_name'),
74 'metadata': kwargs.get('metadata'),
Zhongyue Luo30a563f2012-09-30 23:43:50 +090075 }
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053076
77 post_body = json.dumps({'volume': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -070078 resp, body = self.post('os-volumes', post_body, self.headers)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053079 body = json.loads(body)
80 return resp, body['volume']
81
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053082 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050083 """Deletes the Specified Volume."""
chris fattarsi5098fa22012-04-17 13:27:00 -070084 return self.delete("os-volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053085
86 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053088 resp, body = self.get_volume(volume_id)
89 volume_name = body['displayName']
90 volume_status = body['status']
91 start = int(time.time())
92
93 while volume_status != status:
94 time.sleep(self.build_interval)
95 resp, body = self.get_volume(volume_id)
96 volume_status = body['status']
97 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +053098 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053099
100 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800101 message = ('Volume %s failed to reach %s status within '
102 'the required time (%s s).' %
103 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530104 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400105
106 def is_resource_deleted(self, id):
107 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100108 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400109 except exceptions.NotFound:
110 return True
111 return False