Merge "Update ODL tag to release/carbon"
[functest.git] / functest / utils / openstack_clean.py
old mode 100755 (executable)
new mode 100644 (file)
index cdd9185..d7df8f8
@@ -23,7 +23,6 @@
 #
 
 import logging
-import sys
 import time
 import yaml
 
@@ -80,22 +79,25 @@ def remove_instances(nova_client, default_instances):
         break
 
 
-def remove_images(nova_client, default_images):
+def remove_images(glance_client, default_images):
     logger.debug("Removing Glance images...")
-    images = os_utils.get_images(nova_client)
-    if images is None or len(images) == 0:
+    images = os_utils.get_images(glance_client)
+    if images is None:
+        return -1
+    images = {image.id: image.name for image in images}
+    if len(images) == 0:
         logger.debug("No images found.")
         return
 
     for image in images:
-        image_name = getattr(image, 'name')
-        image_id = getattr(image, 'id')
+        image_id = image
+        image_name = images.get(image_id)
         logger.debug("'%s', ID=%s " % (image_name, image_id))
         if (image_id not in default_images and
                 image_name not in default_images.values()):
             logger.debug("Removing image '%s', ID=%s ..."
                          % (image_name, image_id))
-            if os_utils.delete_glance_image(nova_client, image_id):
+            if os_utils.delete_glance_image(glance_client, image_id):
                 logger.debug("  > Done!")
             else:
                 logger.error("There has been a problem removing the"
@@ -135,9 +137,9 @@ def remove_volumes(cinder_client, default_volumes):
                          "NOT be deleted.")
 
 
-def remove_floatingips(nova_client, default_floatingips):
+def remove_floatingips(neutron_client, default_floatingips):
     logger.debug("Removing floating IPs...")
-    floatingips = os_utils.get_floating_ips(nova_client)
+    floatingips = os_utils.get_floating_ips(neutron_client)
     if floatingips is None or len(floatingips) == 0:
         logger.debug("No floating IPs found.")
         return
@@ -145,13 +147,13 @@ def remove_floatingips(nova_client, default_floatingips):
     init_len = len(floatingips)
     deleted = 0
     for fip in floatingips:
-        fip_id = getattr(fip, 'id')
-        fip_ip = getattr(fip, 'ip')
+        fip_id = fip['id']
+        fip_ip = fip['floating_ip_address']
         logger.debug("'%s', ID=%s " % (fip_ip, fip_id))
         if (fip_id not in default_floatingips and
                 fip_ip not in default_floatingips.values()):
             logger.debug("Removing floating IP %s ..." % fip_id)
-            if os_utils.delete_floating_ip(nova_client, fip_id):
+            if os_utils.delete_floating_ip(neutron_client, fip_id):
                 logger.debug("  > Done!")
                 deleted += 1
             else:
@@ -163,7 +165,7 @@ def remove_floatingips(nova_client, default_floatingips):
 
     timeout = 50
     while timeout > 0:
-        floatingips = os_utils.get_floating_ips(nova_client)
+        floatingips = os_utils.get_floating_ips(neutron_client)
         if floatingips is None or len(floatingips) == (init_len - deleted):
             break
         else:
@@ -379,12 +381,14 @@ def remove_tenants(keystone_client, default_tenants):
 
 
 def main():
+    logging.basicConfig()
     logger.info("Cleaning OpenStack resources...")
 
     nova_client = os_utils.get_nova_client()
     neutron_client = os_utils.get_neutron_client()
     keystone_client = os_utils.get_keystone_client()
     cinder_client = os_utils.get_cinder_client()
+    glance_client = os_utils.get_glance_client()
 
     try:
         with open(OS_SNAPSHOT_FILE) as f:
@@ -411,11 +415,11 @@ def main():
 
     remove_instances(nova_client, default_instances)
     separator()
-    remove_images(nova_client, default_images)
+    remove_images(glance_client, default_images)
     separator()
     remove_volumes(cinder_client, default_volumes)
     separator()
-    remove_floatingips(nova_client, default_floatingips)
+    remove_floatingips(neutron_client, default_floatingips)
     separator()
     remove_networks(neutron_client, default_networks, default_routers)
     separator()
@@ -425,8 +429,4 @@ def main():
     separator()
     remove_tenants(keystone_client, default_tenants)
     separator()
-
-
-if __name__ == '__main__':
-    logging.basicConfig()
-    sys.exit(main())
+    return 0