BugFix: correct Copyright info in openstack_utils.py
[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 ##############################################################################
9
10 import os
11 import logging
12
13 from keystoneauth1 import loading
14 from keystoneauth1 import session
15
16 log = logging.getLogger(__name__)
17
18 DEFAULT_HEAT_API_VERSION = '1'
19
20
21 def get_credentials():
22     """Returns a creds dictionary filled with parsed from env"""
23     creds = {}
24
25     keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
26
27     if keystone_api_version is None or keystone_api_version == '2':
28         keystone_v3 = False
29         tenant_env = 'OS_TENANT_NAME'
30         tenant = 'tenant_name'
31     else:
32         keystone_v3 = True
33         tenant_env = 'OS_PROJECT_NAME'
34         tenant = 'project_name'
35
36     # The most common way to pass these info to the script is to do it
37     # through environment variables.
38     creds.update({
39         "username": os.environ.get("OS_USERNAME"),
40         "password": os.environ.get("OS_PASSWORD"),
41         "auth_url": os.environ.get("OS_AUTH_URL"),
42         tenant: os.environ.get(tenant_env)
43     })
44
45     if keystone_v3:
46         if os.getenv('OS_USER_DOMAIN_NAME') is not None:
47             creds.update({
48                 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
49             })
50         if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
51             creds.update({
52                 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
53             })
54
55     cacert = os.environ.get("OS_CACERT")
56
57     if cacert is not None:
58         # each openstack client uses differnt kwargs for this
59         creds.update({"cacert": cacert,
60                       "ca_cert": cacert,
61                       "https_ca_cert": cacert,
62                       "https_cacert": cacert,
63                       "ca_file": cacert})
64         creds.update({"insecure": "True", "https_insecure": "True"})
65         if not os.path.isfile(cacert):
66             log.info("WARNING: The 'OS_CACERT' environment variable is set\
67                       to %s but the file does not exist." % cacert)
68
69     return creds
70
71
72 def get_session_auth():
73     loader = loading.get_plugin_loader('password')
74     creds = get_credentials()
75     auth = loader.load_from_options(**creds)
76     return auth
77
78
79 def get_session():
80     auth = get_session_auth()
81     return session.Session(auth=auth)
82
83
84 def get_endpoint(service_type, endpoint_type='publicURL'):
85     auth = get_session_auth()
86     return get_session().get_endpoint(auth=auth,
87                                       service_type=service_type,
88                                       endpoint_type=endpoint_type)
89
90
91 def get_heat_api_version():
92     api_version = os.getenv('HEAT_API_VERSION')
93     if api_version is not None:
94         log.info("HEAT_API_VERSION is set in env as '%s'", api_version)
95         return api_version
96     return DEFAULT_HEAT_API_VERSION