blob: 165640fd654c250cb03130061a334b3eedb34649 [file] [log] [blame]
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +05301from tempest import exceptions
chris fattarsi5098fa22012-04-17 13:27:00 -07002from tempest.common.rest_client import RestClient
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +05303import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +05304import time
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +05305
6
chris fattarsi5098fa22012-04-17 13:27:00 -07007class VolumesClient(RestClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +05308
chris fattarsi5098fa22012-04-17 13:27:00 -07009 def __init__(self, config, username, password, auth_url, tenant_name=None):
10 super(VolumesClient, self).__init__(config, username, password,
11 auth_url, tenant_name)
12 self.service = self.config.compute.catalog_type
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053013 self.build_interval = self.config.compute.build_interval
14 self.build_timeout = self.config.compute.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053015
16 def list_volumes(self, params=None):
17 """List all the volumes created"""
18 url = 'os-volumes'
19 if params != None:
20 param_list = []
21 for param, value in params.iteritems():
22 param_list.append("%s=%s&" % (param, value))
23
24 url += '?' + ' '.join(param_list)
25
chris fattarsi5098fa22012-04-17 13:27:00 -070026 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053027 body = json.loads(body)
28 return resp, body['volumes']
29
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053030 def list_volumes_with_detail(self, params=None):
31 """List all the details of volumes"""
32 url = 'os-volumes/detail'
33 if params != None:
34 param_list = []
35 for param, value in params.iteritems():
36 param_list.append("%s=%s&" % (param, value))
37
38 url = '?' + ' '.join(param_list)
39
chris fattarsi5098fa22012-04-17 13:27:00 -070040 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053041 body = json.loads(body)
42 return resp, body['volumes']
43
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053044 def get_volume(self, volume_id):
45 """Returns the details of a single volume"""
46 url = "os-volumes/%s" % str(volume_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070047 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053048 body = json.loads(body)
49 return resp, body['volume']
50
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053051 def create_volume(self, size, **kwargs):
52 """
53 Creates a new Volume.
54 size(Required): Size of volume in GB.
55 Following optional keyword arguments are accepted:
56 display_name: Optional Volume Name.
57 metadata: A dictionary of values to be used as metadata.
58 """
59 post_body = {
60 'size': size,
61 'display_name': kwargs.get('display_name'),
62 'metadata': kwargs.get('metadata'),
63 }
64
65 post_body = json.dumps({'volume': post_body})
chris fattarsi5098fa22012-04-17 13:27:00 -070066 resp, body = self.post('os-volumes', post_body, self.headers)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053067 body = json.loads(body)
68 return resp, body['volume']
69
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053070 def delete_volume(self, volume_id):
71 """Deletes the Specified Volume"""
chris fattarsi5098fa22012-04-17 13:27:00 -070072 return self.delete("os-volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053073
74 def wait_for_volume_status(self, volume_id, status):
75 """Waits for a Volume to reach a given status"""
76 resp, body = self.get_volume(volume_id)
77 volume_name = body['displayName']
78 volume_status = body['status']
79 start = int(time.time())
80
81 while volume_status != status:
82 time.sleep(self.build_interval)
83 resp, body = self.get_volume(volume_id)
84 volume_status = body['status']
85 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +053086 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053087
88 if int(time.time()) - start >= self.build_timeout:
89 message = 'Volume %s failed to reach %s status within '\
90 'the required time (%s s).' % (volume_name, status,
91 self.build_timeout)
92 raise exceptions.TimeoutException(message)