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 |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 20 | import urllib |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 21 | |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 22 | from tempest.common.rest_client import RestClient |
| 23 | from tempest import exceptions |
| 24 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 25 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 26 | class VolumesClientJSON(RestClient): |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 27 | """ |
| 28 | Client class to send CRUD Volume API requests to a Cinder endpoint |
| 29 | """ |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 30 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 31 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 32 | super(VolumesClientJSON, self).__init__(config, username, password, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 33 | auth_url, tenant_name) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 34 | |
| 35 | self.service = self.config.volume.catalog_type |
| 36 | self.build_interval = self.config.volume.build_interval |
| 37 | self.build_timeout = self.config.volume.build_timeout |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 38 | |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 39 | def get_attachment_from_volume(self, volume): |
| 40 | """Return the element 'attachment' from input volumes.""" |
| 41 | return volume['attachments'][0] |
| 42 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 43 | def list_volumes(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 44 | """List all the volumes created.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 45 | url = 'volumes' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 46 | if params: |
| 47 | url += '?%s' % urllib.urlencode(params) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 48 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 49 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 50 | body = json.loads(body) |
| 51 | return resp, body['volumes'] |
| 52 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 53 | def list_volumes_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 54 | """List the details of all volumes.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 55 | url = 'volumes/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 56 | if params: |
| 57 | url += '?%s' % urllib.urlencode(params) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 58 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 59 | resp, body = self.get(url) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 60 | body = json.loads(body) |
| 61 | return resp, body['volumes'] |
| 62 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 63 | def get_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 64 | """Returns the details of a single volume.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 65 | url = "volumes/%s" % str(volume_id) |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 66 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 67 | body = json.loads(body) |
| 68 | return resp, body['volume'] |
| 69 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 70 | def create_volume(self, size, **kwargs): |
| 71 | """ |
| 72 | Creates a new Volume. |
| 73 | size(Required): Size of volume in GB. |
| 74 | Following optional keyword arguments are accepted: |
| 75 | display_name: Optional Volume Name. |
| 76 | metadata: A dictionary of values to be used as metadata. |
Rohan Rhishikesh Kanade | c316f0a | 2012-12-04 05:44:39 -0800 | [diff] [blame] | 77 | volume_type: Optional Name of volume_type for the volume |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 78 | snapshot_id: When specified the volume is created from this snapshot |
Giulio Fidente | 36836c4 | 2013-04-05 15:43:51 +0200 | [diff] [blame] | 79 | imageRef: When specified the volume is created from this image |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 80 | """ |
Attila Fazekas | 36b1fcf | 2013-01-31 16:41:04 +0100 | [diff] [blame] | 81 | post_body = {'size': size} |
| 82 | post_body.update(kwargs) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 83 | post_body = json.dumps({'volume': post_body}) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 84 | resp, body = self.post('volumes', post_body, self.headers) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 85 | body = json.loads(body) |
| 86 | return resp, body['volume'] |
| 87 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 88 | def delete_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 89 | """Deletes the Specified Volume.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 90 | return self.delete("volumes/%s" % str(volume_id)) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 91 | |
Giulio Fidente | 884e9da | 2013-06-21 17:25:42 +0200 | [diff] [blame] | 92 | def upload_volume(self, volume_id, image_name): |
| 93 | """Uploads a volume in Glance.""" |
| 94 | post_body = { |
| 95 | 'image_name': image_name, |
| 96 | } |
| 97 | post_body = json.dumps({'os-volume_upload_image': post_body}) |
| 98 | url = 'volumes/%s/action' % (volume_id) |
| 99 | resp, body = self.post(url, post_body, self.headers) |
| 100 | body = json.loads(body) |
| 101 | return resp, body['os-volume_upload_image'] |
| 102 | |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 103 | def attach_volume(self, volume_id, instance_uuid, mountpoint): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 104 | """Attaches a volume to a given instance on a given mountpoint.""" |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 105 | post_body = { |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 106 | 'instance_uuid': instance_uuid, |
| 107 | 'mountpoint': mountpoint, |
| 108 | } |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 109 | post_body = json.dumps({'os-attach': post_body}) |
| 110 | url = 'volumes/%s/action' % (volume_id) |
| 111 | resp, body = self.post(url, post_body, self.headers) |
| 112 | return resp, body |
| 113 | |
| 114 | def detach_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 115 | """Detaches a volume from an instance.""" |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 116 | post_body = {} |
| 117 | post_body = json.dumps({'os-detach': post_body}) |
| 118 | url = 'volumes/%s/action' % (volume_id) |
| 119 | resp, body = self.post(url, post_body, self.headers) |
| 120 | return resp, body |
| 121 | |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 122 | def wait_for_volume_status(self, volume_id, status): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 123 | """Waits for a Volume to reach a given status.""" |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 124 | resp, body = self.get_volume(volume_id) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 125 | volume_name = body['display_name'] |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 126 | volume_status = body['status'] |
| 127 | start = int(time.time()) |
| 128 | |
| 129 | while volume_status != status: |
| 130 | time.sleep(self.build_interval) |
| 131 | resp, body = self.get_volume(volume_id) |
| 132 | volume_status = body['status'] |
| 133 | if volume_status == 'error': |
rajalakshmi-ganesan | e3bb58f | 2012-05-16 12:01:15 +0530 | [diff] [blame] | 134 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 135 | |
| 136 | if int(time.time()) - start >= self.build_timeout: |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 137 | message = ('Volume %s failed to reach %s status within ' |
| 138 | 'the required time (%s s).' % |
| 139 | (volume_name, status, self.build_timeout)) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 140 | raise exceptions.TimeoutException(message) |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 141 | |
| 142 | def is_resource_deleted(self, id): |
| 143 | try: |
Attila Fazekas | f53172c | 2013-01-26 01:04:42 +0100 | [diff] [blame] | 144 | self.get_volume(id) |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 145 | except exceptions.NotFound: |
| 146 | return True |
| 147 | return False |