cloud: Stop reimplementing ignore_missing=False

Rather than setting this to True and then checking locally for Noneness,
just set it to False. We lose a small bit of granularity in error
message but remove quite a bit more code.

Change-Id: Ic5f8c0d0c3bc4e86cf7bbe5a159f78dd4e384aaa
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2024-09-16 16:28:30 +01:00 committed by Takashi Kajinami
parent bd6ab754e6
commit 52672a57f0
6 changed files with 20 additions and 107 deletions

View File

@ -136,12 +136,7 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin):
if isinstance(image, dict):
kwargs['imageRef'] = image['id']
else: # object
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 "
f"volume but was not found on the cloud"
)
image_obj = self.image.find_image(image, ignore_missing=False)
kwargs['imageRef'] = image_obj['id']
kwargs = self._get_volume_kwargs(kwargs)
kwargs['size'] = size
@ -267,10 +262,8 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin):
params = {}
if name_or_id:
project = self.identity.find_project(
name_or_id, ignore_missing=True
name_or_id, ignore_missing=False
)
if not project:
raise exceptions.SDKException("project does not exist")
params['project'] = project
return self.block_storage.get_limits(**params)

View File

@ -381,13 +381,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
params = {}
if name_or_id:
project = self.identity.find_project(
name_or_id, ignore_missing=True
name_or_id, ignore_missing=False
)
if not project:
raise exceptions.SDKException(
f"Project {name_or_id} was requested but was not found "
f"on the cloud"
)
params['tenant_id'] = project.id
return self.compute.get_limits(**params).absolute
@ -429,13 +424,12 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
"""
if not filters:
filters = {}
flavor = self.compute.find_flavor(
return self.compute.find_flavor(
name_or_id,
get_extra_specs=get_extra,
ignore_missing=True,
**filters,
)
return flavor
def get_flavor_by_id(self, id, get_extra=False):
"""Get a flavor by ID
@ -835,13 +829,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
group_id = group['id']
else: # object
group_obj = self.compute.find_server_group(
group, ignore_missing=True
group, ignore_missing=False
)
if not group_obj:
raise exceptions.SDKException(
"Server Group {group} was requested but was not found"
" on the cloud".format(group=group)
)
group_id = group_obj['id']
if 'scheduler_hints' not in kwargs:
kwargs['scheduler_hints'] = {}
@ -871,17 +860,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
network_id = net['id']
else:
network_obj = self.network.find_network(
net, ignore_missing=True
net, ignore_missing=False
)
if not network_obj:
raise exceptions.SDKException(
'Network {network} is not a valid network in'
' {cloud}:{region}'.format(
network=network,
cloud=self.name,
region=self._compute_region,
)
)
network_id = network_obj['id']
nics.append({'net-id': network_id})
@ -902,14 +882,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
elif 'net-name' in nic:
net_name = nic.pop('net-name')
nic_net = self.network.find_network(
net_name, ignore_missing=True
net_name, ignore_missing=False
)
if not nic_net:
raise exceptions.SDKException(
"Requested network {net} could not be found.".format(
net=net_name
)
)
net['uuid'] = nic_net['id']
for ip_key in ('v4-fixed-ip', 'v6-fixed-ip', 'fixed_ip'):
fixed_ip = nic.pop(ip_key, None)
@ -948,12 +922,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
if isinstance(image, dict):
kwargs['imageRef'] = image['id']
else:
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 "
f"on the cloud"
)
image_obj = self.image.find_image(image, ignore_missing=False)
kwargs['imageRef'] = image_obj.id
# TODO(stephenfin): Drop support for dicts: we should only accept
@ -1055,13 +1024,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
volume_id = boot_volume['id']
else:
volume = self.block_storage.find_volume(
boot_volume, ignore_missing=True
boot_volume, ignore_missing=False
)
if not volume:
raise exceptions.SDKException(
f"Volume {volume} was requested but was not found "
f"on the cloud"
)
volume_id = volume['id']
block_mapping = {
'boot_index': '0',
@ -1078,12 +1042,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
if isinstance(image, dict):
image_obj = image
else:
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 "
f"on the cloud"
)
image_obj = self.image.find_image(image, ignore_missing=False)
block_mapping = {
'boot_index': '0',
@ -1115,13 +1074,8 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
volume_id = volume['id']
else:
volume_obj = self.block_storage.find_volume(
volume, ignore_missing=True
volume, ignore_missing=False
)
if not volume_obj:
raise exceptions.SDKException(
f"Volume {volume} was requested but was not found "
f"on the cloud"
)
volume_id = volume_obj['id']
block_mapping = {
'boot_index': '-1',
@ -1841,14 +1795,9 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
if isinstance(end, str):
end = parse_date(end)
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 "
f"on the cloud"
)
project = self.identity.find_project(name_or_id, ignore_missing=False)
return self.compute.get_usage(proj, start, end)
return self.compute.get_usage(project, start, end)
def _encode_server_userdata(self, userdata):
if hasattr(userdata, 'read'):

View File

@ -165,10 +165,8 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin):
project = self.identity.find_project(
name_or_id=name_or_id,
domain_id=domain_id,
ignore_missing=True,
ignore_missing=False,
)
if not project:
raise exceptions.SDKException("Project %s not found." % name_or_id)
if enabled is not None:
kwargs.update({'enabled': enabled})
project = self.identity.update_project(project, **kwargs)
@ -950,12 +948,8 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin):
wrong during the OpenStack API call.
"""
group = self.identity.find_group(
name_or_id, ignore_missing=True, **kwargs
name_or_id, ignore_missing=False, **kwargs
)
if group is None:
raise exceptions.SDKException(
f"Group {name_or_id} not found for updating"
)
group_ref = {}
if name:
@ -1218,11 +1212,7 @@ class IdentityCloudMixin(openstackcloud._OpenStackCloudMixin):
# group, role, project
search_args['domain_id'] = data['domain'].id
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.')
data['role'] = self.identity.find_role(role, ignore_missing=False)
if user:
# use cloud.get_user to save us from bad searching by name

View File

@ -122,12 +122,7 @@ class ImageCloudMixin(openstackcloud._OpenStackCloudMixin):
' however only one can be used at once'
)
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
)
image = self.image.find_image(name_or_id, ignore_missing=False)
return self.image.download_image(
image, output=output_file or output_path, chunk_size=chunk_size
)

View File

@ -122,15 +122,7 @@ class TestProject(base.TestCase):
),
]
)
# NOTE(notmorgan): This test (and shade) does not represent a case
# where the project is in the project list but a 404 is raised when
# the PATCH is issued. This is a bug in shade and should be fixed,
# shade will raise an attribute error instead of the proper
# project not found exception.
with testtools.ExpectedException(
exceptions.SDKException,
"Project %s not found." % project_data.project_id,
):
with testtools.ExpectedException(exceptions.NotFoundException):
self.cloud.update_project(project_data.project_id)
self.assert_calls()

View File

@ -1742,10 +1742,7 @@ class TestRoleAssignment(base.TestCase):
)
self.register_uris(uris)
with testtools.ExpectedException(
exceptions.SDKException,
f'Role {self.role_data.role_name} not found',
):
with testtools.ExpectedException(exceptions.NotFoundException):
self.cloud.grant_role(
self.role_data.role_name,
group=self.group_data.group_name,
@ -1782,10 +1779,7 @@ class TestRoleAssignment(base.TestCase):
)
self.register_uris(uris)
with testtools.ExpectedException(
exceptions.SDKException,
f'Role {self.role_data.role_name} not found',
):
with testtools.ExpectedException(exceptions.NotFoundException):
self.cloud.revoke_role(
self.role_data.role_name,
group=self.group_data.group_name,