ab0dadf5e0c51b315f697745493f09713dc00d38
[bottlenecks.git] / utils / infra_setup / heat / common.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 DEFAULT_NOVA_API_VERSION = '2'
20 DEFAULT_GLANCE_API_VERSION = '2'
21
22
23 def get_credentials():
24     """Returns a creds dictionary filled with parsed from env"""
25     creds = {}
26
27     keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
28
29     if keystone_api_version is None or keystone_api_version == '2':
30         keystone_v3 = False
31         tenant_env = 'OS_TENANT_NAME'
32         tenant = 'tenant_name'
33     else:
34         keystone_v3 = True
35         tenant_env = 'OS_PROJECT_NAME'
36         tenant = 'project_name'
37
38     # The most common way to pass these info to the script is to do it
39     # through environment variables.
40     creds.update({
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)
45     })
46
47     if keystone_v3:
48         if os.getenv('OS_USER_DOMAIN_NAME') is not None:
49             creds.update({
50                 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
51             })
52         if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
53             creds.update({
54                 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
55             })
56
57     cacert = os.environ.get("OS_CACERT")
58
59     if cacert is not None:
60         # each openstack client uses differnt kwargs for this
61         creds.update({"cacert": cacert,
62                       "ca_cert": cacert,
63                       "https_ca_cert": cacert,
64                       "https_cacert": cacert,
65                       "ca_file": cacert})
66         creds.update({"insecure": "True", "https_insecure": "True"})
67         if not os.path.isfile(cacert):
68             log.info("WARNING: The 'OS_CACERT' environment variable is set\
69                       to %s but the file does not exist." % cacert)
70
71     return creds
72
73
74 def get_session_auth():
75     loader = loading.get_plugin_loader('password')
76     creds = get_credentials()
77     auth = loader.load_from_options(**creds)
78     return auth
79
80
81 def get_session():
82     auth = get_session_auth()
83     return session.Session(auth=auth)
84
85
86 def get_endpoint(service_type, endpoint_type='publicURL'):
87     auth = get_session_auth()
88     return get_session().get_endpoint(auth=auth,
89                                       service_type=service_type,
90                                       endpoint_type=endpoint_type)
91
92
93 def get_heat_api_version():
94     api_version = os.getenv('HEAT_API_VERSION')
95     if api_version is not None:
96         log.info("HEAT_API_VERSION is set in env as '%s'", api_version)
97         return api_version
98     return DEFAULT_HEAT_API_VERSION
99
100
101 def get_nova_api_version():
102     api_version = os.getenv('OS_COMPUTE_API_VERSION')
103     if api_version is not None:
104         log.info("NOVA_API_VERSION is set in env as '%s'", api_version)
105         return api_version
106     return DEFAULT_NOVA_API_VERSION
107
108
109 def get_glance_api_version():
110     api_version = os.getenv('OS_IMAGE_API_VERSION')
111     if api_version is not None:
112         log.info("GLANCE_API_VERSION is set in env as '%s'", api_version)
113         return api_version
114     return DEFAULT_GLANCE_API_VERSION