From bd6ab754e6e6dc2fa57a830e1d0953716e0115c6 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 16 Sep 2024 16:11:31 +0100 Subject: [PATCH] cloud: Consistently set 'ignore_missing' arguments Change-Id: I3f543b82ec6b663e6a0dbabba323963c5b24e378 Signed-off-by: Stephen Finucane --- openstack/cloud/_baremetal.py | 14 +++---- openstack/cloud/_block_storage.py | 11 +++-- openstack/cloud/_compute.py | 33 ++++++++++----- openstack/cloud/_dns.py | 16 +++---- openstack/cloud/_identity.py | 15 ++++--- openstack/cloud/_image.py | 2 +- openstack/cloud/_network.py | 67 +++++++++++++++++++++--------- openstack/cloud/_network_common.py | 4 +- 8 files changed, 102 insertions(+), 60 deletions(-) diff --git a/openstack/cloud/_baremetal.py b/openstack/cloud/_baremetal.py index e9eee152b..9d1841a31 100644 --- a/openstack/cloud/_baremetal.py +++ b/openstack/cloud/_baremetal.py @@ -82,10 +82,7 @@ class BaremetalCloudMixin(openstackcloud._OpenStackCloudMixin): :rtype: :class:`~openstack.baremetal.v1.node.Node`. :returns: The node found or None if no nodes are found. """ - try: - return self.baremetal.find_node(name_or_id, ignore_missing=False) - except exceptions.NotFoundException: - return None + return self.baremetal.find_node(name_or_id, ignore_missing=True) def get_machine_by_mac(self, mac): """Get machine by port MAC address @@ -417,7 +414,7 @@ class BaremetalCloudMixin(openstackcloud._OpenStackCloudMixin): :return: Nothing. """ machine = self.get_machine(name_or_id) - port = self.network.find_port(port_name_or_id) + port = self.network.find_port(port_name_or_id, ignore_missing=False) self.baremetal.attach_vif_to_node(machine, port['id']) def detach_port_from_machine(self, name_or_id, port_name_or_id): @@ -429,7 +426,7 @@ class BaremetalCloudMixin(openstackcloud._OpenStackCloudMixin): :return: Nothing. """ machine = self.get_machine(name_or_id) - port = self.network.find_port(port_name_or_id) + port = self.network.find_port(port_name_or_id, ignore_missing=False) self.baremetal.detach_vif_from_node(machine, port['id']) def list_ports_attached_to_machine(self, name_or_id): @@ -441,7 +438,10 @@ class BaremetalCloudMixin(openstackcloud._OpenStackCloudMixin): """ machine = self.get_machine(name_or_id) vif_ids = self.baremetal.list_node_vifs(machine) - return [self.network.find_port(vif) for vif in vif_ids] + return [ + self.network.find_port(vif, ignore_missing=False) + for vif in vif_ids + ] def validate_machine(self, name_or_id, for_deploy=True): """Validate parameters of the machine. diff --git a/openstack/cloud/_block_storage.py b/openstack/cloud/_block_storage.py index 71e0b1b83..56e650313 100644 --- a/openstack/cloud/_block_storage.py +++ b/openstack/cloud/_block_storage.py @@ -136,7 +136,7 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin): if isinstance(image, dict): kwargs['imageRef'] = image['id'] else: # object - image_obj = self.image.find_image(image) + image_obj = self.image.find_image(image, ignore_missing=True) if not image_obj: raise exceptions.SDKException( f"Image {image} was requested as the basis for a new " @@ -221,8 +221,9 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` on operation error. """ - volume = self.block_storage.find_volume(name_or_id) - + 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", @@ -265,7 +266,9 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin): """ params = {} if name_or_id: - project = self.identity.find_project(name_or_id) + project = self.identity.find_project( + name_or_id, ignore_missing=True + ) if not project: raise exceptions.SDKException("project does not exist") params['project'] = project diff --git a/openstack/cloud/_compute.py b/openstack/cloud/_compute.py index e0a54db40..28641ffa2 100644 --- a/openstack/cloud/_compute.py +++ b/openstack/cloud/_compute.py @@ -380,7 +380,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): """ params = {} if name_or_id: - project = self.identity.find_project(name_or_id) + project = self.identity.find_project( + name_or_id, ignore_missing=True + ) if not project: raise exceptions.SDKException( f"Project {name_or_id} was requested but was not found " @@ -832,7 +834,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): # accept strings or Image objects group_id = group['id'] else: # object - group_obj = self.compute.find_server_group(group) + group_obj = self.compute.find_server_group( + group, ignore_missing=True + ) if not group_obj: raise exceptions.SDKException( "Server Group {group} was requested but was not found" @@ -866,7 +870,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): # accept strings or Network objects network_id = net['id'] else: - network_obj = self.network.find_network(net) + network_obj = self.network.find_network( + net, ignore_missing=True + ) if not network_obj: raise exceptions.SDKException( 'Network {network} is not a valid network in' @@ -895,7 +901,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): nic.pop('net-name', None) elif 'net-name' in nic: net_name = nic.pop('net-name') - nic_net = self.network.find_network(net_name) + nic_net = self.network.find_network( + net_name, ignore_missing=True + ) if not nic_net: raise exceptions.SDKException( "Requested network {net} could not be found.".format( @@ -940,7 +948,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): if isinstance(image, dict): kwargs['imageRef'] = image['id'] else: - image_obj = self.image.find_image(image) + image_obj = self.image.find_image(image, ignore_missing=True) if not image_obj: raise exc.OpenStackCloudException( f"Image {image} was requested but was not found " @@ -1046,7 +1054,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): if isinstance(boot_volume, dict): volume_id = boot_volume['id'] else: - volume = self.block_storage.find_volume(boot_volume) + volume = self.block_storage.find_volume( + boot_volume, ignore_missing=True + ) if not volume: raise exceptions.SDKException( f"Volume {volume} was requested but was not found " @@ -1068,7 +1078,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): if isinstance(image, dict): image_obj = image else: - image_obj = self.image.find_image(image) + image_obj = self.image.find_image(image, ignore_missing=True) if not image_obj: raise exceptions.SDKException( f"Image {image} was requested but was not found " @@ -1104,7 +1114,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): if isinstance(volume, dict): volume_id = volume['id'] else: - volume_obj = self.block_storage.find_volume(volume) + volume_obj = self.block_storage.find_volume( + volume, ignore_missing=True + ) if not volume_obj: raise exceptions.SDKException( f"Volume {volume} was requested but was not found " @@ -1433,7 +1445,6 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): error. """ server = self.compute.find_server(name_or_id, ignore_missing=False) - server = self.compute.update_server(server, **kwargs) return self._expand_server(server, bare=bare, detailed=detailed) @@ -1531,7 +1542,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): error. """ try: - flavor = self.compute.find_flavor(name_or_id) + flavor = self.compute.find_flavor(name_or_id, ignore_missing=True) if not flavor: self.log.debug("Flavor %s not found for deleting", name_or_id) return False @@ -1830,7 +1841,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin): if isinstance(end, str): end = parse_date(end) - proj = self.identity.find_project(name_or_id) + proj = self.identity.find_project(name_or_id, ignore_missing=True) if not proj: raise exceptions.SDKException( f"Project {name_or_id} was requested but was not found " diff --git a/openstack/cloud/_dns.py b/openstack/cloud/_dns.py index d495b8689..2fe13bed8 100644 --- a/openstack/cloud/_dns.py +++ b/openstack/cloud/_dns.py @@ -39,12 +39,9 @@ class DnsCloudMixin(openstackcloud._OpenStackCloudMixin): """ if not filters: filters = {} - zone = self.dns.find_zone( + return self.dns.find_zone( name_or_id=name_or_id, ignore_missing=True, **filters ) - if not zone: - return None - return zone def search_zones(self, name_or_id=None, filters=None): zones = self.list_zones(filters) @@ -136,7 +133,7 @@ class DnsCloudMixin(openstackcloud._OpenStackCloudMixin): error. """ - zone = self.dns.find_zone(name_or_id) + zone = self.dns.find_zone(name_or_id, ignore_missing=True) if not zone: self.log.debug("Zone %s not found for deleting", name_or_id) return False @@ -179,12 +176,9 @@ class DnsCloudMixin(openstackcloud._OpenStackCloudMixin): zone_obj = self.get_zone(zone) if not zone_obj: raise exceptions.SDKException("Zone %s not found." % zone) - try: - return self.dns.find_recordset( - zone=zone_obj, name_or_id=name_or_id, ignore_missing=False - ) - except Exception: - return None + return self.dns.find_recordset( + zone=zone_obj, name_or_id=name_or_id, ignore_missing=True + ) def search_recordsets(self, zone, name_or_id=None, filters=None): recordsets = self.list_recordsets(zone=zone) diff --git a/openstack/cloud/_identity.py b/openstack/cloud/_identity.py index eb78bb2ed..0c7b82078 100644 --- a/openstack/cloud/_identity.py +++ b/openstack/cloud/_identity.py @@ -165,6 +165,7 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): project = self.identity.find_project( name_or_id=name_or_id, domain_id=domain_id, + ignore_missing=True, ) if not project: raise exceptions.SDKException("Project %s not found." % name_or_id) @@ -211,7 +212,7 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): """ try: project = self.identity.find_project( - name_or_id=name_or_id, ignore_missing=True, domain_id=domain_id + name_or_id=name_or_id, domain_id=domain_id, ignore_missing=True ) if not project: self.log.debug("Project %s not found for deleting", name_or_id) @@ -848,7 +849,7 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): wrong during the OpenStack API call. """ if domain_id is None: - return self.identity.find_domain(name_or_id) + return self.identity.find_domain(name_or_id, ignore_missing=False) else: return self.identity.get_domain(domain_id) @@ -948,7 +949,9 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` if something goes wrong during the OpenStack API call. """ - group = self.identity.find_group(name_or_id, **kwargs) + group = self.identity.find_group( + name_or_id, ignore_missing=True, **kwargs + ) if group is None: raise exceptions.SDKException( f"Group {name_or_id} not found for updating" @@ -974,7 +977,7 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): wrong during the OpenStack API call. """ try: - group = self.identity.find_group(name_or_id) + group = self.identity.find_group(name_or_id, ignore_missing=True) if group is None: self.log.debug("Group %s not found for deleting", name_or_id) return False @@ -1215,7 +1218,9 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin): # group, role, project search_args['domain_id'] = data['domain'].id - data['role'] = self.identity.find_role(name_or_id=role) + data['role'] = self.identity.find_role( + name_or_id=role, ignore_missing=True + ) if not data['role']: raise exceptions.SDKException(f'Role {role} not found.') diff --git a/openstack/cloud/_image.py b/openstack/cloud/_image.py index 00b232321..48ce92714 100644 --- a/openstack/cloud/_image.py +++ b/openstack/cloud/_image.py @@ -122,7 +122,7 @@ class ImageCloudMixin(openstackcloud._OpenStackCloudMixin): ' however only one can be used at once' ) - image = self.image.find_image(name_or_id) + image = self.image.find_image(name_or_id, ignore_missing=True) if not image: raise exceptions.NotFoundException( "No images with name or ID %s were found" % name_or_id, None diff --git a/openstack/cloud/_network.py b/openstack/cloud/_network.py index 1abd9a436..f51c7b897 100644 --- a/openstack/cloud/_network.py +++ b/openstack/cloud/_network.py @@ -617,7 +617,7 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` if the resource to set the quota does not exist. """ - proj = self.identity.find_project(name_or_id) + proj = self.identity.find_project(name_or_id, ignore_missing=True) if not proj: raise exceptions.SDKException( f"Project {name_or_id} was requested by was not found " @@ -636,7 +636,7 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` if it's not a valid project """ - proj = self.identity.find_project(name_or_id) + proj = self.identity.find_project(name_or_id, ignore_missing=True) if not proj: raise exc.OpenStackCloudException( f"Project {name_or_id} was requested by was not found " @@ -660,7 +660,7 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` if it's not a valid project or the network client call failed """ - proj = self.identity.find_project(name_or_id) + proj = self.identity.find_project(name_or_id, ignore_missing=True) if not proj: raise exceptions.SDKException( f"Project {name_or_id} was requested by was not found " @@ -1351,7 +1351,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): self.log.debug("No QoS policy data to update") return - curr_policy = self.network.find_qos_policy(name_or_id) + curr_policy = self.network.find_qos_policy( + name_or_id, ignore_missing=True + ) if not curr_policy: raise exceptions.SDKException( "QoS policy %s not found." % name_or_id @@ -1372,7 +1374,7 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): raise exc.OpenStackCloudUnavailableExtension( 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(name_or_id) + policy = self.network.find_qos_policy(name_or_id, ignore_missing=True) if not policy: self.log.debug("QoS policy %s not found for deleting", name_or_id) return False @@ -1419,7 +1421,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1451,7 +1455,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1487,7 +1493,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1581,7 +1589,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1671,7 +1681,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1701,7 +1713,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1733,7 +1747,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1771,7 +1787,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1836,7 +1854,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1866,7 +1886,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1900,7 +1922,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1934,7 +1958,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -1974,7 +2000,9 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): 'QoS extension is not available on target cloud' ) - policy = self.network.find_qos_policy(policy_name_or_id) + policy = self.network.find_qos_policy( + policy_name_or_id, ignore_missing=True + ) if not policy: raise exceptions.NotFoundException( "QoS policy {name_or_id} not Found.".format( @@ -2669,8 +2697,7 @@ class NetworkCloudMixin(_network_common.NetworkCommonCloudMixin): :raises: :class:`~openstack.exceptions.SDKException` on operation error. """ - port = self.network.find_port(name_or_id) - + port = self.network.find_port(name_or_id, ignore_missing=True) if port is None: self.log.debug("Port %s not found for deleting", name_or_id) return False diff --git a/openstack/cloud/_network_common.py b/openstack/cloud/_network_common.py index b504eb114..622886b98 100644 --- a/openstack/cloud/_network_common.py +++ b/openstack/cloud/_network_common.py @@ -1528,7 +1528,9 @@ class NetworkCommonCloudMixin(openstackcloud._OpenStackCloudMixin): if not fixed_address: if len(ports) > 1: if nat_destination: - nat_network = self.network.find_network(nat_destination) + nat_network = self.network.find_network( + nat_destination, ignore_missing=True + ) if not nat_network: raise exceptions.SDKException( 'NAT Destination {nat_destination} was configured'