Merge "Update Yardstick README file"
[yardstick.git] / yardstick / common / openstack_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # yardstick: this file is copied from rally and slightly modified
9 ##############################################################################
10
11 import os
12 import logging
13
14 from keystoneauth1 import loading
15 from keystoneauth1 import session
16
17 log = logging.getLogger(__name__)
18
19 DEFAULT_HEAT_API_VERSION = '1'
20
21
22 def get_credentials():
23     """Returns a creds dictionary filled with parsed from env"""
24     creds = {}
25
26     keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
27
28     if keystone_api_version is None or keystone_api_version == '2':
29         keystone_v3 = False
30         tenant_env = 'OS_TENANT_NAME'
31         tenant = 'tenant_name'
32     else:
33         keystone_v3 = True
34         tenant_env = 'OS_PROJECT_NAME'
35         tenant = 'project_name'
36
37     # The most common way to pass these info to the script is to do it
38     # through environment variables.
39     creds.update({
40         "username": os.environ.get("OS_USERNAME"),
41         "password": os.environ.get("OS_PASSWORD"),
42         "auth_url": os.environ.get("OS_AUTH_URL"),
43         tenant: os.environ.get(tenant_env)
44     })
45
46     if keystone_v3:
47         if os.getenv('OS_USER_DOMAIN_NAME') is not None:
48             creds.update({
49                 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
50             })
51         if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
52             creds.update({
53                 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
54             })
55
56     cacert = os.environ.get("OS_CACERT")
57
58     if cacert is not None:
59         # each openstack client uses differnt kwargs for this
60         creds.update({"cacert": cacert,
61                       "ca_cert": cacert,
62                       "https_ca_cert": cacert,
63                       "https_cacert": cacert,
64                       "ca_file": cacert})
65         creds.update({"insecure": "True", "https_insecure": "True"})
66         if not os.path.isfile(cacert):
67             log.info("WARNING: The 'OS_CACERT' environment variable is set\
68                       to %s but the file does not exist." % cacert)
69
70     return creds
71
72
73 def get_session_auth():
74     loader = loading.get_plugin_loader('password')
75     creds = get_credentials()
76     auth = loader.load_from_options(**creds)
77     return auth
78
79
80 def get_session():
81     auth = get_session_auth()
82     return session.Session(auth=auth)
83
84
85 def get_endpoint(service_type, endpoint_type='publicURL'):
86     auth = get_session_auth()
87     return get_session().get_endpoint(auth=auth,
88                                       service_type=service_type,
89                                       endpoint_type=endpoint_type)
90
91
92 def get_heat_api_version():
93     api_version = os.getenv('HEAT_API_VERSION')
94     if api_version is not None:
95         log.info("HEAT_API_VERSION is set in env as '%s'", api_version)
96         return api_version
97     return DEFAULT_HEAT_API_VERSION