Fix: Refactor image clean 31/68531/3
authorAnil Belur <abelur@linuxfoundation.org>
Fri, 13 Aug 2021 13:20:08 +0000 (23:20 +1000)
committerAnil Belur <abelur@linuxfoundation.org>
Sun, 15 Aug 2021 08:33:34 +0000 (18:33 +1000)
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 <abelur@linuxfoundation.org>
lftools/openstack/image.py
releasenotes/notes/fix-image-cleanup-4266161f3068cf96.yaml [new file with mode: 0644]

index 8e0220d..630f594 100644 (file)
@@ -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 (file)
index 0000000..18460d9
--- /dev/null
@@ -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'