Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 1 | # 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 Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 15 | import time |
| 16 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 17 | from oslo_serialization import jsonutils as json |
Masayuki Igawa | dd75908 | 2015-01-20 14:35:20 +0900 | [diff] [blame] | 18 | from tempest_lib import exceptions as lib_exc |
| 19 | |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 20 | from tempest.common import service_client |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 21 | from tempest import exceptions |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 22 | |
| 23 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 24 | class BaseQosSpecsClient(service_client.ServiceClient): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 25 | """Client class to send CRUD QoS API requests""" |
| 26 | |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 27 | def is_resource_deleted(self, qos_id): |
| 28 | try: |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 29 | self.show_qos(qos_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 30 | except lib_exc.NotFound: |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 31 | return True |
| 32 | return False |
| 33 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 34 | @property |
| 35 | def resource_type(self): |
| 36 | """Returns the primary type of resource this client works with.""" |
| 37 | return 'qos' |
| 38 | |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 39 | 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 Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 51 | body = self.show_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 52 | if not any(key in body['specs'] for key in args): |
| 53 | return |
| 54 | elif operation == 'disassociate': |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 55 | body = self.show_association_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 56 | if not any(args in body[i]['id'] for i in range(0, len(body))): |
| 57 | return |
| 58 | elif operation == 'disassociate-all': |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 59 | body = self.show_association_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 60 | if not body: |
| 61 | return |
| 62 | else: |
| 63 | msg = (" operation value is either not defined or incorrect.") |
Masayuki Igawa | dd75908 | 2015-01-20 14:35:20 +0900 | [diff] [blame] | 64 | raise lib_exc.UnprocessableEntity(msg) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 65 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 82 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 83 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 89 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 90 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 97 | return service_client.ResponseBodyList(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 98 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 99 | def show_qos(self, qos_id): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 100 | """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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 105 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 106 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 116 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 117 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 124 | resp, body = self.put('qos-specs/%s/delete_keys' % qos_id, put_body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 125 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 126 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 127 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 132 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 133 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 134 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 135 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 136 | def show_association_qos(self, qos_id): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 137 | """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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 142 | return service_client.ResponseBodyList(resp, body['qos_associations']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 143 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 148 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 149 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 150 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 151 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 155 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 156 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 157 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 158 | |
| 159 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 160 | class QosSpecsClient(BaseQosSpecsClient): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 161 | """Volume V1 QoS client.""" |