blob: ac55049ea67986d4f4898d0ea7b81eb151f9b18f [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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 Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090018from tempest_lib import exceptions as lib_exc
19
ghanshyamaa93b4b2015-03-20 11:03:44 +090020from tempest.api_schema.response.compute.v2_1 import volumes as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000021from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053023
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000024class VolumesExtensionsClient(service_client.ServiceClient):
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053025
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010026 def __init__(self, auth_provider, service, region,
27 default_volume_size=1, **kwargs):
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000028 super(VolumesExtensionsClient, self).__init__(
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010029 auth_provider, service, region, **kwargs)
30 self.default_volume_size = default_volume_size
31
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000032 def list_volumes(self, detail=False, **params):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """List all the volumes created."""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053034 url = 'os-volumes'
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000035
36 if detail:
37 url += '/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050038 if params:
39 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053040
chris fattarsi5098fa22012-04-17 13:27:00 -070041 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053042 body = json.loads(body)
Ghanshyame8940da2014-03-24 15:06:30 +090043 self.validate_response(schema.list_volumes, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050044 return service_client.ResponseBodyList(resp, body['volumes'])
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053045
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000046 def show_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """Returns the details of a single volume."""
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000048 url = "os-volumes/%s" % volume_id
Attila Fazekasb8aa7592013-01-26 01:25:45 +010049 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053050 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090051 self.validate_response(schema.create_get_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050052 return service_client.ResponseBody(resp, body['volume'])
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053053
Markus Zoeller3d2a21c2015-02-27 12:04:22 +010054 def create_volume(self, size=None, **kwargs):
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053055 """
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 Zoeller3d2a21c2015-02-27 12:04:22 +010062 if size is None:
63 size = self.default_volume_size
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053064 post_body = {
Ken'ichi Ohmichif6bf3d52014-11-27 07:33:55 +000065 'size': size
Zhongyue Luo30a563f2012-09-30 23:43:50 +090066 }
Ken'ichi Ohmichif6bf3d52014-11-27 07:33:55 +000067 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053068
69 post_body = json.dumps({'volume': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020070 resp, body = self.post('os-volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053071 body = json.loads(body)
Ghanshyamb4ffd762014-03-27 10:22:36 +090072 self.validate_response(schema.create_get_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050073 return service_client.ResponseBody(resp, body['volume'])
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053074
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053075 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050076 """Deletes the Specified Volume."""
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000077 resp, body = self.delete("os-volumes/%s" % volume_id)
Ghanshyamb4ffd762014-03-27 10:22:36 +090078 self.validate_response(schema.delete_volume, resp, body)
David Kranz3ebc7212015-02-10 12:19:19 -050079 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053080
David Kranz6aceb4a2012-06-05 14:05:45 -040081 def is_resource_deleted(self, id):
82 try:
Ken'ichi Ohmichi5f448a52015-07-01 06:26:30 +000083 self.show_volume(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090084 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -040085 return True
86 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -070087
88 @property
89 def resource_type(self):
90 """Returns the primary type of resource this client works with."""
91 return 'volume'