1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
13 from keystoneauth1 import loading
14 from keystoneauth1 import session
16 log = logging.getLogger(__name__)
18 DEFAULT_HEAT_API_VERSION = '1'
21 def get_credentials():
22 """Returns a creds dictionary filled with parsed from env"""
25 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
27 if keystone_api_version is None or keystone_api_version == '2':
29 tenant_env = 'OS_TENANT_NAME'
30 tenant = 'tenant_name'
33 tenant_env = 'OS_PROJECT_NAME'
34 tenant = 'project_name'
36 # The most common way to pass these info to the script is to do it
37 # through environment variables.
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)
46 if os.getenv('OS_USER_DOMAIN_NAME') is not None:
48 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
50 if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
52 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
55 cacert = os.environ.get("OS_CACERT")
57 if cacert is not None:
58 # each openstack client uses differnt kwargs for this
59 creds.update({"cacert": cacert,
61 "https_ca_cert": cacert,
62 "https_cacert": 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)
72 def get_session_auth():
73 loader = loading.get_plugin_loader('password')
74 creds = get_credentials()
75 auth = loader.load_from_options(**creds)
80 auth = get_session_auth()
81 return session.Session(auth=auth)
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)
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)
96 return DEFAULT_HEAT_API_VERSION