rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 1 | from tempest import exceptions |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 2 | from tempest.common.rest_client import RestClient |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 3 | import json |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 4 | import time |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 5 | |
| 6 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 7 | class VolumesClient(RestClient): |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 8 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 9 | 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-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 13 | self.build_interval = self.config.compute.build_interval |
| 14 | self.build_timeout = self.config.compute.build_timeout |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 15 | |
| 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 fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 26 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 27 | body = json.loads(body) |
| 28 | return resp, body['volumes'] |
| 29 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 30 | 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 fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 40 | resp, body = self.get(url) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 41 | body = json.loads(body) |
| 42 | return resp, body['volumes'] |
| 43 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 44 | def get_volume(self, volume_id): |
| 45 | """Returns the details of a single volume""" |
| 46 | url = "os-volumes/%s" % str(volume_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 47 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 48 | body = json.loads(body) |
| 49 | return resp, body['volume'] |
| 50 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 51 | 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 fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 66 | resp, body = self.post('os-volumes', post_body, self.headers) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 67 | body = json.loads(body) |
| 68 | return resp, body['volume'] |
| 69 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 70 | def delete_volume(self, volume_id): |
| 71 | """Deletes the Specified Volume""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 72 | return self.delete("os-volumes/%s" % str(volume_id)) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 73 | |
| 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-ganesan | e3bb58f | 2012-05-16 12:01:15 +0530 | [diff] [blame^] | 86 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 87 | |
| 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) |