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 | |
| 15 | import json |
| 16 | import time |
| 17 | |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 18 | from tempest.common import service_client |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 19 | from tempest import exceptions |
Ken'ichi Ohmichi | a39d0be | 2014-12-17 08:46:11 +0000 | [diff] [blame] | 20 | from tempest.services.volume.json import base |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 21 | |
| 22 | |
Ken'ichi Ohmichi | a39d0be | 2014-12-17 08:46:11 +0000 | [diff] [blame] | 23 | class BaseQosSpecsClientJSON(base.VolumeClient): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 24 | """Client class to send CRUD QoS API requests""" |
| 25 | |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 26 | 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 Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 33 | @property |
| 34 | def resource_type(self): |
| 35 | """Returns the primary type of resource this client works with.""" |
| 36 | return 'qos' |
| 37 | |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 38 | 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 50 | body = self.get_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 51 | if not any(key in body['specs'] for key in args): |
| 52 | return |
| 53 | elif operation == 'disassociate': |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 54 | body = self.get_association_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 55 | if not any(args in body[i]['id'] for i in range(0, len(body))): |
| 56 | return |
| 57 | elif operation == 'disassociate-all': |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 58 | body = self.get_association_qos(qos_id) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 59 | 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 81 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 82 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 88 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 89 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 96 | return service_client.ResponseBodyList(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 97 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 104 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 105 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 115 | return service_client.ResponseBody(resp, body['qos_specs']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 116 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 123 | 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] | 124 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 125 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 126 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 131 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 132 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 133 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 134 | |
| 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 Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 141 | return service_client.ResponseBodyList(resp, body['qos_associations']) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 142 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 147 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 148 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 149 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 150 | |
| 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 Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 154 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 155 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 156 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | class QosSpecsClientJSON(BaseQosSpecsClientJSON): |
| 160 | """Volume V1 QoS client.""" |