blob: f14c8b4d6c4a4ea9c9fabbb0c107bde9641b2c90 [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)
88
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)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000095 return service_client.ResponseBodyList(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +000096
97 def get_qos(self, qos_id):
98 """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)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000103 return service_client.ResponseBody(resp, body['qos_specs'])
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
108 kwargs : it is the dictionary of the key=value pairs to set
109 """
110 put_body = json.dumps({"qos_specs": kwargs})
111 resp, body = self.put('qos-specs/%s' % qos_id, put_body)
112 body = json.loads(body)
113 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000114 return service_client.ResponseBody(resp, body['qos_specs'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000115
116 def unset_qos_key(self, qos_id, keys):
117 """Unset the specified keys of QoS specification.
118
119 keys : it is the array of the keys to unset
120 """
121 put_body = json.dumps({'keys': keys})
122 resp, _ = self.put('qos-specs/%s/delete_keys' % qos_id, put_body)
123 self.expected_success(202, resp.status)
124
125 def associate_qos(self, qos_id, vol_type_id):
126 """Associate the specified QoS with specified volume-type."""
127 url = "qos-specs/%s/associate" % str(qos_id)
128 url += "?vol_type_id=%s" % vol_type_id
129 resp, _ = self.get(url)
130 self.expected_success(202, resp.status)
131
132 def get_association_qos(self, qos_id):
133 """Get the association of the specified QoS specification."""
134 url = "qos-specs/%s/associations" % str(qos_id)
135 resp, body = self.get(url)
136 body = json.loads(body)
137 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000138 return service_client.ResponseBodyList(resp, body['qos_associations'])
Swapnil Kulkarni7dba3e62014-08-14 09:05:07 +0000139
140 def disassociate_qos(self, qos_id, vol_type_id):
141 """Disassociate the specified QoS with specified volume-type."""
142 url = "qos-specs/%s/disassociate" % str(qos_id)
143 url += "?vol_type_id=%s" % vol_type_id
144 resp, _ = self.get(url)
145 self.expected_success(202, resp.status)
146
147 def disassociate_all_qos(self, qos_id):
148 """Disassociate the specified QoS with all associations."""
149 url = "qos-specs/%s/disassociate_all" % str(qos_id)
150 resp, _ = self.get(url)
151 self.expected_success(202, resp.status)
152
153
154class QosSpecsClientJSON(BaseQosSpecsClientJSON):
155 """Volume V1 QoS client."""