Move deeper-indented code to a separate method
This is a follow-up patch of https://review.opendev.org/#/c/656456/ During the review, it turns out that the logic is too deep and we cannot add more "if" clause. This commit tries to address it. Change-Id: If133b1bed8ae1a591c4ce34e0a86d6dc1c138c68
This commit is contained in:
parent
a1559be778
commit
209e537fbd
|
@ -155,39 +155,7 @@ class IndexView(tables.PagedTableMixin, tables.DataTableView):
|
||||||
|
|
||||||
# Loop through instances to get flavor info.
|
# Loop through instances to get flavor info.
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
if hasattr(instance, 'image'):
|
self._populate_image_info(instance, image_dict, volume_dict)
|
||||||
# Instance from image returns dict
|
|
||||||
if isinstance(instance.image, dict):
|
|
||||||
image_id = instance.image.get('id')
|
|
||||||
if image_id in image_dict:
|
|
||||||
instance.image = image_dict[image_id]
|
|
||||||
# In case image not found in image_dict, set name to empty
|
|
||||||
# to avoid fallback API call to Glance in api/nova.py
|
|
||||||
# until the call is deprecated in api itself
|
|
||||||
else:
|
|
||||||
instance.image['name'] = _("-")
|
|
||||||
# Otherwise trying to get image from volume metadata
|
|
||||||
else:
|
|
||||||
instance_volumes = [
|
|
||||||
attachment
|
|
||||||
for volume in volume_dict.values()
|
|
||||||
for attachment in volume.attachments
|
|
||||||
if attachment['server_id'] == instance.id
|
|
||||||
]
|
|
||||||
# Sorting attached volumes by device name (eg '/dev/sda')
|
|
||||||
instance_volumes.sort(key=lambda attach: attach['device'])
|
|
||||||
# While instance from volume is being created,
|
|
||||||
# it does not have volumes
|
|
||||||
if instance_volumes:
|
|
||||||
# Getting volume object, which is as attached
|
|
||||||
# as the first device
|
|
||||||
boot_volume = volume_dict[instance_volumes[0]['id']]
|
|
||||||
if (hasattr(boot_volume, "volume_image_metadata") and
|
|
||||||
boot_volume.volume_image_metadata['image_id'] in
|
|
||||||
image_dict):
|
|
||||||
instance.image = image_dict[
|
|
||||||
boot_volume.volume_image_metadata['image_id']
|
|
||||||
]
|
|
||||||
|
|
||||||
flavor_id = instance.flavor["id"]
|
flavor_id = instance.flavor["id"]
|
||||||
if flavor_id in flavor_dict:
|
if flavor_id in flavor_dict:
|
||||||
|
@ -200,6 +168,45 @@ class IndexView(tables.PagedTableMixin, tables.DataTableView):
|
||||||
|
|
||||||
return instances
|
return instances
|
||||||
|
|
||||||
|
def _populate_image_info(self, instance, image_dict, volume_dict):
|
||||||
|
if not hasattr(instance, 'image'):
|
||||||
|
return
|
||||||
|
# Instance from image returns dict
|
||||||
|
if isinstance(instance.image, dict):
|
||||||
|
image_id = instance.image.get('id')
|
||||||
|
if image_id in image_dict:
|
||||||
|
instance.image = image_dict[image_id]
|
||||||
|
# In case image not found in image_dict, set name to empty
|
||||||
|
# to avoid fallback API call to Glance in api/nova.py
|
||||||
|
# until the call is deprecated in api itself
|
||||||
|
else:
|
||||||
|
instance.image['name'] = _("-")
|
||||||
|
# Otherwise trying to get image from volume metadata
|
||||||
|
else:
|
||||||
|
instance_volumes = [
|
||||||
|
attachment
|
||||||
|
for volume in volume_dict.values()
|
||||||
|
for attachment in volume.attachments
|
||||||
|
if attachment['server_id'] == instance.id
|
||||||
|
]
|
||||||
|
# While instance from volume is being created,
|
||||||
|
# it does not have volumes
|
||||||
|
if not instance_volumes:
|
||||||
|
return
|
||||||
|
# Sorting attached volumes by device name (eg '/dev/sda')
|
||||||
|
instance_volumes.sort(key=lambda attach: attach['device'])
|
||||||
|
# Getting volume object, which is as attached
|
||||||
|
# as the first device
|
||||||
|
boot_volume = volume_dict[instance_volumes[0]['id']]
|
||||||
|
if hasattr(boot_volume, "volume_image_metadata"):
|
||||||
|
image_id = boot_volume.volume_image_metadata['image_id']
|
||||||
|
try:
|
||||||
|
instance.image = image_dict[image_id]
|
||||||
|
except KeyError:
|
||||||
|
# KeyError occurs when volume was created from image and
|
||||||
|
# then this image is deleted.
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def process_non_api_filters(search_opts, non_api_filter_info):
|
def process_non_api_filters(search_opts, non_api_filter_info):
|
||||||
"""Process filters by non-API fields
|
"""Process filters by non-API fields
|
||||||
|
|
Loading…
Reference in New Issue