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'
19 DEFAULT_NOVA_API_VERSION = '2'
20 DEFAULT_GLANCE_API_VERSION = '2'
23 def get_credentials():
24 """Returns a creds dictionary filled with parsed from env"""
27 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
29 if keystone_api_version is None or keystone_api_version == '2':
31 tenant_env = 'OS_TENANT_NAME'
32 tenant = 'tenant_name'
35 tenant_env = 'OS_PROJECT_NAME'
36 tenant = 'project_name'
38 # The most common way to pass these info to the script is to do it
39 # through environment variables.
41 "username": os.environ.get("OS_USERNAME"),
42 "password": os.environ.get("OS_PASSWORD"),
43 "auth_url": os.environ.get("OS_AUTH_URL"),
44 tenant: os.environ.get(tenant_env)
48 if os.getenv('OS_USER_DOMAIN_NAME') is not None:
50 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
52 if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
54 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
60 def get_session_auth():
61 loader = loading.get_plugin_loader('password')
62 creds = get_credentials()
63 auth = loader.load_from_options(**creds)
68 auth = get_session_auth()
70 cacert = os.environ['OS_CACERT']
72 return session.Session(auth=auth)
74 insecure = os.getenv('OS_INSECURE', '').lower() == 'true'
75 cacert = False if insecure else cacert
76 return session.Session(auth=auth, verify=cacert)
79 def get_endpoint(service_type, endpoint_type='publicURL'):
80 auth = get_session_auth()
81 return get_session().get_endpoint(auth=auth,
82 service_type=service_type,
83 endpoint_type=endpoint_type)
86 def get_heat_api_version():
87 api_version = os.getenv('HEAT_API_VERSION')
88 if api_version is not None:
89 log.info("HEAT_API_VERSION is set in env as '%s'", api_version)
91 return DEFAULT_HEAT_API_VERSION
94 def get_nova_api_version():
95 api_version = os.getenv('OS_COMPUTE_API_VERSION')
96 if api_version is not None:
97 log.info("NOVA_API_VERSION is set in env as '%s'", api_version)
99 return DEFAULT_NOVA_API_VERSION
102 def get_glance_api_version():
103 api_version = os.getenv('OS_IMAGE_API_VERSION')
104 if api_version is not None:
105 log.info("GLANCE_API_VERSION is set in env as '%s'", api_version)
107 return DEFAULT_GLANCE_API_VERSION