blob: 697e9023119987c68e692aecc09cb879402461f6 [file] [log] [blame]
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +00001# All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000015import time
16
Matthew Treinish21905512015-07-13 10:33:35 -040017from oslo_serialization import jsonutils as json
Masayuki Igawadd759082015-01-20 14:35:20 +090018from tempest_lib import exceptions as lib_exc
19
Joseph Lanoux6809bab2014-12-18 14:57:18 +000020from tempest.common import service_client
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000021from tempest import exceptions
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000022
23
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000024class BaseQosSpecsClient(service_client.ServiceClient):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000025 """Client class to send CRUD QoS API requests"""
26
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000027 def is_resource_deleted(self, qos_id):
28 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000029 self.show_qos(qos_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090030 except lib_exc.NotFound:
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000031 return True
32 return False
33
Matt Riedemannd2b96512014-10-13 10:18:16 -070034 @property
35 def resource_type(self):
36 """Returns the primary type of resource this client works with."""
37 return 'qos'
38
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000039 def wait_for_qos_operations(self, qos_id, operation, args=None):
40 """Waits for a qos operations to be completed.
41
42 NOTE : operation value is required for wait_for_qos_operations()
43 operation = 'qos-key' / 'disassociate' / 'disassociate-all'
44 args = keys[] when operation = 'qos-key'
45 args = volume-type-id disassociated when operation = 'disassociate'
46 args = None when operation = 'disassociate-all'
47 """
48 start_time = int(time.time())
49 while True:
50 if operation == 'qos-key-unset':
John Warren3836f3d2015-08-13 18:08:52 +000051 body = self.show_qos(qos_id)['qos_specs']
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000052 if not any(key in body['specs'] for key in args):
53 return
54 elif operation == 'disassociate':
John Warren3836f3d2015-08-13 18:08:52 +000055 body = self.show_association_qos(qos_id)['qos_associations']
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000056 if not any(args in body[i]['id'] for i in range(0, len(body))):
57 return
58 elif operation == 'disassociate-all':
John Warren3836f3d2015-08-13 18:08:52 +000059 body = self.show_association_qos(qos_id)['qos_associations']
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000060 if not body:
61 return
62 else:
63 msg = (" operation value is either not defined or incorrect.")
Masayuki Igawadd759082015-01-20 14:35:20 +090064 raise lib_exc.UnprocessableEntity(msg)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000065
66 if int(time.time()) - start_time >= self.build_timeout:
67 raise exceptions.TimeoutException
68 time.sleep(self.build_interval)
69
Jordan Pittier0359c4d2015-12-09 14:34:58 +010070 def create_qos(self, **kwargs):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000071 """Create a QoS Specification.
72
Jordan Pittier0359c4d2015-12-09 14:34:58 +010073 Available params: see http://developer.openstack.org/
74 api-ref-blockstorage-v2.html#createQoSSpec
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000075 """
Jordan Pittier0359c4d2015-12-09 14:34:58 +010076 post_body = json.dumps({'qos_specs': kwargs})
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000077 resp, body = self.post('qos-specs', post_body)
78 self.expected_success(200, resp.status)
79 body = json.loads(body)
John Warren3836f3d2015-08-13 18:08:52 +000080 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000081
82 def delete_qos(self, qos_id, force=False):
83 """Delete the specified QoS specification."""
84 resp, body = self.delete(
85 "qos-specs/%s?force=%s" % (str(qos_id), force))
86 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +000087 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000088
89 def list_qos(self):
90 """List all the QoS specifications created."""
91 url = 'qos-specs'
92 resp, body = self.get(url)
93 body = json.loads(body)
94 self.expected_success(200, resp.status)
John Warren3836f3d2015-08-13 18:08:52 +000095 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000096
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000097 def show_qos(self, qos_id):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000098 """Get the specified QoS specification."""
99 url = "qos-specs/%s" % str(qos_id)
100 resp, body = self.get(url)
101 body = json.loads(body)
102 self.expected_success(200, resp.status)
John Warren3836f3d2015-08-13 18:08:52 +0000103 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000104
105 def set_qos_key(self, qos_id, **kwargs):
106 """Set the specified keys/values of QoS specification.
107
Jordan Pittier0359c4d2015-12-09 14:34:58 +0100108 Available params: see http://developer.openstack.org/
109 api-ref-blockstorage-v2.html#setQoSKey
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000110 """
111 put_body = json.dumps({"qos_specs": kwargs})
112 resp, body = self.put('qos-specs/%s' % qos_id, put_body)
113 body = json.loads(body)
114 self.expected_success(200, resp.status)
John Warren3836f3d2015-08-13 18:08:52 +0000115 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000116
117 def unset_qos_key(self, qos_id, keys):
118 """Unset the specified keys of QoS specification.
119
Jordan Pittier0359c4d2015-12-09 14:34:58 +0100120 :param keys: keys to delete from the QoS specification.
121
122 TODO(jordanP): Add a link once LP #1524877 is fixed.
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000123 """
124 put_body = json.dumps({'keys': keys})
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000125 resp, body = self.put('qos-specs/%s/delete_keys' % qos_id, put_body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000126 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000127 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000128
129 def associate_qos(self, qos_id, vol_type_id):
130 """Associate the specified QoS with specified volume-type."""
131 url = "qos-specs/%s/associate" % str(qos_id)
132 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000133 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000134 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000135 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000136
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000137 def show_association_qos(self, qos_id):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000138 """Get the association of the specified QoS specification."""
139 url = "qos-specs/%s/associations" % str(qos_id)
140 resp, body = self.get(url)
141 body = json.loads(body)
142 self.expected_success(200, resp.status)
John Warren3836f3d2015-08-13 18:08:52 +0000143 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000144
145 def disassociate_qos(self, qos_id, vol_type_id):
146 """Disassociate the specified QoS with specified volume-type."""
147 url = "qos-specs/%s/disassociate" % str(qos_id)
148 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000149 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000150 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000151 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000152
153 def disassociate_all_qos(self, qos_id):
154 """Disassociate the specified QoS with all associations."""
155 url = "qos-specs/%s/disassociate_all" % str(qos_id)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000156 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000157 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000158 return service_client.ResponseBody(resp, body)