Merge "Yardstick Preliminary Documentation"
[yardstick.git] / yardstick / common / utils.py
index 473bbf5..d2be800 100644 (file)
@@ -26,9 +26,7 @@ import sys
 from functools import reduce
 
 import yaml
-from keystoneauth1 import identity
-from keystoneauth1 import session
-from neutronclient.v2_0 import client
+from six.moves import configparser
 from oslo_utils import importutils
 from oslo_serialization import jsonutils
 
@@ -134,24 +132,34 @@ def source_env(env_file):
     return env
 
 
-def get_openstack_session():
-    auth = identity.Password(auth_url=os.environ.get('OS_AUTH_URL'),
-                             username=os.environ.get('OS_USERNAME'),
-                             password=os.environ.get('OS_PASSWORD'),
-                             tenant_name=os.environ.get('OS_TENANT_NAME'))
-    return session.Session(auth=auth)
-
-
-def get_neutron_client():
-    sess = get_openstack_session()
-    neutron_client = client.Client(session=sess)
-    return neutron_client
+def read_json_from_file(path):
+    with open(path, 'r') as f:
+        j = f.read()
+    # don't use jsonutils.load() it conflicts with already decoded input
+    return jsonutils.loads(j)
 
 
 def write_json_to_file(path, data, mode='w'):
-    write_file(path, jsonutils.dump_as_bytes(data), mode)
+    with open(path, mode) as f:
+        jsonutils.dump(data, f)
 
 
 def write_file(path, data, mode='w'):
     with open(path, mode) as f:
         f.write(data)
+
+
+def parse_ini_file(path):
+    parser = configparser.ConfigParser()
+    parser.read(path)
+
+    try:
+        default = {k: v for k, v in parser.items('DEFAULT')}
+    except configparser.NoSectionError:
+        default = {}
+
+    config = dict(DEFAULT=default,
+                  **{s: {k: v for k, v in parser.items(
+                      s)} for s in parser.sections()})
+
+    return config