blob: 2787779fcc96fd2dc92686d1491b8f288cd2e701 [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
30 Available params: see http://developer.openstack.org/
31 api-ref-compute-v2.1.html#listVolumes
32 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050033 url = 'os-volumes'
34
35 if detail:
36 url += '/detail'
37 if params:
38 url += '?%s' % urllib.urlencode(params)
39
40 resp, body = self.get(url)
41 body = json.loads(body)
42 self.validate_response(schema.list_volumes, resp, body)
43 return rest_client.ResponseBody(resp, body)
44
45 def show_volume(self, volume_id):
Lv Fumei7e326332016-07-08 15:18:03 +080046 """Return the details of a single volume.
47
48 Available params: see http://developer.openstack.org/
49 api-ref-compute-v2.1.html#showVolume
50 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050051 url = "os-volumes/%s" % volume_id
52 resp, body = self.get(url)
53 body = json.loads(body)
54 self.validate_response(schema.create_get_volume, resp, body)
55 return rest_client.ResponseBody(resp, body)
56
57 def create_volume(self, **kwargs):
58 """Create a new Volume.
59
60 Available params: see http://developer.openstack.org/
61 api-ref-compute-v2.1.html#createVolume
62 """
63 post_body = json.dumps({'volume': kwargs})
64 resp, body = self.post('os-volumes', post_body)
65 body = json.loads(body)
66 self.validate_response(schema.create_get_volume, resp, body)
67 return rest_client.ResponseBody(resp, body)
68
69 def delete_volume(self, volume_id):
Lv Fumei7e326332016-07-08 15:18:03 +080070 """Delete the Specified Volume.
71
72 Available params: see http://developer.openstack.org/
73 api-ref-compute-v2.1.html#deleteVolume
74 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050075 resp, body = self.delete("os-volumes/%s" % volume_id)
76 self.validate_response(schema.delete_volume, resp, body)
77 return rest_client.ResponseBody(resp, body)
78
79 def is_resource_deleted(self, id):
80 try:
81 self.show_volume(id)
82 except lib_exc.NotFound:
83 return True
84 return False
85
86 @property
87 def resource_type(self):
88 """Return the primary type of resource this client works with."""
89 return 'volume'