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 ##############################################################################
10 from __future__ import absolute_import
14 from keystoneauth1 import loading
15 from keystoneauth1 import session
17 log = logging.getLogger(__name__)
19 DEFAULT_HEAT_API_VERSION = '1'
22 def get_credentials():
23 """Returns a creds dictionary filled with parsed from env"""
26 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
28 if keystone_api_version is None or keystone_api_version == '2':
30 tenant_env = 'OS_TENANT_NAME'
31 tenant = 'tenant_name'
34 tenant_env = 'OS_PROJECT_NAME'
35 tenant = 'project_name'
37 # The most common way to pass these info to the script is to do it
38 # through environment variables.
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)
47 if os.getenv('OS_USER_DOMAIN_NAME') is not None:
49 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
51 if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
53 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
56 cacert = os.environ.get("OS_CACERT")
58 if cacert is not None:
59 # each openstack client uses differnt kwargs for this
60 creds.update({"cacert": cacert,
62 "https_ca_cert": cacert,
63 "https_cacert": 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)
73 def get_session_auth():
74 loader = loading.get_plugin_loader('password')
75 creds = get_credentials()
76 auth = loader.load_from_options(**creds)
81 auth = get_session_auth()
82 return session.Session(auth=auth)
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)
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)
97 return DEFAULT_HEAT_API_VERSION