Merge "Add --hwlb options as a command line argument for SampleVNF"
[yardstick.git] / yardstick / benchmark / scenarios / networking / sfc_openstack.py
index d1d45d8..aaab213 100644 (file)
@@ -1,3 +1,13 @@
+##############################################################################
+# Copyright (c) 2017 Ericsson AB and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+from __future__ import print_function
+from __future__ import absolute_import
 import os
 from novaclient import client as novaclient
 from neutronclient.v2_0 import client as neutronclient
@@ -24,11 +34,13 @@ def get_credentials(service):  # pragma: no cover
 
     # The most common way to pass these info to the script is to do it through
     # environment variables.
+    # NOTE(ralonsoh): OS_TENANT_NAME is deprecated.
+    project_name = os.environ.get('OS_PROJECT_NAME', 'admin')
     creds.update({
         "username": os.environ.get('OS_USERNAME', "admin"),
         password: os.environ.get("OS_PASSWORD", 'admin'),
         "auth_url": os.environ.get("OS_AUTH_URL"),
-        tenant: os.environ.get("OS_TENANT_NAME", "admin"),
+        tenant: os.environ.get("OS_TENANT_NAME", project_name),
     })
     cacert = os.environ.get("OS_CACERT")
     if cacert is not None:
@@ -40,8 +52,8 @@ def get_credentials(service):  # pragma: no cover
                       "ca_file": cacert})
         creds.update({"insecure": "True", "https_insecure": "True"})
         if not os.path.isfile(cacert):
-            print ("WARNING: The 'OS_CACERT' environment variable is " +
-                   "set to %s but the file does not exist." % cacert)
+            print(("WARNING: The 'OS_CACERT' environment variable is " +
+                   "set to %s but the file does not exist." % cacert))
     return creds
 
 
@@ -49,8 +61,8 @@ def get_instances(nova_client):  # pragma: no cover
     try:
         instances = nova_client.servers.list(search_opts={'all_tenants': 1})
         return instances
-    except Exception, e:
-        print "Error [get_instances(nova_client)]:", e
+    except Exception as e:  # pylint: disable=broad-except
+        print("Error [get_instances(nova_client)]:", e)
         return None
 
 
@@ -62,8 +74,8 @@ def get_SFs(nova_client):  # pragma: no cover
             if "sfc_test" not in instance.name:
                 SFs.append(instance)
         return SFs
-    except Exception, e:
-        print "Error [get_SFs(nova_client)]:", e
+    except Exception as e:  # pylint: disable=broad-except
+        print("Error [get_SFs(nova_client)]:", e)
         return None
 
 
@@ -79,12 +91,12 @@ def create_floating_ips(neutron_client):  # pragma: no cover
     ips = []
     props = {'floating_network_id': extnet_id}
     try:
-        while (len(ips) < 2):
+        while len(ips) < 2:
             ip_json = neutron_client.create_floatingip({'floatingip': props})
             fip_addr = ip_json['floatingip']['floating_ip_address']
             ips.append(fip_addr)
-    except Exception, e:
-        print "Error [create_floating_ip(neutron_client)]:", e
+    except Exception as e:  # pylint: disable=broad-except
+        print("Error [create_floating_ip(neutron_client)]:", e)
         return None
     return ips
 
@@ -96,9 +108,9 @@ def floatIPtoSFs(SFs, floatips):  # pragma: no cover
             SF.add_floating_ip(floatips[i])
             i = i + 1
         return True
-    except Exception, e:
-        print ("Error [add_floating_ip(nova_client, '%s', '%s')]:" %
-               (SF, floatips[i]), e)
+    except Exception as e:  # pylint: disable=broad-except
+        print(("Error [add_floating_ip(nova_client, '%s', '%s')]:" %
+               (SF, floatips[i]), e))
         return False
 
 
@@ -112,6 +124,3 @@ def get_an_IP():  # pragma: no cover
     floatips = create_floating_ips(neutron_client)
     floatIPtoSFs(SFs, floatips)
     return floatips
-
-if __name__ == '__main__':  # pragma: no cover
-    get_an_IP()