Fix: Improve parameter handling in os/image cleanup 70/70970/10 v0.37.1
authorMatthew Watkins <mwatkins@linuxfoundation.org>
Mon, 7 Nov 2022 16:55:51 +0000 (16:55 +0000)
committerMatthew Watkins <mwatkins@linuxfoundation.org>
Fri, 25 Nov 2022 08:41:48 +0000 (08:41 +0000)
Issue: RELENG-4467
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Change-Id: If91463f83c4be698415e695a83f10f416e379dac

lftools/openstack/image.py
releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml [new file with mode: 0644]

index 630f594..b5a221f 100644 (file)
@@ -18,11 +18,11 @@ import subprocess
 import sys
 import tempfile
 from datetime import datetime, timedelta
+from six.moves import urllib
 
 import openstack
 import openstack.config
 from openstack.cloud.exc import OpenStackCloudException
-from six.moves import urllib
 
 log = logging.getLogger(__name__)
 
@@ -35,19 +35,31 @@ def _filter_images(images, days=0, hide_public=False, ci_managed=True):
     :arg bool hide_public: Whether or not to include public images.
     """
     filtered = []
+    bad_attribute = ""
     for image in images:
         if hide_public and image.is_public:
             continue
         if ci_managed and image.metadata.get("ci_managed", None) != "yes":
             continue
-        if image.protected:
-            continue
+        # Safely handle potentially problematic attributes
+        try:
+            if image.is_protected:
+                continue
+        except AttributeError:
+            bad_attribute = "image.is_protected"
+        try:
+            if image.protected:
+                continue
+        except AttributeError:
+            bad_attribute = "image.protected"
         if days and (
             datetime.strptime(image.created_at, "%Y-%m-%dT%H:%M:%SZ") >= datetime.now() - timedelta(days=days)
         ):
             continue
 
         filtered.append(image)
+    if bad_attribute:
+        log.warning("Use of " + bad_attribute + " resulted in an exception")
     return filtered
 
 
@@ -73,13 +85,24 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None):
         from. Otherwise os_cloud will be used.
     """
 
+    bad_attribute = ""
     def _remove_images_from_cloud(images, cloud):
         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
+            # Safely handle potentially problematic attributes
+            try:
+                if image.is_protected:
+                    log.warning("Image {} is protected. Cannot remove...".format(image.name))
+                    continue
+            except AttributeError:
+                bad_attribute = "image.is_protected"
+            try:
+                if image.protected:
+                    log.warning("Image {} is protected. Cannot remove...".format(image.name))
+                    continue
+            except AttributeError:
+                bad_attribute = "image.protected"
 
             if image.visibility == "shared":
                 log.warning("Image {} is shared. Cannot remove...".format(image.name))
@@ -121,7 +144,8 @@ def cleanup(os_cloud, days=0, hide_public=False, ci_managed=True, clouds=None):
             filtered_images = _filter_images(images, days, hide_public, ci_managed)
         if filtered_images:
             _remove_images_from_cloud(filtered_images, cloud)
-
+    if bad_attribute:
+        log.warning("Use of " + bad_attribute + " resulted in an exception")
 
 def share(os_cloud, image, clouds):
     """Share image with another tenant."""
diff --git a/releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml b/releasenotes/notes/fix-os-image-cleanup-94c98f53e6c13edb.yaml
new file mode 100644 (file)
index 0000000..909d047
--- /dev/null
@@ -0,0 +1,4 @@
+---
+fixes:
+  - |
+    Correct parameter name in os image cleanup code