ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 16 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 18 | from tempest_lib import exceptions as lib_exc |
| 19 | |
ghanshyam | aa93b4b | 2015-03-20 11:03:44 +0900 | [diff] [blame] | 20 | from tempest.api_schema.response.compute.v2_1 import volumes as schema |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 21 | from tempest.common import service_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 23 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 24 | class VolumesExtensionsClient(service_client.ServiceClient): |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 25 | |
Markus Zoeller | 3d2a21c | 2015-02-27 12:04:22 +0100 | [diff] [blame] | 26 | def __init__(self, auth_provider, service, region, |
| 27 | default_volume_size=1, **kwargs): |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 28 | super(VolumesExtensionsClient, self).__init__( |
Markus Zoeller | 3d2a21c | 2015-02-27 12:04:22 +0100 | [diff] [blame] | 29 | auth_provider, service, region, **kwargs) |
| 30 | self.default_volume_size = default_volume_size |
| 31 | |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 32 | def list_volumes(self, detail=False, **params): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 33 | """List all the volumes created.""" |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 34 | url = 'os-volumes' |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 35 | |
| 36 | if detail: |
| 37 | url += '/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 38 | if params: |
| 39 | url += '?%s' % urllib.urlencode(params) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 40 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 41 | resp, body = self.get(url) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 42 | body = json.loads(body) |
Ghanshyam | e8940da | 2014-03-24 15:06:30 +0900 | [diff] [blame] | 43 | self.validate_response(schema.list_volumes, resp, body) |
David Kranz | 3ebc721 | 2015-02-10 12:19:19 -0500 | [diff] [blame] | 44 | return service_client.ResponseBodyList(resp, body['volumes']) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 45 | |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 46 | def show_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 47 | """Returns the details of a single volume.""" |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 48 | url = "os-volumes/%s" % volume_id |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [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) |
Ghanshyam | b4ffd76 | 2014-03-27 10:22:36 +0900 | [diff] [blame] | 51 | self.validate_response(schema.create_get_volume, resp, body) |
David Kranz | 3ebc721 | 2015-02-10 12:19:19 -0500 | [diff] [blame] | 52 | return service_client.ResponseBody(resp, body['volume']) |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 53 | |
Markus Zoeller | 3d2a21c | 2015-02-27 12:04:22 +0100 | [diff] [blame] | 54 | def create_volume(self, size=None, **kwargs): |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 55 | """ |
| 56 | Creates a new Volume. |
| 57 | size(Required): Size of volume in GB. |
| 58 | Following optional keyword arguments are accepted: |
| 59 | display_name: Optional Volume Name. |
| 60 | metadata: A dictionary of values to be used as metadata. |
| 61 | """ |
Markus Zoeller | 3d2a21c | 2015-02-27 12:04:22 +0100 | [diff] [blame] | 62 | if size is None: |
| 63 | size = self.default_volume_size |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 64 | post_body = { |
Ken'ichi Ohmichi | f6bf3d5 | 2014-11-27 07:33:55 +0000 | [diff] [blame] | 65 | 'size': size |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 66 | } |
Ken'ichi Ohmichi | f6bf3d5 | 2014-11-27 07:33:55 +0000 | [diff] [blame] | 67 | post_body.update(kwargs) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 68 | |
| 69 | post_body = json.dumps({'volume': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 70 | resp, body = self.post('os-volumes', post_body) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 71 | body = json.loads(body) |
Ghanshyam | b4ffd76 | 2014-03-27 10:22:36 +0900 | [diff] [blame] | 72 | self.validate_response(schema.create_get_volume, resp, body) |
David Kranz | 3ebc721 | 2015-02-10 12:19:19 -0500 | [diff] [blame] | 73 | return service_client.ResponseBody(resp, body['volume']) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 74 | |
rajalakshmi-ganesan | ddd9e0e | 2012-03-21 00:49:22 +0530 | [diff] [blame] | 75 | def delete_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 76 | """Deletes the Specified Volume.""" |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 77 | resp, body = self.delete("os-volumes/%s" % volume_id) |
Ghanshyam | b4ffd76 | 2014-03-27 10:22:36 +0900 | [diff] [blame] | 78 | self.validate_response(schema.delete_volume, resp, body) |
David Kranz | 3ebc721 | 2015-02-10 12:19:19 -0500 | [diff] [blame] | 79 | return service_client.ResponseBody(resp, body) |
rajalakshmi-ganesan | b446557 | 2012-03-22 01:22:50 +0530 | [diff] [blame] | 80 | |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 81 | def is_resource_deleted(self, id): |
| 82 | try: |
Ken'ichi Ohmichi | 5f448a5 | 2015-07-01 06:26:30 +0000 | [diff] [blame] | 83 | self.show_volume(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 84 | except lib_exc.NotFound: |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 85 | return True |
| 86 | return False |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 87 | |
| 88 | @property |
| 89 | def resource_type(self): |
| 90 | """Returns the primary type of resource this client works with.""" |
| 91 | return 'volume' |