jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 1 | # Copyright 2017 FiberHome Telecommunication Technologies CO.,LTD |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
songwenping | 99d6e00 | 2021-01-05 03:07:46 +0000 | [diff] [blame] | 16 | from urllib import parse as urllib |
| 17 | |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 19 | |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 20 | from tempest.lib.api_schema.response.volume import volumes as schema |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
| 23 | from tempest.lib.services.volume import base_client |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 24 | |
| 25 | |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 26 | class VolumesClient(base_client.BaseClient): |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 27 | """Client class to send CRUD Volume V3 API requests""" |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 28 | |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 29 | def _prepare_params(self, params): |
| 30 | """Prepares params for use in get or _ext_get methods. |
| 31 | |
| 32 | If params is a string it will be left as it is, but if it's not it will |
| 33 | be urlencoded. |
| 34 | """ |
songwenping | a6ee2d1 | 2021-02-22 10:24:16 +0800 | [diff] [blame^] | 35 | if isinstance(params, str): |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 36 | return params |
| 37 | return urllib.urlencode(params) |
| 38 | |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 39 | def list_hosts(self): |
| 40 | """Lists all hosts summary info that is not disabled. |
| 41 | |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 42 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#list-all-hosts-for-a-project |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 43 | """ |
| 44 | resp, body = self.get('os-hosts') |
| 45 | body = json.loads(body) |
| 46 | self.expected_success(200, resp.status) |
| 47 | return rest_client.ResponseBody(resp, body) |
| 48 | |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 49 | def list_volumes(self, detail=False, params=None): |
| 50 | """List all the volumes created. |
| 51 | |
| 52 | Params can be a string (must be urlencoded) or a dictionary. |
| 53 | For a full list of available parameters, please refer to the official |
| 54 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 55 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#list-accessible-volumes-with-details |
| 56 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#list-accessible-volumes |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 57 | """ |
| 58 | url = 'volumes' |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 59 | list_schema = schema.list_volumes_no_detail |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 60 | if detail: |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 61 | list_schema = schema.list_volumes_with_detail |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 62 | url += '/detail' |
| 63 | if params: |
| 64 | url += '?%s' % self._prepare_params(params) |
| 65 | |
| 66 | resp, body = self.get(url) |
| 67 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 68 | self.validate_response(list_schema, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 69 | return rest_client.ResponseBody(resp, body) |
| 70 | |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 71 | def migrate_volume(self, volume_id, **kwargs): |
| 72 | """Migrate a volume to a new backend |
| 73 | |
| 74 | For a full list of available parameters please refer to the offical |
| 75 | API reference: |
| 76 | |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 77 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#migrate-a-volume |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 78 | """ |
| 79 | post_body = json.dumps({'os-migrate_volume': kwargs}) |
| 80 | resp, body = self.post('volumes/%s/action' % volume_id, post_body) |
| 81 | self.expected_success(202, resp.status) |
| 82 | return rest_client.ResponseBody(resp, body) |
| 83 | |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 84 | def show_volume(self, volume_id): |
| 85 | """Returns the details of a single volume.""" |
| 86 | url = "volumes/%s" % volume_id |
| 87 | resp, body = self.get(url) |
| 88 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 89 | self.validate_response(schema.show_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 90 | return rest_client.ResponseBody(resp, body) |
| 91 | |
| 92 | def create_volume(self, **kwargs): |
| 93 | """Creates a new Volume. |
| 94 | |
| 95 | For a full list of available parameters, please refer to the official |
| 96 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 97 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#create-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 98 | """ |
| 99 | post_body = json.dumps({'volume': kwargs}) |
| 100 | resp, body = self.post('volumes', post_body) |
| 101 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 102 | self.validate_response(schema.create_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 103 | return rest_client.ResponseBody(resp, body) |
| 104 | |
| 105 | def update_volume(self, volume_id, **kwargs): |
| 106 | """Updates the Specified Volume. |
| 107 | |
| 108 | For a full list of available parameters, please refer to the official |
| 109 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 110 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#update-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 111 | """ |
| 112 | put_body = json.dumps({'volume': kwargs}) |
| 113 | resp, body = self.put('volumes/%s' % volume_id, put_body) |
| 114 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 115 | self.validate_response(schema.update_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 116 | return rest_client.ResponseBody(resp, body) |
| 117 | |
| 118 | def delete_volume(self, volume_id, **params): |
| 119 | """Deletes the Specified Volume. |
| 120 | |
| 121 | For a full list of available parameters, please refer to the official |
| 122 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 123 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#delete-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 124 | """ |
| 125 | url = 'volumes/%s' % volume_id |
| 126 | if params: |
| 127 | url += '?%s' % urllib.urlencode(params) |
| 128 | resp, body = self.delete(url) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 129 | self.validate_response(schema.delete_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 130 | return rest_client.ResponseBody(resp, body) |
| 131 | |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 132 | def show_volume_summary(self, **params): |
| 133 | """Get volumes summary. |
| 134 | |
| 135 | For a full list of available parameters, please refer to the official |
| 136 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 137 | https://docs.openstack.org/api-ref/block-storage/v3/#get-volumes-summary |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 138 | """ |
| 139 | url = 'volumes/summary' |
| 140 | if params: |
| 141 | url += '?%s' % urllib.urlencode(params) |
| 142 | resp, body = self.get(url) |
| 143 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 144 | self.validate_response(schema.show_volume_summary, resp, body) |
jeremy.zhang | 79a1cbf | 2017-05-07 16:09:17 +0800 | [diff] [blame] | 145 | return rest_client.ResponseBody(resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 146 | |
| 147 | def upload_volume(self, volume_id, **kwargs): |
zhufl | aa605d5 | 2019-08-21 15:17:02 +0800 | [diff] [blame] | 148 | """Uploads a volume in Glance. |
| 149 | |
| 150 | For a full list of available parameters, please refer to the official |
| 151 | API reference: |
| 152 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#upload-volume-to-image |
| 153 | """ |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 154 | post_body = json.dumps({'os-volume_upload_image': kwargs}) |
| 155 | url = 'volumes/%s/action' % (volume_id) |
| 156 | resp, body = self.post(url, post_body) |
| 157 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 158 | # TODO(zhufl): This is under discussion, so will be merged |
| 159 | # in a seperate patch. |
| 160 | # https://bugs.launchpad.net/cinder/+bug/1880566 |
| 161 | # self.validate_response(schema.upload_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 162 | self.expected_success(202, resp.status) |
| 163 | return rest_client.ResponseBody(resp, body) |
| 164 | |
| 165 | def attach_volume(self, volume_id, **kwargs): |
| 166 | """Attaches a volume to a given instance on a given mountpoint. |
| 167 | |
| 168 | For a full list of available parameters, please refer to the official |
| 169 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 170 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#attach-volume-to-a-server |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 171 | """ |
| 172 | post_body = json.dumps({'os-attach': kwargs}) |
| 173 | url = 'volumes/%s/action' % (volume_id) |
| 174 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 175 | self.validate_response(schema.attach_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 176 | return rest_client.ResponseBody(resp, body) |
| 177 | |
| 178 | def set_bootable_volume(self, volume_id, **kwargs): |
| 179 | """Set a bootable flag for a volume - true or false. |
| 180 | |
| 181 | For a full list of available parameters, please refer to the official |
| 182 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 183 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#update-a-volume-s-bootable-status |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 184 | """ |
| 185 | post_body = json.dumps({'os-set_bootable': kwargs}) |
| 186 | url = 'volumes/%s/action' % (volume_id) |
| 187 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 188 | self.validate_response(schema.set_bootable_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 189 | return rest_client.ResponseBody(resp, body) |
| 190 | |
| 191 | def detach_volume(self, volume_id): |
| 192 | """Detaches a volume from an instance.""" |
| 193 | post_body = json.dumps({'os-detach': {}}) |
| 194 | url = 'volumes/%s/action' % (volume_id) |
| 195 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 196 | self.validate_response(schema.detach_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 197 | return rest_client.ResponseBody(resp, body) |
| 198 | |
| 199 | def reserve_volume(self, volume_id): |
| 200 | """Reserves a volume.""" |
| 201 | post_body = json.dumps({'os-reserve': {}}) |
| 202 | url = 'volumes/%s/action' % (volume_id) |
| 203 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 204 | self.validate_response(schema.reserve_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 205 | return rest_client.ResponseBody(resp, body) |
| 206 | |
| 207 | def unreserve_volume(self, volume_id): |
| 208 | """Restore a reserved volume .""" |
| 209 | post_body = json.dumps({'os-unreserve': {}}) |
| 210 | url = 'volumes/%s/action' % (volume_id) |
| 211 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 212 | self.validate_response(schema.unreserve_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 213 | return rest_client.ResponseBody(resp, body) |
| 214 | |
| 215 | def is_resource_deleted(self, id): |
| 216 | """Check the specified resource is deleted or not. |
| 217 | |
| 218 | :param id: A checked resource id |
| 219 | :raises lib_exc.DeleteErrorException: If the specified resource is on |
Sergey Vilgelm | eac094a | 2018-11-21 18:27:51 -0600 | [diff] [blame] | 220 | the status the delete was failed. |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 221 | """ |
| 222 | try: |
| 223 | volume = self.show_volume(id) |
| 224 | except lib_exc.NotFound: |
| 225 | return True |
| 226 | if volume["volume"]["status"] == "error_deleting": |
zhufl | 0ded98f | 2019-06-13 11:44:24 +0800 | [diff] [blame] | 227 | raise lib_exc.DeleteErrorException( |
| 228 | "Volume %s failed to delete and is in error_deleting status" % |
Martin Kopec | 14e2a4e | 2020-07-24 09:35:38 +0000 | [diff] [blame] | 229 | volume['volume']['id']) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 230 | return False |
| 231 | |
| 232 | @property |
| 233 | def resource_type(self): |
| 234 | """Returns the primary type of resource this client works with.""" |
| 235 | return 'volume' |
| 236 | |
| 237 | def extend_volume(self, volume_id, **kwargs): |
| 238 | """Extend a volume. |
| 239 | |
| 240 | For a full list of available parameters, please refer to the official |
| 241 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 242 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#extend-a-volume-size |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 243 | """ |
| 244 | post_body = json.dumps({'os-extend': kwargs}) |
| 245 | url = 'volumes/%s/action' % (volume_id) |
| 246 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 247 | self.validate_response(schema.extend_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 248 | return rest_client.ResponseBody(resp, body) |
| 249 | |
| 250 | def reset_volume_status(self, volume_id, **kwargs): |
| 251 | """Reset the Specified Volume's Status. |
| 252 | |
| 253 | For a full list of available parameters, please refer to the official |
| 254 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 255 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#reset-a-volume-s-statuses |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 256 | """ |
| 257 | post_body = json.dumps({'os-reset_status': kwargs}) |
| 258 | resp, body = self.post('volumes/%s/action' % volume_id, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 259 | self.validate_response(schema.reset_volume_status, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 260 | return rest_client.ResponseBody(resp, body) |
| 261 | |
| 262 | def update_volume_readonly(self, volume_id, **kwargs): |
zhufl | aa605d5 | 2019-08-21 15:17:02 +0800 | [diff] [blame] | 263 | """Update the Specified Volume readonly. |
| 264 | |
| 265 | For a full list of available parameters, please refer to the official |
| 266 | API reference: |
| 267 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#updates-volume-read-only-access-mode-flag |
| 268 | """ |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 269 | post_body = json.dumps({'os-update_readonly_flag': kwargs}) |
| 270 | url = 'volumes/%s/action' % (volume_id) |
| 271 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 272 | self.validate_response(schema.update_volume_readonly, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 273 | return rest_client.ResponseBody(resp, body) |
| 274 | |
| 275 | def force_delete_volume(self, volume_id): |
| 276 | """Force Delete Volume.""" |
| 277 | post_body = json.dumps({'os-force_delete': {}}) |
| 278 | resp, body = self.post('volumes/%s/action' % volume_id, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 279 | self.validate_response(schema.force_delete_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 280 | return rest_client.ResponseBody(resp, body) |
| 281 | |
| 282 | def create_volume_metadata(self, volume_id, metadata): |
| 283 | """Create metadata for the volume. |
| 284 | |
| 285 | For a full list of available parameters, please refer to the official |
| 286 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 287 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#create-metadata-for-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 288 | """ |
| 289 | put_body = json.dumps({'metadata': metadata}) |
| 290 | url = "volumes/%s/metadata" % volume_id |
| 291 | resp, body = self.post(url, put_body) |
| 292 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 293 | self.validate_response(schema.create_volume_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 294 | return rest_client.ResponseBody(resp, body) |
| 295 | |
| 296 | def show_volume_metadata(self, volume_id): |
| 297 | """Get metadata of the volume.""" |
| 298 | url = "volumes/%s/metadata" % volume_id |
| 299 | resp, body = self.get(url) |
| 300 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 301 | self.validate_response(schema.show_volume_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 302 | return rest_client.ResponseBody(resp, body) |
| 303 | |
| 304 | def update_volume_metadata(self, volume_id, metadata): |
| 305 | """Update metadata for the volume. |
| 306 | |
| 307 | For a full list of available parameters, please refer to the official |
| 308 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 309 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#update-a-volume-s-metadata |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 310 | """ |
| 311 | put_body = json.dumps({'metadata': metadata}) |
| 312 | url = "volumes/%s/metadata" % volume_id |
| 313 | resp, body = self.put(url, put_body) |
| 314 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 315 | self.validate_response(schema.update_volume_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 316 | return rest_client.ResponseBody(resp, body) |
| 317 | |
| 318 | def show_volume_metadata_item(self, volume_id, id): |
| 319 | """Show metadata item for the volume.""" |
| 320 | url = "volumes/%s/metadata/%s" % (volume_id, id) |
| 321 | resp, body = self.get(url) |
| 322 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 323 | self.validate_response(schema.show_volume_metadata_item, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 324 | return rest_client.ResponseBody(resp, body) |
| 325 | |
| 326 | def update_volume_metadata_item(self, volume_id, id, meta_item): |
| 327 | """Update metadata item for the volume.""" |
| 328 | put_body = json.dumps({'meta': meta_item}) |
| 329 | url = "volumes/%s/metadata/%s" % (volume_id, id) |
| 330 | resp, body = self.put(url, put_body) |
| 331 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 332 | self.validate_response(schema.update_volume_metadata_item, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 333 | return rest_client.ResponseBody(resp, body) |
| 334 | |
| 335 | def delete_volume_metadata_item(self, volume_id, id): |
| 336 | """Delete metadata item for the volume.""" |
| 337 | url = "volumes/%s/metadata/%s" % (volume_id, id) |
| 338 | resp, body = self.delete(url) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 339 | self.validate_response(schema.delete_volume_metadata_item, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 340 | return rest_client.ResponseBody(resp, body) |
| 341 | |
| 342 | def retype_volume(self, volume_id, **kwargs): |
| 343 | """Updates volume with new volume type. |
| 344 | |
| 345 | For a full list of available parameters, please refer to the official |
| 346 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 347 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#retype-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 348 | """ |
| 349 | post_body = json.dumps({'os-retype': kwargs}) |
| 350 | resp, body = self.post('volumes/%s/action' % volume_id, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 351 | self.validate_response(schema.retype_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 352 | return rest_client.ResponseBody(resp, body) |
| 353 | |
| 354 | def force_detach_volume(self, volume_id, **kwargs): |
| 355 | """Force detach a volume. |
| 356 | |
| 357 | For a full list of available parameters, please refer to the official |
| 358 | API reference: |
zhufl | df95930 | 2019-11-19 10:25:13 +0800 | [diff] [blame] | 359 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#force-detach-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 360 | """ |
| 361 | post_body = json.dumps({'os-force_detach': kwargs}) |
| 362 | url = 'volumes/%s/action' % volume_id |
| 363 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 364 | self.validate_response(schema.force_detach_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 365 | return rest_client.ResponseBody(resp, body) |
| 366 | |
| 367 | def update_volume_image_metadata(self, volume_id, **kwargs): |
| 368 | """Update image metadata for the volume. |
| 369 | |
| 370 | For a full list of available parameters, please refer to the official |
| 371 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 372 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#set-image-metadata-for-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 373 | """ |
| 374 | post_body = json.dumps({'os-set_image_metadata': {'metadata': kwargs}}) |
| 375 | url = "volumes/%s/action" % (volume_id) |
| 376 | resp, body = self.post(url, post_body) |
| 377 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 378 | self.validate_response(schema.update_volume_image_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 379 | return rest_client.ResponseBody(resp, body) |
| 380 | |
| 381 | def delete_volume_image_metadata(self, volume_id, key_name): |
| 382 | """Delete image metadata item for the volume.""" |
| 383 | post_body = json.dumps({'os-unset_image_metadata': {'key': key_name}}) |
| 384 | url = "volumes/%s/action" % (volume_id) |
| 385 | resp, body = self.post(url, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 386 | self.validate_response(schema.delete_volume_image_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 387 | return rest_client.ResponseBody(resp, body) |
| 388 | |
| 389 | def show_volume_image_metadata(self, volume_id): |
| 390 | """Show image metadata for the volume.""" |
| 391 | post_body = json.dumps({'os-show_image_metadata': {}}) |
| 392 | url = "volumes/%s/action" % volume_id |
| 393 | resp, body = self.post(url, post_body) |
| 394 | body = json.loads(body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 395 | self.validate_response(schema.show_volume_image_metadata, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 396 | return rest_client.ResponseBody(resp, body) |
| 397 | |
| 398 | def unmanage_volume(self, volume_id): |
| 399 | """Unmanage volume. |
| 400 | |
| 401 | For a full list of available parameters, please refer to the official |
| 402 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 403 | https://docs.openstack.org/api-ref/block-storage/v3/index.html#unmanage-a-volume |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 404 | """ |
| 405 | post_body = json.dumps({'os-unmanage': {}}) |
| 406 | resp, body = self.post('volumes/%s/action' % volume_id, post_body) |
zhufl | 3cb4772 | 2018-11-09 14:31:49 +0800 | [diff] [blame] | 407 | self.validate_response(schema.unmanage_volume, resp, body) |
ghanshyam | de676ba | 2018-02-19 06:20:00 +0000 | [diff] [blame] | 408 | return rest_client.ResponseBody(resp, body) |