Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 1 | # 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-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 18 | import json |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 19 | import time |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 20 | |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 21 | from tempest.common.rest_client import RestClient |
| 22 | from tempest import exceptions |
| 23 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 24 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 25 | class VolumesClient(RestClient): |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 26 | """ |
| 27 | Client class to send CRUD Volume API requests to a Cinder endpoint |
| 28 | """ |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 29 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 30 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 31 | super(VolumesClient, self).__init__(config, username, password, |
| 32 | auth_url, tenant_name) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 33 | |
| 34 | self.service = self.config.volume.catalog_type |
| 35 | self.build_interval = self.config.volume.build_interval |
| 36 | self.build_timeout = self.config.volume.build_timeout |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 37 | |
| 38 | def list_volumes(self, params=None): |
| 39 | """List all the volumes created""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 40 | url = 'volumes' |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 41 | if params != None: |
| 42 | param_list = [] |
| 43 | for param, value in params.iteritems(): |
| 44 | param_list.append("%s=%s&" % (param, value)) |
| 45 | |
| 46 | url += '?' + ' '.join(param_list) |
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['volumes'] |
| 50 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 51 | def list_volumes_with_detail(self, params=None): |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 52 | """List the details of all volumes""" |
| 53 | url = 'volumes/detail' |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 54 | if params != None: |
| 55 | param_list = [] |
| 56 | for param, value in params.iteritems(): |
| 57 | param_list.append("%s=%s&" % (param, value)) |
| 58 | |
| 59 | url = '?' + ' '.join(param_list) |
| 60 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 61 | resp, body = self.get(url) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 62 | body = json.loads(body) |
| 63 | return resp, body['volumes'] |
| 64 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 65 | def get_volume(self, volume_id): |
| 66 | """Returns the details of a single volume""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 67 | url = "volumes/%s" % str(volume_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 68 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 69 | body = json.loads(body) |
| 70 | return resp, body['volume'] |
| 71 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 72 | def create_volume(self, size, **kwargs): |
| 73 | """ |
| 74 | Creates a new Volume. |
| 75 | size(Required): Size of volume in GB. |
| 76 | Following optional keyword arguments are accepted: |
| 77 | display_name: Optional Volume Name. |
| 78 | metadata: A dictionary of values to be used as metadata. |
| 79 | """ |
| 80 | post_body = { |
| 81 | 'size': size, |
| 82 | 'display_name': kwargs.get('display_name'), |
| 83 | 'metadata': kwargs.get('metadata'), |
| 84 | } |
| 85 | |
| 86 | post_body = json.dumps({'volume': post_body}) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 87 | resp, body = self.post('volumes', post_body, self.headers) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 88 | body = json.loads(body) |
| 89 | return resp, body['volume'] |
| 90 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 91 | def delete_volume(self, volume_id): |
| 92 | """Deletes the Specified Volume""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 93 | return self.delete("volumes/%s" % str(volume_id)) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 94 | |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame^] | 95 | def attach_volume(self, volume_id, instance_uuid, mountpoint): |
| 96 | """Attaches a volume to a given instance on a given mountpoint""" |
| 97 | post_body = { |
| 98 | 'instance_uuid': instance_uuid, |
| 99 | 'mountpoint': mountpoint |
| 100 | } |
| 101 | post_body = json.dumps({'os-attach': post_body}) |
| 102 | url = 'volumes/%s/action' % (volume_id) |
| 103 | resp, body = self.post(url, post_body, self.headers) |
| 104 | return resp, body |
| 105 | |
| 106 | def detach_volume(self, volume_id): |
| 107 | """Detaches a volume from an instance""" |
| 108 | post_body = {} |
| 109 | post_body = json.dumps({'os-detach': post_body}) |
| 110 | url = 'volumes/%s/action' % (volume_id) |
| 111 | resp, body = self.post(url, post_body, self.headers) |
| 112 | return resp, body |
| 113 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 114 | def wait_for_volume_status(self, volume_id, status): |
| 115 | """Waits for a Volume to reach a given status""" |
| 116 | resp, body = self.get_volume(volume_id) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 117 | volume_name = body['display_name'] |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 118 | volume_status = body['status'] |
| 119 | start = int(time.time()) |
| 120 | |
| 121 | while volume_status != status: |
| 122 | time.sleep(self.build_interval) |
| 123 | resp, body = self.get_volume(volume_id) |
| 124 | volume_status = body['status'] |
| 125 | if volume_status == 'error': |
rajalakshmi-ganesan | e3bb58f | 2012-05-16 12:01:15 +0530 | [diff] [blame] | 126 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 127 | |
| 128 | if int(time.time()) - start >= self.build_timeout: |
| 129 | message = 'Volume %s failed to reach %s status within '\ |
| 130 | 'the required time (%s s).' % (volume_name, status, |
| 131 | self.build_timeout) |
| 132 | raise exceptions.TimeoutException(message) |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 133 | |
| 134 | def is_resource_deleted(self, id): |
| 135 | try: |
| 136 | self.get_volume(id) |
| 137 | except exceptions.NotFound: |
| 138 | return True |
| 139 | return False |