Use dict.get() when accessing capabilities dict

Many compute drivers override the driver capabilities dict [1]. The
code should be using dict.get() to avoid throwing KeyErrors if the
overriding dictionaries do not have the corresponding keys. If the
key is not found it will be assumed the the capability is not supported
and default to false. This ensures that no drivers are broken because
they are missing a capability.

[1] https://github.com/openstack/nova/blob/5251f18d87/nova/virt/driver.py#L124-L134

Change-Id: I1fa04fa110f2c65e10c065c61f2f0f58c1fad646
Closes-Bug: #1746608
This commit is contained in:
esberglu 2018-01-31 16:29:57 -06:00
parent c966524672
commit 2dd322a642
2 changed files with 12 additions and 7 deletions

View File

@ -1794,7 +1794,8 @@ class ComputeManager(manager.Manager):
tagging_requested = True
break
if (tagging_requested and
not self.driver.capabilities.get('supports_device_tagging')):
not self.driver.capabilities.get('supports_device_tagging',
False)):
raise exception.BuildAbortException('Attempt to boot guest with '
'tagged devices on host that '
'does not support tagging.')
@ -2964,7 +2965,7 @@ class ComputeManager(manager.Manager):
hints = self._get_scheduler_hints({}, request_spec)
self._validate_instance_group_policy(context, instance, hints)
if not self.driver.capabilities["supports_recreate"]:
if not self.driver.capabilities.get("supports_recreate", False):
raise exception.InstanceRecreateNotSupported
self._check_instance_exists(context, instance)
@ -4061,7 +4062,8 @@ class ComputeManager(manager.Manager):
# if the flavor IDs match, it's migrate; otherwise resize
if same_host and instance_type.id == instance['instance_type_id']:
# check driver whether support migrate to same host
if not self.driver.capabilities['supports_migrate_to_same_host']:
if not self.driver.capabilities.get(
'supports_migrate_to_same_host', False):
raise exception.UnableToMigrateToSelf(
instance_id=instance.uuid, host=self.host)
@ -5406,7 +5408,8 @@ class ComputeManager(manager.Manager):
# TODO(mriedem): This is copied from DriverVolumeBlockDevice
# and should be consolidated into some common code at some point.
vol_multiattach = new_volume.get('multiattach', False)
virt_multiattach = self.driver.capabilities['supports_multiattach']
virt_multiattach = self.driver.capabilities.get(
'supports_multiattach', False)
if vol_multiattach and not virt_multiattach:
raise exception.MultiattachNotSupportedByVirtDriver(
volume_id=new_volume_id)
@ -5660,7 +5663,8 @@ class ComputeManager(manager.Manager):
def attach_interface(self, context, instance, network_id, port_id,
requested_ip, tag):
"""Use hotplug to add an network adapter to an instance."""
if not self.driver.capabilities['supports_attach_interface']:
if not self.driver.capabilities.get('supports_attach_interface',
False):
raise exception.AttachInterfaceNotSupported(
instance_uuid=instance.uuid)
if (tag and not
@ -7593,7 +7597,7 @@ class ComputeManager(manager.Manager):
def _run_image_cache_manager_pass(self, context):
"""Run a single pass of the image cache manager."""
if not self.driver.capabilities["has_imagecache"]:
if not self.driver.capabilities.get("has_imagecache", False):
return
# Determine what other nodes use this storage

View File

@ -496,7 +496,8 @@ class DriverVolumeBlockDevice(DriverBlockDevice):
self.volume_size = volume.get('size')
vol_multiattach = volume.get('multiattach', False)
virt_multiattach = virt_driver.capabilities['supports_multiattach']
virt_multiattach = virt_driver.capabilities.get(
'supports_multiattach', False)
if vol_multiattach and not virt_multiattach:
raise exception.MultiattachNotSupportedByVirtDriver(