blob: 1c5d5253c8189c2660466a3e7f0e72d02ff12675 [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# 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-ganesanb4465572012-03-22 01:22:50 +053018from tempest import exceptions
chris fattarsi5098fa22012-04-17 13:27:00 -070019from tempest.common.rest_client import RestClient
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053020import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053021import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050022import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053023
24
Matthew Treinish4e086902012-08-17 17:52:22 -040025class VolumesExtensionsClientJSON(RestClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053026
chris fattarsi5098fa22012-04-17 13:27:00 -070027 def __init__(self, config, username, password, auth_url, tenant_name=None):
Matthew Treinish4e086902012-08-17 17:52:22 -040028 super(VolumesExtensionsClientJSON, self).__init__(config, username,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080029 password, auth_url,
30 tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070031 self.service = self.config.compute.catalog_type
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070032 self.build_interval = self.config.volume.build_interval
33 self.build_timeout = self.config.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053034
35 def list_volumes(self, params=None):
36 """List all the volumes created"""
37 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)
43 return resp, body['volumes']
44
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053045 def list_volumes_with_detail(self, params=None):
46 """List all the details of volumes"""
47 url = 'os-volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050048 if params:
49 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053050
chris fattarsi5098fa22012-04-17 13:27:00 -070051 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053052 body = json.loads(body)
53 return resp, body['volumes']
54
Matthew Treinish426326e2012-11-30 13:17:00 -050055 def get_volume(self, volume_id, wait=None):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053056 """Returns the details of a single volume"""
57 url = "os-volumes/%s" % str(volume_id)
Matthew Treinish426326e2012-11-30 13:17:00 -050058 resp, body = self.get(url, wait=wait)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053059 body = json.loads(body)
60 return resp, body['volume']
61
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053062 def create_volume(self, size, **kwargs):
63 """
64 Creates a new Volume.
65 size(Required): Size of volume in GB.
66 Following optional keyword arguments are accepted:
67 display_name: Optional Volume Name.
68 metadata: A dictionary of values to be used as metadata.
69 """
70 post_body = {
71 'size': size,
72 'display_name': kwargs.get('display_name'),
73 'metadata': kwargs.get('metadata'),
Zhongyue Luo30a563f2012-09-30 23:43:50 +090074 }
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053075
76 post_body = json.dumps({'volume': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -070077 resp, body = self.post('os-volumes', post_body, self.headers)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053078 body = json.loads(body)
79 return resp, body['volume']
80
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053081 def delete_volume(self, volume_id):
82 """Deletes the Specified Volume"""
chris fattarsi5098fa22012-04-17 13:27:00 -070083 return self.delete("os-volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053084
85 def wait_for_volume_status(self, volume_id, status):
86 """Waits for a Volume to reach a given status"""
87 resp, body = self.get_volume(volume_id)
88 volume_name = body['displayName']
89 volume_status = body['status']
90 start = int(time.time())
91
92 while volume_status != status:
93 time.sleep(self.build_interval)
94 resp, body = self.get_volume(volume_id)
95 volume_status = body['status']
96 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +053097 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053098
99 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800100 message = ('Volume %s failed to reach %s status within '
101 'the required time (%s s).' %
102 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530103 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400104
105 def is_resource_deleted(self, id):
106 try:
Matthew Treinish426326e2012-11-30 13:17:00 -0500107 self.get_volume(id, wait=True)
David Kranz6aceb4a2012-06-05 14:05:45 -0400108 except exceptions.NotFound:
109 return True
110 return False