blob: a9bd981dd96666a6755d92578130090d62b9fb7c [file] [log] [blame]
Steve Baker450aa7f2014-08-25 10:37:27 +12001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Angus Salkeld70c2f282014-11-21 08:51:41 +100013import os
14
Rabi Mishrae9dffe52016-02-05 14:36:58 +053015from cinderclient import client as cinder_client
Pavlo Shchelokovskyy19b0cb92020-01-23 20:07:00 +020016try:
17 from gnocchiclient import client as gnocchi_client
18except ImportError:
19 # catches ujson vs Python GCC compat problem,
20 # see https://github.com/esnme/ultrajson/issues/346
21 gnocchi_client = None
Rabi Mishrae9dffe52016-02-05 14:36:58 +053022from heatclient import client as heat_client
Ethan Lynn07fac1f2016-05-10 23:09:51 +080023from keystoneauth1 import exceptions as kc_exceptions
24from keystoneauth1.identity.generic import password
25from keystoneauth1 import session
Rabi Mishrae9dffe52016-02-05 14:36:58 +053026from neutronclient.v2_0 import client as neutron_client
27from novaclient import client as nova_client
28from swiftclient import client as swift_client
Steve Baker450aa7f2014-08-25 10:37:27 +120029
Steve Baker450aa7f2014-08-25 10:37:27 +120030
Rabi Mishra65493fb2016-01-29 22:23:21 +053031class KeystoneWrapperClient(object):
32 """Wrapper object for keystone client
33
Rabi Mishrae9dffe52016-02-05 14:36:58 +053034 This wraps keystone client, so we can encpasulate certain
Rabi Mishra65493fb2016-01-29 22:23:21 +053035 added properties like auth_token, project_id etc.
36 """
37 def __init__(self, auth_plugin, verify=True):
38 self.auth_plugin = auth_plugin
39 self.session = session.Session(
40 auth=auth_plugin,
41 verify=verify)
42
43 @property
44 def auth_token(self):
45 return self.auth_plugin.get_token(self.session)
46
47 @property
48 def auth_ref(self):
49 return self.auth_plugin.get_access(self.session)
50
51 @property
52 def project_id(self):
53 return self.auth_plugin.get_project_id(self.session)
54
Colleen Murphy30b1fd62017-12-29 12:43:53 +010055 def get_endpoint_url(self, service_type, region=None,
56 endpoint_type='public'):
Rabi Mishra65493fb2016-01-29 22:23:21 +053057 kwargs = {
58 'service_type': service_type,
Colleen Murphy30b1fd62017-12-29 12:43:53 +010059 'region_name': region,
60 'interface': endpoint_type}
Rabi Mishra65493fb2016-01-29 22:23:21 +053061 return self.auth_ref.service_catalog.url_for(**kwargs)
62
63
Steve Baker450aa7f2014-08-25 10:37:27 +120064class ClientManager(object):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030065 """Provides access to the official python clients for calling various APIs.
66
Steve Baker450aa7f2014-08-25 10:37:27 +120067 Manager that provides access to the official python clients for
68 calling various OpenStack APIs.
69 """
70
Rabi Mishraa30ac122015-09-29 11:47:52 +053071 CINDERCLIENT_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120072 HEATCLIENT_VERSION = '1'
huangtianhua15ad5322016-06-02 14:39:13 +080073 NOVA_API_VERSION = '2.1'
rabid69f0312017-10-26 14:52:52 +053074 GNOCCHI_VERSION = '1'
Steve Baker450aa7f2014-08-25 10:37:27 +120075
Steve Bakerb752e912016-08-01 22:05:37 +000076 def __init__(self, conf, admin_credentials=False):
Steve Baker450aa7f2014-08-25 10:37:27 +120077 self.conf = conf
Steve Bakerb752e912016-08-01 22:05:37 +000078 self.admin_credentials = admin_credentials
79
rabi8fcf1922017-04-19 09:21:54 +053080 self.auth_version = self.conf.auth_version
81 if not self.auth_version:
rabid2916d02017-09-22 18:19:24 +053082 self.auth_version = self.conf.auth_url.split('/v')[1]
tyagi39aa11a2016-03-07 04:47:00 -080083 self.insecure = self.conf.disable_ssl_certificate_validation
84 self.ca_file = self.conf.ca_file
Rabi Mishra17f41de2016-05-03 11:19:41 +053085
Steve Baker450aa7f2014-08-25 10:37:27 +120086 self.identity_client = self._get_identity_client()
87 self.orchestration_client = self._get_orchestration_client()
88 self.compute_client = self._get_compute_client()
89 self.network_client = self._get_network_client()
90 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100091 self.object_client = self._get_object_client()
rabid69f0312017-10-26 14:52:52 +053092 self.metric_client = self._get_metric_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120093
Steve Bakerb752e912016-08-01 22:05:37 +000094 def _username(self):
95 if self.admin_credentials:
96 return self.conf.admin_username
97 return self.conf.username
98
99 def _password(self):
100 if self.admin_credentials:
101 return self.conf.admin_password
102 return self.conf.password
103
rabibe38c302017-04-11 09:54:07 +0530104 def _project_name(self):
Steve Bakerb752e912016-08-01 22:05:37 +0000105 if self.admin_credentials:
rabibe38c302017-04-11 09:54:07 +0530106 return self.conf.admin_project_name
107 return self.conf.project_name
Steve Bakerb752e912016-08-01 22:05:37 +0000108
Steve Baker450aa7f2014-08-25 10:37:27 +1200109 def _get_orchestration_client(self):
Angus Salkeld70c2f282014-11-21 08:51:41 +1000110 endpoint = os.environ.get('HEAT_URL')
111 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
112 token = None
113 else:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530114 token = self.identity_client.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +1200115 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +1000116 if endpoint is None:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530117 endpoint = self.identity_client.get_endpoint_url(
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100118 'orchestration', region=self.conf.region,
119 endpoint_type=self.conf.endpoint_type)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530120 except kc_exceptions.EndpointNotFound:
Steve Baker450aa7f2014-08-25 10:37:27 +1200121 return None
122 else:
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530123 return heat_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200124 self.HEATCLIENT_VERSION,
125 endpoint,
126 token=token,
Steve Bakerb752e912016-08-01 22:05:37 +0000127 username=self._username(),
128 password=self._password())
Steve Baker450aa7f2014-08-25 10:37:27 +1200129
130 def _get_identity_client(self):
rabi133ee5f2016-12-01 09:54:37 +0530131 user_domain_id = self.conf.user_domain_id
132 project_domain_id = self.conf.project_domain_id
Rabi Mishra1d538742016-03-21 21:09:20 +0530133 user_domain_name = self.conf.user_domain_name
134 project_domain_name = self.conf.project_domain_name
Rabi Mishra65493fb2016-01-29 22:23:21 +0530135 kwargs = {
Steve Bakerb752e912016-08-01 22:05:37 +0000136 'username': self._username(),
137 'password': self._password(),
rabibe38c302017-04-11 09:54:07 +0530138 'project_name': self._project_name(),
Rabi Mishra65493fb2016-01-29 22:23:21 +0530139 'auth_url': self.conf.auth_url
140 }
141 # keystone v2 can't ignore domain details
142 if self.auth_version == '3':
143 kwargs.update({
rabi133ee5f2016-12-01 09:54:37 +0530144 'user_domain_id': user_domain_id,
145 'project_domain_id': project_domain_id,
Rabi Mishra1d538742016-03-21 21:09:20 +0530146 'user_domain_name': user_domain_name,
147 'project_domain_name': project_domain_name})
Rabi Mishra65493fb2016-01-29 22:23:21 +0530148 auth = password.Password(**kwargs)
tyagi39aa11a2016-03-07 04:47:00 -0800149 if self.insecure:
150 verify_cert = False
151 else:
152 verify_cert = self.ca_file or True
153
154 return KeystoneWrapperClient(auth, verify_cert)
Steve Baker450aa7f2014-08-25 10:37:27 +1200155
156 def _get_compute_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200157 # Create our default Nova client to use in testing
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530158 return nova_client.Client(
huangtianhua15ad5322016-06-02 14:39:13 +0800159 self.NOVA_API_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530160 session=self.identity_client.session,
Steve Baker450aa7f2014-08-25 10:37:27 +1200161 service_type='compute',
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100162 endpoint_type=self.conf.endpoint_type,
rabi613f8152017-02-14 18:59:15 +0530163 region_name=self.conf.region,
rabi530626f2017-01-20 07:53:38 +0530164 os_cache=False,
Steve Baker450aa7f2014-08-25 10:37:27 +1200165 http_log_debug=True)
166
167 def _get_network_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200168
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530169 return neutron_client.Client(
Rabi Mishra17f41de2016-05-03 11:19:41 +0530170 session=self.identity_client.session,
rabi613f8152017-02-14 18:59:15 +0530171 service_type='network',
172 region_name=self.conf.region,
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100173 endpoint_type=self.conf.endpoint_type)
Steve Baker450aa7f2014-08-25 10:37:27 +1200174
175 def _get_volume_client(self):
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530176 return cinder_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200177 self.CINDERCLIENT_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530178 session=self.identity_client.session,
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100179 endpoint_type=self.conf.endpoint_type,
rabi613f8152017-02-14 18:59:15 +0530180 region_name=self.conf.region,
Steve Baker450aa7f2014-08-25 10:37:27 +1200181 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000182
183 def _get_object_client(self):
Angus Salkeld4408da32015-02-03 18:53:30 +1000184 args = {
Rabi Mishra65493fb2016-01-29 22:23:21 +0530185 'auth_version': self.auth_version,
rabi613f8152017-02-14 18:59:15 +0530186 'session': self.identity_client.session,
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100187 'os_options': {'endpoint_type': self.conf.endpoint_type,
rabi613f8152017-02-14 18:59:15 +0530188 'region_name': self.conf.region,
189 'service_type': 'object-store'},
Angus Salkeld4408da32015-02-03 18:53:30 +1000190 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530191 return swift_client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000192
rabid69f0312017-10-26 14:52:52 +0530193 def _get_metric_client(self):
Pavlo Shchelokovskyy19b0cb92020-01-23 20:07:00 +0200194 if gnocchi_client is None:
195 return None
rabid69f0312017-10-26 14:52:52 +0530196
Colleen Murphy30b1fd62017-12-29 12:43:53 +0100197 adapter_options = {'interface': self.conf.endpoint_type,
rabid69f0312017-10-26 14:52:52 +0530198 'region_name': self.conf.region}
199 args = {
200 'session': self.identity_client.session,
201 'adapter_options': adapter_options
202 }
Vasyl Saienko4dd2fef2020-01-25 13:54:12 +0200203 try:
204 return gnocchi_client.Client(version=self.GNOCCHI_VERSION,
205 **args)
206 except ImportError:
207 # catches ujson vs Python GCC compat problem,
208 # see https://github.com/esnme/ultrajson/issues/346
209 return None