1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
|
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings
from openstack.cloud import _utils
from openstack.cloud import openstackcloud
from openstack import exceptions
from openstack import resource
from openstack import warnings as os_warnings
class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin):
def list_volumes(self, cache=None):
"""List all available volumes.
:param cache: **DEPRECATED** This parameter no longer does anything.
:returns: A list of volume ``Volume`` objects.
"""
if cache is not None:
warnings.warn(
"the 'cache' argument is deprecated and no longer does "
"anything; consider removing it from calls",
os_warnings.RemovedInSDK50Warning,
)
return list(self.block_storage.volumes())
def list_volume_types(self, get_extra=None):
"""List all available volume types.
:param get_extra: **DEPRECATED** This parameter no longer does
anything.
:returns: A list of volume ``Type`` objects.
"""
if get_extra is not None:
warnings.warn(
"the 'get_extra' argument is deprecated and no longer does "
"anything; consider removing it from calls",
os_warnings.RemovedInSDK50Warning,
)
return list(self.block_storage.types())
# TODO(stephenfin): Remove 'filters' in a future major version
def get_volume(self, name_or_id, filters=None):
"""Get a volume by name or ID.
:param name_or_id: Name or unique ID of the volume.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A volume ``Volume`` object if found, else None.
"""
if filters is not None:
warnings.warn(
"The 'filters' argument is deprecated; use "
"'search_volumes' instead",
os_warnings.RemovedInSDK60Warning,
)
entities = self.search_volumes(name_or_id, filters)
if not entities:
return None
if len(entities) > 1:
raise exceptions.SDKException(
f"Multiple matches found for {name_or_id}",
)
return entities[0]
return self.block_storage.find_volume(name_or_id)
def get_volume_by_id(self, id):
"""Get a volume by ID
:param id: ID of the volume.
:returns: A volume ``Volume`` object if found, else None.
"""
return self.block_storage.get_volume(id)
# TODO(stephenfin): Remove 'filters' in a future major version
def get_volume_type(self, name_or_id, filters=None):
"""Get a volume type by name or ID.
:param name_or_id: Name or unique ID of the volume type.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A volume ``Type`` object if found, else None.
"""
if filters is not None:
warnings.warn(
"The 'filters' argument is deprecated; use "
"'search_volume_types' instead",
os_warnings.RemovedInSDK60Warning,
)
entities = self.search_volume_types(name_or_id, filters)
if not entities:
return None
if len(entities) > 1:
raise exceptions.SDKException(
f"Multiple matches found for {name_or_id}",
)
return entities[0]
return self.block_storage.find_type(name_or_id)
def create_volume(
self,
size,
wait=True,
timeout=None,
image=None,
bootable=None,
**kwargs,
):
"""Create a volume.
:param size: Size, in GB of the volume to create.
:param wait: If true, waits for volume to be created.
:param timeout: Seconds to wait for volume creation. None is forever.
:param image: (optional) Image name, ID or object from which to create
the volume
:param bootable: (optional) Make this volume bootable. If set, wait
will also be set to true.
:param kwargs: Keyword arguments as expected for cinder client.
:returns: The created volume ``Volume`` object.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
if bootable is not None:
wait = True
if image:
if isinstance(image, dict):
if not isinstance(image, resource.Resource):
warnings.warn(
"Support for passing image as a raw dict has "
"been deprecated for removal. Consider passing a "
"string name or ID or an Image object instead.",
os_warnings.RemovedInSDK60Warning,
)
kwargs['imageRef'] = image['id']
else: # object
image_obj = self.image.find_image(image, ignore_missing=False)
kwargs['imageRef'] = image_obj['id']
kwargs = self._get_volume_kwargs(kwargs)
kwargs['size'] = size
volume = self.block_storage.create_volume(**kwargs)
if volume['status'] == 'error':
raise exceptions.SDKException("Error in creating volume")
if wait:
self.block_storage.wait_for_status(volume, wait=timeout)
if bootable:
self.block_storage.set_volume_bootable_status(volume, True)
return volume
def update_volume(self, name_or_id, **kwargs):
"""Update a volume.
:param name_or_id: Name or unique ID of the volume.
:param kwargs: Volume attributes to be updated.
:returns: The updated volume ``Volume`` object.
"""
kwargs = self._get_volume_kwargs(kwargs)
volume = self.get_volume(name_or_id)
if not volume:
raise exceptions.SDKException(f"Volume {name_or_id} not found.")
volume = self.block_storage.update_volume(volume, **kwargs)
return volume
def set_volume_bootable(self, name_or_id, bootable=True):
"""Set a volume's bootable flag.
:param name_or_id: Name or unique ID of the volume.
:param bool bootable: Whether the volume should be bootable.
(Defaults to True)
:returns: None
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume = self.get_volume(name_or_id)
if not volume:
raise exceptions.SDKException(
f"Volume {name_or_id} does not exist"
)
self.block_storage.set_volume_bootable_status(volume, bootable)
def delete_volume(
self,
name_or_id=None,
wait=True,
timeout=None,
force=False,
):
"""Delete a volume.
:param name_or_id: Name or unique ID of the volume.
:param wait: If true, waits for volume to be deleted.
:param timeout: Seconds to wait for volume deletion. None is forever.
:param force: Force delete volume even if the volume is in deleting
or error_deleting state.
:returns: True if deletion was successful, else False.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume = self.block_storage.find_volume(
name_or_id, ignore_missing=True
)
if not volume:
self.log.debug(
"Volume %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True,
)
return False
try:
self.block_storage.delete_volume(volume, force=force)
except exceptions.SDKException:
self.log.exception("error in deleting volume")
raise
if wait:
self.block_storage.wait_for_delete(volume, wait=timeout)
return True
def get_volumes(self, server, cache=None):
"""Get volumes for a server.
:param server: The server to fetch volumes for.
:param cache: **DEPRECATED** This parameter no longer does anything.
:returns: A list of volume ``Volume`` objects.
"""
if cache is not None:
warnings.warn(
"the 'cache' argument is deprecated and no longer does "
"anything; consider removing it from calls",
os_warnings.RemovedInSDK50Warning,
)
# avoid spamming warnings
cache = None
volumes = []
for volume in self.list_volumes(cache=cache):
for attach in volume['attachments']:
if attach['server_id'] == server['id']:
volumes.append(volume)
return volumes
def get_volume_limits(self, name_or_id=None):
"""Get volume limits for the current project
:param name_or_id: (optional) Project name or ID to get limits for
if different from the current project
:returns: The volume ``Limits`` object if found, else None.
"""
params = {}
if name_or_id:
project = self.identity.find_project(
name_or_id, ignore_missing=False
)
params['project'] = project
return self.block_storage.get_limits(**params)
def get_volume_id(self, name_or_id):
"""Get ID of a volume.
:param name_or_id: Name or unique ID of the volume.
:returns: The ID of the volume if found, else None.
"""
volume = self.get_volume(name_or_id)
if volume:
return volume['id']
return None
def volume_exists(self, name_or_id):
"""Check if a volume exists.
:param name_or_id: Name or unique ID of the volume.
:returns: True if the volume exists, else False.
"""
return self.get_volume(name_or_id) is not None
def get_volume_attach_device(self, volume, server_id):
"""Return the device name a volume is attached to for a server.
This can also be used to verify if a volume is attached to
a particular server.
:param volume: The volume to fetch the device name from.
:param server_id: ID of server to check.
:returns: Device name if attached, None if volume is not attached.
"""
for attach in volume['attachments']:
if server_id == attach['server_id']:
return attach['device']
return None
def detach_volume(self, server, volume, wait=True, timeout=None):
"""Detach a volume from a server.
:param server: The server dict to detach from.
:param volume: The volume dict to detach.
:param wait: If true, waits for volume to be detached.
:param timeout: Seconds to wait for volume detachment. None is forever.
:returns: None
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
self.compute.delete_volume_attachment(
server=server['id'],
volume=volume['id'],
ignore_missing=False,
)
if wait:
vol = self.get_volume(volume['id'])
self.block_storage.wait_for_status(vol)
def attach_volume(
self,
server,
volume,
device=None,
wait=True,
timeout=None,
):
"""Attach a volume to a server.
This will attach a volume, described by the passed in volume
dict (as returned by get_volume()), to the server described by
the passed in server dict (as returned by get_server()) on the
named device on the server.
If the volume is already attached to the server, or generally not
available, then an exception is raised. To re-attach to a server,
but under a different device, the user must detach it first.
:param server: The server dict to attach to.
:param volume: The volume dict to attach.
:param device: The device name where the volume will attach.
:param wait: If true, waits for volume to be attached.
:param timeout: Seconds to wait for volume attachment. None is forever.
:returns: a volume attachment object.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
dev = self.get_volume_attach_device(volume, server['id'])
if dev:
raise exceptions.SDKException(
"Volume {} already attached to server {} on device {}".format(
volume['id'], server['id'], dev
)
)
if volume['status'] != 'available':
raise exceptions.SDKException(
"Volume {} is not available. Status is '{}'".format(
volume['id'], volume['status']
)
)
payload = {}
if device:
payload['device'] = device
attachment = self.compute.create_volume_attachment(
server=server['id'],
volume=volume['id'],
**payload,
)
if wait:
if not hasattr(volume, 'fetch'):
# If we got volume as dict we need to re-fetch it to be able to
# use wait_for_status.
volume = self.block_storage.get_volume(volume['id'])
self.block_storage.wait_for_status(volume, 'in-use', wait=timeout)
return attachment
def _get_volume_kwargs(self, kwargs):
name = kwargs.pop('name', kwargs.pop('display_name', None))
description = kwargs.pop(
'description', kwargs.pop('display_description', None)
)
if name:
kwargs['name'] = name
if description:
kwargs['description'] = description
return kwargs
@_utils.valid_kwargs(
'name', 'display_name', 'description', 'display_description'
)
def create_volume_snapshot(
self,
volume_id,
force=False,
wait=True,
timeout=None,
**kwargs,
):
"""Create a volume.
:param volume_id: the ID of the volume to snapshot.
:param force: If set to True the snapshot will be created even if the
volume is attached to an instance, if False it will not
:param name: name of the snapshot, one will be generated if one is
not provided
:param description: description of the snapshot, one will be generated
if one is not provided
:param wait: If true, waits for volume snapshot to be created.
:param timeout: Seconds to wait for volume snapshot creation. None is
forever.
:returns: The created volume ``Snapshot`` object.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
kwargs = self._get_volume_kwargs(kwargs)
payload = {'volume_id': volume_id, 'force': force}
payload.update(kwargs)
snapshot = self.block_storage.create_snapshot(**payload)
if wait:
snapshot = self.block_storage.wait_for_status(
snapshot, wait=timeout
)
return snapshot
def get_volume_snapshot_by_id(self, snapshot_id):
"""Takes a snapshot_id and gets a dict of the snapshot
that maches that ID.
Note: This is more efficient than get_volume_snapshot.
param: snapshot_id: ID of the volume snapshot.
:returns: A volume ``Snapshot`` object if found, else None.
"""
return self.block_storage.get_snapshot(snapshot_id)
# TODO(stephenfin): Remove 'filters' in a future major version
def get_volume_snapshot(self, name_or_id, filters=None):
"""Get a volume by name or ID.
:param name_or_id: Name or unique ID of the volume snapshot.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A volume ``Snapshot`` object if found, else None.
"""
if filters is not None:
warnings.warn(
"The 'filters' argument is deprecated; use "
"'search_volume_snapshots' instead",
os_warnings.RemovedInSDK60Warning,
)
entities = self.search_volume_snapshots(name_or_id, filters)
if not entities:
return None
if len(entities) > 1:
raise exceptions.SDKException(
f"Multiple matches found for {name_or_id}",
)
return entities[0]
return self.block_storage.find_snapshot(name_or_id)
def create_volume_backup(
self,
volume_id,
name=None,
description=None,
force=False,
wait=True,
timeout=None,
incremental=False,
snapshot_id=None,
):
"""Create a volume backup.
:param volume_id: the ID of the volume to backup.
:param name: name of the backup, one will be generated if one is
not provided
:param description: description of the backup, one will be generated
if one is not provided
:param force: If set to True the backup will be created even if the
volume is attached to an instance, if False it will not
:param wait: If true, waits for volume backup to be created.
:param timeout: Seconds to wait for volume backup creation. None is
forever.
:param incremental: If set to true, the backup will be incremental.
:param snapshot_id: The UUID of the source snapshot to back up.
:returns: The created volume ``Backup`` object.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
payload = {
'name': name,
'volume_id': volume_id,
'description': description,
'force': force,
'is_incremental': incremental,
'snapshot_id': snapshot_id,
}
backup = self.block_storage.create_backup(**payload)
if wait:
backup = self.block_storage.wait_for_status(backup, wait=timeout)
return backup
# TODO(stephenfin): Remove 'filters' in a future major version
def get_volume_backup(self, name_or_id, filters=None):
"""Get a volume backup by name or ID.
:param name_or_id: Name or unique ID of the volume backup.
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A volume ``Backup`` object if found, else None.
"""
if filters is not None:
warnings.warn(
"The 'filters' argument is deprecated; use "
"'search_volume_backups' instead",
os_warnings.RemovedInSDK60Warning,
)
entities = self.search_volume_backups(name_or_id, filters)
if not entities:
return None
if len(entities) > 1:
raise exceptions.SDKException(
f"Multiple matches found for {name_or_id}",
)
return entities[0]
return self.block_storage.find_backup(name_or_id)
def list_volume_snapshots(self, detailed=True, filters=None):
"""List all volume snapshots.
:param detailed: Whether or not to add detailed additional information.
:param filters: A dictionary of meta data to use for further filtering.
Example::
{
'name': 'my-volume-snapshot',
'volume_id': 'e126044c-7b4c-43be-a32a-c9cbbc9ddb56',
'all_tenants': 1,
}
:returns: A list of volume ``Snapshot`` objects.
"""
if not filters:
filters = {}
return list(self.block_storage.snapshots(details=detailed, **filters))
def list_volume_backups(self, detailed=True, filters=None):
"""List all volume backups.
:param detailed: Whether or not to add detailed additional information.
:param filters: A dictionary of meta data to use for further filtering.
Example::
{
'name': 'my-volume-backup',
'status': 'available',
'volume_id': 'e126044c-7b4c-43be-a32a-c9cbbc9ddb56',
'all_tenants': 1,
}
:returns: A list of volume ``Backup`` objects.
"""
if not filters:
filters = {}
return list(self.block_storage.backups(details=detailed, **filters))
def delete_volume_backup(
self, name_or_id=None, force=False, wait=False, timeout=None
):
"""Delete a volume backup.
:param name_or_id: Name or unique ID of the volume backup.
:param force: Allow delete in state other than error or available.
:param wait: If true, waits for volume backup to be deleted.
:param timeout: Seconds to wait for volume backup deletion. None is
forever.
:returns: True if deletion was successful, else False.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume_backup = self.get_volume_backup(name_or_id)
if not volume_backup:
return False
self.block_storage.delete_backup(
volume_backup, ignore_missing=False, force=force
)
if wait:
self.block_storage.wait_for_delete(volume_backup, wait=timeout)
return True
def delete_volume_snapshot(
self,
name_or_id=None,
wait=False,
timeout=None,
):
"""Delete a volume snapshot.
:param name_or_id: Name or unique ID of the volume snapshot.
:param wait: If true, waits for volume snapshot to be deleted.
:param timeout: Seconds to wait for volume snapshot deletion. None is
forever.
:returns: True if deletion was successful, else False.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if wait time
exceeded.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volumesnapshot = self.get_volume_snapshot(name_or_id)
if not volumesnapshot:
return False
self.block_storage.delete_snapshot(
volumesnapshot, ignore_missing=False
)
if wait:
self.block_storage.wait_for_delete(volumesnapshot, wait=timeout)
return True
def search_volumes(self, name_or_id=None, filters=None):
"""Search for one or more volumes.
:param name_or_id: Name or unique ID of volume(s).
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of volume ``Volume`` objects, if any are found.
"""
volumes = self.list_volumes()
return _utils._filter_list(volumes, name_or_id, filters)
def search_volume_snapshots(self, name_or_id=None, filters=None):
"""Search for one or more volume snapshots.
:param name_or_id: Name or unique ID of volume snapshot(s).
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of volume ``Snapshot`` objects, if any are found.
"""
volumesnapshots = self.list_volume_snapshots()
return _utils._filter_list(volumesnapshots, name_or_id, filters)
def search_volume_backups(self, name_or_id=None, filters=None):
"""Search for one or more volume backups.
:param name_or_id: Name or unique ID of volume backup(s).
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of volume ``Backup`` objects, if any are found.
"""
volume_backups = self.list_volume_backups()
return _utils._filter_list(volume_backups, name_or_id, filters)
def search_volume_types(
self,
name_or_id=None,
filters=None,
get_extra=None,
):
"""Search for one or more volume types.
:param name_or_id: Name or unique ID of volume type(s).
:param filters: **DEPRECATED** A dictionary of meta data to use for
further filtering. Elements of this dictionary may, themselves, be
dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example::
"[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A list of volume ``Type`` objects, if any are found.
"""
if get_extra is not None:
warnings.warn(
"the 'get_extra' argument is deprecated and no longer does "
"anything; consider removing it from calls",
os_warnings.RemovedInSDK50Warning,
)
volume_types = self.list_volume_types()
return _utils._filter_list(volume_types, name_or_id, filters)
def get_volume_type_access(self, name_or_id):
"""Return a list of volume_type_access.
:param name_or_id: Name or unique ID of the volume type.
:returns: A volume ``Type`` object if found, else None.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exceptions.SDKException(
f"VolumeType not found: {name_or_id}"
)
return self.block_storage.get_type_access(volume_type)
def add_volume_type_access(self, name_or_id, project_id):
"""Grant access on a volume_type to a project.
NOTE: the call works even if the project does not exist.
:param name_or_id: ID or name of a volume_type
:param project_id: A project id
:returns: None
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exceptions.SDKException(
f"VolumeType not found: {name_or_id}"
)
self.block_storage.add_type_access(volume_type, project_id)
def remove_volume_type_access(self, name_or_id, project_id):
"""Revoke access on a volume_type to a project.
:param name_or_id: ID or name of a volume_type
:param project_id: A project id
:returns: None
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exceptions.SDKException(
f"VolumeType not found: {name_or_id}"
)
self.block_storage.remove_type_access(volume_type, project_id)
def set_volume_quotas(self, name_or_id, **kwargs):
"""Set a volume quota in a project
:param name_or_id: project name or id
:param kwargs: key/value pairs of quota name and quota value
:returns: None
:raises: :class:`~openstack.exceptions.SDKException` if the resource to
set the quota does not exist.
"""
project = self.identity.find_project(name_or_id, ignore_missing=False)
self.block_storage.update_quota_set(project=project, **kwargs)
def get_volume_quotas(self, name_or_id):
"""Get volume quotas for a project
:param name_or_id: project name or id
:returns: A volume ``QuotaSet`` object with the quotas
:raises: :class:`~openstack.exceptions.SDKException` if it's not a
valid project
"""
proj = self.identity.find_project(name_or_id, ignore_missing=False)
return self.block_storage.get_quota_set(proj)
def delete_volume_quotas(self, name_or_id):
"""Delete volume quotas for a project
:param name_or_id: project name or id
:returns: The deleted volume ``QuotaSet`` object.
:raises: :class:`~openstack.exceptions.SDKException` if it's not a
valid project or the call failed
"""
proj = self.identity.find_project(name_or_id, ignore_missing=False)
return self.block_storage.revert_quota_set(proj)
|