Fix formatting error
[functest.git] / utils / openstack_utils.py
index bc718bb..8081594 100755 (executable)
@@ -16,6 +16,7 @@ import time
 
 from cinderclient import client as cinderclient
 import functest.utils.functest_logger as ft_logger
+import functest.utils.functest_utils as ft_utils
 from glanceclient import client as glanceclient
 from keystoneclient.v2_0 import client as keystoneclient
 from neutronclient.v2_0 import client as neutronclient
@@ -210,6 +211,45 @@ def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
     return id
 
 
+def create_flavor(nova_client, flavor_name, ram, disk, vcpus):
+    try:
+        flavor = nova_client.flavors.create(flavor_name, ram, vcpus, disk)
+        try:
+            extra_specs = ft_utils.get_functest_config(
+                'general.flavor_extra_specs')
+            flavor.set_keys(extra_specs)
+        except ValueError:
+            # flavor extra specs are not configured, therefore skip the update
+            pass
+
+    except Exception, e:
+        logger.error("Error [create_flavor(nova_client, '%s', '%s', '%s', "
+                     "'%s')]: %s" % (flavor_name, ram, disk, vcpus, e))
+        return None
+    return flavor.id
+
+
+def get_or_create_flavor(flavor_name, ram, disk, vcpus):
+    flavor_exists = False
+    nova_client = get_nova_client()
+
+    flavor_id = get_flavor_id(nova_client, flavor_name)
+    if flavor_id != '':
+        logger.info("Using existing flavor '%s'..." % flavor_name)
+        flavor_exists = True
+    else:
+        logger.info("Creating flavor '%s' with '%s' RAM, '%s' disk size, "
+                    "'%s' vcpus..." % (flavor_name, ram, disk, vcpus))
+        flavor_id = create_flavor(nova_client, flavor_name, ram, disk, vcpus)
+        if not flavor_id:
+            logger.error("Failed to create flavor '%s'..." % (flavor_name))
+        else:
+            logger.debug("Flavor '%s' with ID=%s created successfully."
+                         % (flavor_name, flavor_id))
+
+    return flavor_exists, flavor_id
+
+
 def get_floating_ips(nova_client):
     try:
         floating_ips = nova_client.floating_ips.list()
@@ -232,16 +272,6 @@ def get_hypervisors(nova_client):
         return None
 
 
-def create_flavor(nova_client, flavor_name, ram, disk, vcpus):
-    try:
-        flavor = nova_client.flavors.create(flavor_name, ram, vcpus, disk)
-    except Exception, e:
-        logger.error("Error [create_flavor(nova_client, '%s', '%s', '%s', "
-                     "'%s')]: %s" % (flavor_name, ram, disk, vcpus, e))
-        return None
-    return flavor.id
-
-
 def create_instance(flavor_name,
                     image_id,
                     network_id,
@@ -359,7 +389,7 @@ def delete_floating_ip(nova_client, floatingip_id):
         nova_client.floating_ips.delete(floatingip_id)
         return True
     except Exception, e:
-        logger.error("Error [delete_floating_ip(nova_client, '%s')]:"
+        logger.error("Error [delete_floating_ip(nova_client, '%s')]: %s"
                      % (floatingip_id, e))
         return False
 
@@ -705,6 +735,11 @@ def create_network_association(neutron_client, bgpvpn_id, neutron_network_id):
     return neutron_client.create_network_association(bgpvpn_id, json_body)
 
 
+def create_router_association(neutron_client, bgpvpn_id, router_id):
+    json_body = {"router_association": {"router_id": router_id}}
+    return neutron_client.create_router_association(bgpvpn_id, json_body)
+
+
 def update_bgpvpn(neutron_client, bgpvpn_id, **kwargs):
     json_body = {"bgpvpn": kwargs}
     return neutron_client.update_bgpvpn(bgpvpn_id, json_body)
@@ -897,11 +932,19 @@ def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
             if logger:
                 logger.info("Creating image '%s' from '%s'..." % (image_name,
                                                                   file_path))
+            try:
+                properties = ft_utils.get_functest_config(
+                    'general.image_properties')
+            except ValueError:
+                # image properties are not configured
+                # therefore don't add any properties
+                properties = {}
             with open(file_path) as fimage:
                 image = glance_client.images.create(name=image_name,
                                                     is_public=public,
                                                     disk_format=disk,
                                                     container_format=container,
+                                                    properties=properties,
                                                     data=fimage)
             image_id = image.id
         return image_id
@@ -1077,7 +1120,7 @@ def create_tenant(keystone_client, tenant_name, tenant_description):
                                                 enabled=True)
         return tenant.id
     except Exception, e:
-        logger.error("Error [create_tenant(cinder_client, '%s', '%s')]: %s"
+        logger.error("Error [create_tenant(keystone_client, '%s', '%s')]: %s"
                      % (tenant_name, tenant_description, e))
         return None