From 209e537fbdf3f06a8bb6b430443ed71b2b50d2c2 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Fri, 3 May 2019 06:38:25 +0900 Subject: [PATCH] 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 --- .../dashboards/project/instances/views.py | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/openstack_dashboard/dashboards/project/instances/views.py b/openstack_dashboard/dashboards/project/instances/views.py index 6c070dc2f4..5254af8102 100644 --- a/openstack_dashboard/dashboards/project/instances/views.py +++ b/openstack_dashboard/dashboards/project/instances/views.py @@ -155,39 +155,7 @@ class IndexView(tables.PagedTableMixin, tables.DataTableView): # Loop through instances to get flavor info. for instance in instances: - if hasattr(instance, 'image'): - # 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'] - ] + self._populate_image_info(instance, image_dict, volume_dict) flavor_id = instance.flavor["id"] if flavor_id in flavor_dict: @@ -200,6 +168,45 @@ class IndexView(tables.PagedTableMixin, tables.DataTableView): 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): """Process filters by non-API fields