blob: e3d6a292522531fc7fd105cb07094c56961bca37 [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':
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000051 body = self.show_qos(qos_id)
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':
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000055 body = self.show_association_qos(qos_id)
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':
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000059 body = self.show_association_qos(qos_id)
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
70 def create_qos(self, name, consumer, **kwargs):
71 """Create a QoS Specification.
72
73 name : name of the QoS specifications
74 consumer : conumer of Qos ( front-end / back-end / both )
75 """
76 post_body = {'name': name, 'consumer': consumer}
77 post_body.update(kwargs)
78 post_body = json.dumps({'qos_specs': post_body})
79 resp, body = self.post('qos-specs', post_body)
80 self.expected_success(200, resp.status)
81 body = json.loads(body)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000082 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000083
84 def delete_qos(self, qos_id, force=False):
85 """Delete the specified QoS specification."""
86 resp, body = self.delete(
87 "qos-specs/%s?force=%s" % (str(qos_id), force))
88 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +000089 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000090
91 def list_qos(self):
92 """List all the QoS specifications created."""
93 url = 'qos-specs'
94 resp, body = self.get(url)
95 body = json.loads(body)
96 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000097 return service_client.ResponseBodyList(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000098
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000099 def show_qos(self, qos_id):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000100 """Get the specified QoS specification."""
101 url = "qos-specs/%s" % str(qos_id)
102 resp, body = self.get(url)
103 body = json.loads(body)
104 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000105 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000106
107 def set_qos_key(self, qos_id, **kwargs):
108 """Set the specified keys/values of QoS specification.
109
110 kwargs : it is the dictionary of the key=value pairs to set
111 """
112 put_body = json.dumps({"qos_specs": kwargs})
113 resp, body = self.put('qos-specs/%s' % qos_id, put_body)
114 body = json.loads(body)
115 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000116 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000117
118 def unset_qos_key(self, qos_id, keys):
119 """Unset the specified keys of QoS specification.
120
121 keys : it is the array of the keys to unset
122 """
123 put_body = json.dumps({'keys': keys})
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000124 resp, body = self.put('qos-specs/%s/delete_keys' % qos_id, put_body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000125 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000126 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000127
128 def associate_qos(self, qos_id, vol_type_id):
129 """Associate the specified QoS with specified volume-type."""
130 url = "qos-specs/%s/associate" % str(qos_id)
131 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000132 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000133 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000134 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000135
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000136 def show_association_qos(self, qos_id):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000137 """Get the association of the specified QoS specification."""
138 url = "qos-specs/%s/associations" % str(qos_id)
139 resp, body = self.get(url)
140 body = json.loads(body)
141 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000142 return service_client.ResponseBodyList(resp, body['qos_associations'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000143
144 def disassociate_qos(self, qos_id, vol_type_id):
145 """Disassociate the specified QoS with specified volume-type."""
146 url = "qos-specs/%s/disassociate" % str(qos_id)
147 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000148 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000149 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000150 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000151
152 def disassociate_all_qos(self, qos_id):
153 """Disassociate the specified QoS with all associations."""
154 url = "qos-specs/%s/disassociate_all" % str(qos_id)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000155 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000156 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000157 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000158
159
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000160class QosSpecsClient(BaseQosSpecsClient):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000161 """Volume V1 QoS client."""