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': |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 51 | body = self.show_qos(qos_id)['qos_specs'] |
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': |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 55 | body = self.show_association_qos(qos_id)['qos_associations'] |
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': |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 59 | body = self.show_association_qos(qos_id)['qos_associations'] |
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 | |
Jordan Pittier | 0359c4d | 2015-12-09 14:34:58 +0100 | [diff] [blame^] | 70 | def create_qos(self, **kwargs): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 71 | """Create a QoS Specification. |
| 72 | |
Jordan Pittier | 0359c4d | 2015-12-09 14:34:58 +0100 | [diff] [blame^] | 73 | Available params: see http://developer.openstack.org/ |
| 74 | api-ref-blockstorage-v2.html#createQoSSpec |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 75 | """ |
Jordan Pittier | 0359c4d | 2015-12-09 14:34:58 +0100 | [diff] [blame^] | 76 | post_body = json.dumps({'qos_specs': kwargs}) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 77 | resp, body = self.post('qos-specs', post_body) |
| 78 | self.expected_success(200, resp.status) |
| 79 | body = json.loads(body) |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 80 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 81 | |
| 82 | def delete_qos(self, qos_id, force=False): |
| 83 | """Delete the specified QoS specification.""" |
| 84 | resp, body = self.delete( |
| 85 | "qos-specs/%s?force=%s" % (str(qos_id), force)) |
| 86 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 87 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 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) |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 95 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 96 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 97 | def show_qos(self, qos_id): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 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) |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 103 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 104 | |
| 105 | def set_qos_key(self, qos_id, **kwargs): |
| 106 | """Set the specified keys/values of QoS specification. |
| 107 | |
Jordan Pittier | 0359c4d | 2015-12-09 14:34:58 +0100 | [diff] [blame^] | 108 | Available params: see http://developer.openstack.org/ |
| 109 | api-ref-blockstorage-v2.html#setQoSKey |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 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) |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 115 | return service_client.ResponseBody(resp, body) |
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 | |
Jordan Pittier | 0359c4d | 2015-12-09 14:34:58 +0100 | [diff] [blame^] | 120 | :param keys: keys to delete from the QoS specification. |
| 121 | |
| 122 | TODO(jordanP): Add a link once LP #1524877 is fixed. |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 123 | """ |
| 124 | put_body = json.dumps({'keys': keys}) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 125 | 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] | 126 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 127 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 128 | |
| 129 | def associate_qos(self, qos_id, vol_type_id): |
| 130 | """Associate the specified QoS with specified volume-type.""" |
| 131 | url = "qos-specs/%s/associate" % str(qos_id) |
| 132 | url += "?vol_type_id=%s" % vol_type_id |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 133 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 134 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 135 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 136 | |
Ken'ichi Ohmichi | 35798fb | 2015-04-06 01:22:41 +0000 | [diff] [blame] | 137 | def show_association_qos(self, qos_id): |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 138 | """Get the association of the specified QoS specification.""" |
| 139 | url = "qos-specs/%s/associations" % str(qos_id) |
| 140 | resp, body = self.get(url) |
| 141 | body = json.loads(body) |
| 142 | self.expected_success(200, resp.status) |
John Warren | 3836f3d | 2015-08-13 18:08:52 +0000 | [diff] [blame] | 143 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 144 | |
| 145 | def disassociate_qos(self, qos_id, vol_type_id): |
| 146 | """Disassociate the specified QoS with specified volume-type.""" |
| 147 | url = "qos-specs/%s/disassociate" % str(qos_id) |
| 148 | url += "?vol_type_id=%s" % vol_type_id |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 149 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 150 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 151 | return service_client.ResponseBody(resp, body) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 152 | |
| 153 | def disassociate_all_qos(self, qos_id): |
| 154 | """Disassociate the specified QoS with all associations.""" |
| 155 | url = "qos-specs/%s/disassociate_all" % str(qos_id) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 156 | resp, body = self.get(url) |
Swapnil Kulkarni | 7dba3e6 | 2014-08-14 09:05:07 +0000 | [diff] [blame] | 157 | self.expected_success(202, resp.status) |
Joseph Lanoux | 2cdd550 | 2015-01-16 14:46:51 +0000 | [diff] [blame] | 158 | return service_client.ResponseBody(resp, body) |