Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
| 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 | |
| 16 | from oslo_serialization import jsonutils as json |
| 17 | from six.moves.urllib import parse as urllib |
| 18 | |
| 19 | from tempest.lib.api_schema.response.compute.v2_1 import volumes as schema |
| 20 | from tempest.lib.common import rest_client |
| 21 | from tempest.lib import exceptions as lib_exc |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 22 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 23 | |
| 24 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 25 | class VolumesClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
| 27 | def list_volumes(self, detail=False, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 28 | """List all the volumes created. |
| 29 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 30 | For a full list of available parameters, please refer to the official |
| 31 | API reference: |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 32 | https://developer.openstack.org/api-ref/compute/#list-volumes |
| 33 | https://developer.openstack.org/api-ref/compute/#list-volumes-with-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 34 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 35 | url = 'os-volumes' |
| 36 | |
| 37 | if detail: |
| 38 | url += '/detail' |
| 39 | if params: |
| 40 | url += '?%s' % urllib.urlencode(params) |
| 41 | |
| 42 | resp, body = self.get(url) |
| 43 | body = json.loads(body) |
| 44 | self.validate_response(schema.list_volumes, resp, body) |
| 45 | return rest_client.ResponseBody(resp, body) |
| 46 | |
| 47 | def show_volume(self, volume_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 48 | """Return the details of a single volume. |
| 49 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 50 | For a full list of available parameters, please refer to the official |
| 51 | API reference: |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 52 | https://developer.openstack.org/api-ref/compute/#show-volume-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 53 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 54 | url = "os-volumes/%s" % volume_id |
| 55 | resp, body = self.get(url) |
| 56 | body = json.loads(body) |
| 57 | self.validate_response(schema.create_get_volume, resp, body) |
| 58 | return rest_client.ResponseBody(resp, body) |
| 59 | |
| 60 | def create_volume(self, **kwargs): |
| 61 | """Create a new Volume. |
| 62 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 63 | For a full list of available parameters, please refer to the official |
| 64 | API reference: |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 65 | https://developer.openstack.org/api-ref/compute/#create-volume |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 66 | """ |
| 67 | post_body = json.dumps({'volume': kwargs}) |
| 68 | resp, body = self.post('os-volumes', post_body) |
| 69 | body = json.loads(body) |
| 70 | self.validate_response(schema.create_get_volume, resp, body) |
| 71 | return rest_client.ResponseBody(resp, body) |
| 72 | |
| 73 | def delete_volume(self, volume_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 74 | """Delete the Specified Volume. |
| 75 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 76 | For a full list of available parameters, please refer to the official |
| 77 | API reference: |
zhufl | 8d4134b | 2017-03-08 15:04:20 +0800 | [diff] [blame] | 78 | https://developer.openstack.org/api-ref/compute/#delete-volume |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 79 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 80 | resp, body = self.delete("os-volumes/%s" % volume_id) |
| 81 | self.validate_response(schema.delete_volume, resp, body) |
| 82 | return rest_client.ResponseBody(resp, body) |
| 83 | |
| 84 | def is_resource_deleted(self, id): |
| 85 | try: |
| 86 | self.show_volume(id) |
| 87 | except lib_exc.NotFound: |
| 88 | return True |
| 89 | return False |
| 90 | |
| 91 | @property |
| 92 | def resource_type(self): |
| 93 | """Return the primary type of resource this client works with.""" |
| 94 | return 'volume' |