Heat: support create and attach volume in heat type context
[yardstick.git] / yardstick / common / openstack_utils.py
index aa369b8..f027b79 100644 (file)
@@ -15,6 +15,7 @@ import logging
 
 from keystoneauth1 import loading
 from keystoneauth1 import session
+from cinderclient import client as cinderclient
 from novaclient import client as novaclient
 from glanceclient import client as glanceclient
 from neutronclient.neutron import client as neutronclient
@@ -62,20 +63,6 @@ def get_credentials():
                 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
             })
 
-    cacert = os.environ.get("OS_CACERT")
-
-    if cacert is not None:
-        # each openstack client uses differnt kwargs for this
-        creds.update({"cacert": cacert,
-                      "ca_cert": cacert,
-                      "https_ca_cert": cacert,
-                      "https_cacert": cacert,
-                      "ca_file": cacert})
-        creds.update({"insecure": "True", "https_insecure": "True"})
-        if not os.path.isfile(cacert):
-            log.info("WARNING: The 'OS_CACERT' environment variable is set \
-                      to %s but the file does not exist.", cacert)
-
     return creds
 
 
@@ -88,14 +75,25 @@ def get_session_auth():
 
 def get_session():
     auth = get_session_auth()
-    return session.Session(auth=auth)
+    try:
+        cacert = os.environ['OS_CACERT']
+    except KeyError:
+        return session.Session(auth=auth)
+    else:
+        insecure = os.getenv('OS_INSECURE', '').lower() == 'true'
+        cacert = False if insecure else cacert
+        return session.Session(auth=auth, verify=cacert)
 
 
 def get_endpoint(service_type, endpoint_type='publicURL'):
     auth = get_session_auth()
+    # for multi-region, we need to specify region
+    # when finding the endpoint
     return get_session().get_endpoint(auth=auth,
                                       service_type=service_type,
-                                      endpoint_type=endpoint_type)
+                                      endpoint_type=endpoint_type,
+                                      region_name=os.environ.get(
+                                          "OS_REGION_NAME"))
 
 
 # *********************************************
@@ -111,6 +109,21 @@ def get_heat_api_version():     # pragma: no cover
         return api_version
 
 
+def get_cinder_client_version():      # pragma: no cover
+    try:
+        api_version = os.environ['OS_VOLUME_API_VERSION']
+    except KeyError:
+        return DEFAULT_API_VERSION
+    else:
+        log.info("OS_VOLUME_API_VERSION is set in env as '%s'", api_version)
+        return api_version
+
+
+def get_cinder_client():      # pragma: no cover
+    sess = get_session()
+    return cinderclient.Client(get_cinder_client_version(), session=sess)
+
+
 def get_nova_client_version():      # pragma: no cover
     try:
         api_version = os.environ['OS_COMPUTE_API_VERSION']
@@ -433,3 +446,11 @@ def get_port_id_by_ip(neutron_client, ip_address):      # pragma: no cover
 def get_image_id(glance_client, image_name):    # pragma: no cover
     images = glance_client.images.list()
     return next((i.id for i in images if i.name == image_name), None)
+
+
+# *********************************************
+#   CINDER
+# *********************************************
+def get_volume_id(volume_name):    # pragma: no cover
+    volumes = get_cinder_client().volumes.list()
+    return next((v.id for v in volumes if v.name == volume_name), None)