blob: 32555eb8d74c6293401ec956b4e6e2a715065fb0 [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
15import json
16import time
17
Joseph Lanoux6809bab2014-12-18 14:57:18 +000018from tempest.common import service_client
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000019from tempest import exceptions
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000020from tempest.services.volume.json import base
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000021
22
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000023class BaseQosSpecsClientJSON(base.VolumeClient):
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000024 """Client class to send CRUD QoS API requests"""
25
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000026 def is_resource_deleted(self, qos_id):
27 try:
28 self.get_qos(qos_id)
29 except exceptions.NotFound:
30 return True
31 return False
32
Matt Riedemannd2b96512014-10-13 10:18:16 -070033 @property
34 def resource_type(self):
35 """Returns the primary type of resource this client works with."""
36 return 'qos'
37
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000038 def wait_for_qos_operations(self, qos_id, operation, args=None):
39 """Waits for a qos operations to be completed.
40
41 NOTE : operation value is required for wait_for_qos_operations()
42 operation = 'qos-key' / 'disassociate' / 'disassociate-all'
43 args = keys[] when operation = 'qos-key'
44 args = volume-type-id disassociated when operation = 'disassociate'
45 args = None when operation = 'disassociate-all'
46 """
47 start_time = int(time.time())
48 while True:
49 if operation == 'qos-key-unset':
Joseph Lanoux6809bab2014-12-18 14:57:18 +000050 body = self.get_qos(qos_id)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000051 if not any(key in body['specs'] for key in args):
52 return
53 elif operation == 'disassociate':
Joseph Lanoux6809bab2014-12-18 14:57:18 +000054 body = self.get_association_qos(qos_id)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000055 if not any(args in body[i]['id'] for i in range(0, len(body))):
56 return
57 elif operation == 'disassociate-all':
Joseph Lanoux6809bab2014-12-18 14:57:18 +000058 body = self.get_association_qos(qos_id)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000059 if not body:
60 return
61 else:
62 msg = (" operation value is either not defined or incorrect.")
63 raise exceptions.UnprocessableEntity(msg)
64
65 if int(time.time()) - start_time >= self.build_timeout:
66 raise exceptions.TimeoutException
67 time.sleep(self.build_interval)
68
69 def create_qos(self, name, consumer, **kwargs):
70 """Create a QoS Specification.
71
72 name : name of the QoS specifications
73 consumer : conumer of Qos ( front-end / back-end / both )
74 """
75 post_body = {'name': name, 'consumer': consumer}
76 post_body.update(kwargs)
77 post_body = json.dumps({'qos_specs': post_body})
78 resp, body = self.post('qos-specs', post_body)
79 self.expected_success(200, resp.status)
80 body = json.loads(body)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000081 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000082
83 def delete_qos(self, qos_id, force=False):
84 """Delete the specified QoS specification."""
85 resp, body = self.delete(
86 "qos-specs/%s?force=%s" % (str(qos_id), force))
87 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +000088 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000089
90 def list_qos(self):
91 """List all the QoS specifications created."""
92 url = 'qos-specs'
93 resp, body = self.get(url)
94 body = json.loads(body)
95 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000096 return service_client.ResponseBodyList(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000097
98 def get_qos(self, qos_id):
99 """Get the specified QoS specification."""
100 url = "qos-specs/%s" % str(qos_id)
101 resp, body = self.get(url)
102 body = json.loads(body)
103 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000104 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000105
106 def set_qos_key(self, qos_id, **kwargs):
107 """Set the specified keys/values of QoS specification.
108
109 kwargs : it is the dictionary of the key=value pairs to set
110 """
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)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000115 return service_client.ResponseBody(resp, body['qos_specs'])
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
120 keys : it is the array of the keys to unset
121 """
122 put_body = json.dumps({'keys': keys})
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000123 resp, body = self.put('qos-specs/%s/delete_keys' % qos_id, put_body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000124 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000125 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000126
127 def associate_qos(self, qos_id, vol_type_id):
128 """Associate the specified QoS with specified volume-type."""
129 url = "qos-specs/%s/associate" % str(qos_id)
130 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000131 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000132 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000133 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000134
135 def get_association_qos(self, qos_id):
136 """Get the association of the specified QoS specification."""
137 url = "qos-specs/%s/associations" % str(qos_id)
138 resp, body = self.get(url)
139 body = json.loads(body)
140 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000141 return service_client.ResponseBodyList(resp, body['qos_associations'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000142
143 def disassociate_qos(self, qos_id, vol_type_id):
144 """Disassociate the specified QoS with specified volume-type."""
145 url = "qos-specs/%s/disassociate" % str(qos_id)
146 url += "?vol_type_id=%s" % vol_type_id
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000147 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000148 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000149 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000150
151 def disassociate_all_qos(self, qos_id):
152 """Disassociate the specified QoS with all associations."""
153 url = "qos-specs/%s/disassociate_all" % str(qos_id)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000154 resp, body = self.get(url)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000155 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000156 return service_client.ResponseBody(resp, body)
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000157
158
159class QosSpecsClientJSON(BaseQosSpecsClientJSON):
160 """Volume V1 QoS client."""