Merge "cloud: Stop reimplementing ignore_missing=False"

This commit is contained in:
Zuul 2024-10-02 10:22:57 +00:00 committed by Gerrit Code Review
commit c6e857cf12
6 changed files with 20 additions and 107 deletions

View File

@ -142,12 +142,7 @@ class BlockStorageCloudMixin(openstackcloud._OpenStackCloudMixin):
)
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
@ -273,10 +268,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

@ -384,13 +384,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
@ -432,13 +427,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
@ -843,13 +837,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'] = {}
@ -884,17 +873,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})
@ -915,14 +895,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)
@ -966,12 +940,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
)
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
if isinstance(flavor, dict):
@ -1083,13 +1052,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',
@ -1111,12 +1075,7 @@ class ComputeCloudMixin(_network_common.NetworkCommonCloudMixin):
)
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',
@ -1153,13 +1112,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',
@ -1879,14 +1833,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,