blob: 95cdd5379aa0556ec71cfaf7c2552c590137b20d [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import volumes as schema
20from tempest.lib.common import rest_client
21from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090022from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023
24
Ghanshyamee9af302016-02-25 06:12:43 +090025class VolumesClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
27 def list_volumes(self, detail=False, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080028 """List all the volumes created.
29
Dong Mad12c2332016-10-19 01:36:27 -070030 For a full list of available parameters, please refer to the official
31 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080032 https://developer.openstack.org/api-ref/compute/#list-volumes
33 https://developer.openstack.org/api-ref/compute/#list-volumes-with-details
Lv Fumei7e326332016-07-08 15:18:03 +080034 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050035 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 Fumei7e326332016-07-08 15:18:03 +080048 """Return the details of a single volume.
49
Dong Mad12c2332016-10-19 01:36:27 -070050 For a full list of available parameters, please refer to the official
51 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080052 https://developer.openstack.org/api-ref/compute/#show-volume-details
Lv Fumei7e326332016-07-08 15:18:03 +080053 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050054 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 Mad12c2332016-10-19 01:36:27 -070063 For a full list of available parameters, please refer to the official
64 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080065 https://developer.openstack.org/api-ref/compute/#create-volume
Matthew Treinish9e26ca82016-02-23 11:43:20 -050066 """
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 Fumei7e326332016-07-08 15:18:03 +080074 """Delete the Specified Volume.
75
Dong Mad12c2332016-10-19 01:36:27 -070076 For a full list of available parameters, please refer to the official
77 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080078 https://developer.openstack.org/api-ref/compute/#delete-volume
Lv Fumei7e326332016-07-08 15:18:03 +080079 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050080 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'