From 41d6c1feff26b6eb4a6634593d5d32b420925a23 Mon Sep 17 00:00:00 2001 From: Anil Belur Date: Fri, 13 Aug 2021 23:20:08 +1000 Subject: [PATCH] Fix: Refactor image clean Refactor image cleanup code to handle attribute types and multi-clouds config correctly. Error: AttributeError: 'Connection' object has no attribute 'cloud_config' Issue-ID: RELENG-3869 Change-Id: If6dd04507f6eb94786f6589299ce31f62a009b84 Signed-off-by: Anil Belur --- lftools/openstack/image.py | 38 ++++++++++------------ .../notes/fix-image-cleanup-4266161f3068cf96.yaml | 8 +++++ 2 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml diff --git a/lftools/openstack/image.py b/lftools/openstack/image.py index 8e0220d7..630f594e 100644 --- a/lftools/openstack/image.py +++ b/lftools/openstack/image.py @@ -74,10 +74,9 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None): """ def _remove_images_from_cloud(images, cloud): - log.info("Removing {} images from {}.".format(len(images), cloud.cloud_config.name)) + log.info("Removing {} images from {}.".format(len(images), cloud.config._name)) project_info = cloud._get_project_info() for image in images: - if image.is_protected: log.warning("Image {} is protected. Cannot remove...".format(image.name)) continue @@ -87,9 +86,7 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None): continue if project_info["id"] != image.owner: - log.warning( - "Image {} not owned by project {}. Cannot remove...".format(image.name, cloud.cloud_config.name) - ) + log.warning("Image {} not owned by project {}. Cannot remove...".format(image.name, cloud.config._name)) continue try: @@ -104,27 +101,26 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None): if not result: log.warning( - 'Failed to remove "{}" from {}. Possibly already deleted.'.format( - image.name, cloud.cloud_config.name - ) + 'Failed to remove "{}" from {}. Possibly already deleted.'.format(image.name, cloud.config._name) ) else: - log.info('Removed "{}" from {}.'.format(image.name, cloud.cloud_config.name)) + log.info('Removed "{}" from {}.'.format(image.name, cloud.config._name)) - cloud = openstack.connection.from_config(cloud=os_cloud) + cloud_list = [] if clouds: - cloud_list = [] for c in clouds.split(","): - cloud_list.append(openstack.connection.from_config(cloud=c)) - - images = cloud.list_images() - filtered_images = _filter_images(images, days, hide_public, ci_managed) - - if clouds: - for c in cloud_list: - _remove_images_from_cloud(filtered_images, c) - else: - _remove_images_from_cloud(filtered_images, cloud) + cloud_list.append(c) + + if os_cloud not in cloud_list: + cloud_list.append(os_cloud) + + for c in cloud_list: + cloud = openstack.connection.from_config(cloud=c) + images = cloud.list_images() + if images: + filtered_images = _filter_images(images, days, hide_public, ci_managed) + if filtered_images: + _remove_images_from_cloud(filtered_images, cloud) def share(os_cloud, image, clouds): diff --git a/releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml b/releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml new file mode 100644 index 00000000..18460d9a --- /dev/null +++ b/releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Refactor image cleanup code to handle attribute types + and multi-cloud config correctly. (Issue-ID: RELENG-3869) + + Error: + AttributeError: 'Connection' object has no attribute 'cloud_config' -- 2.16.6