blob: ffd5b78c0021b51ad493ab01d456c1a0b8ce0f3b [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 ceilometerclient import client as ceilometer_client
16from cinderclient import client as cinder_client
17from heatclient import client as heat_client
Ethan Lynn07fac1f2016-05-10 23:09:51 +080018from keystoneauth1 import exceptions as kc_exceptions
19from keystoneauth1.identity.generic import password
20from keystoneauth1 import session
Rabi Mishrae9dffe52016-02-05 14:36:58 +053021from neutronclient.v2_0 import client as neutron_client
22from novaclient import client as nova_client
23from swiftclient import client as swift_client
Steve Baker450aa7f2014-08-25 10:37:27 +120024
Steve Baker450aa7f2014-08-25 10:37:27 +120025
Rabi Mishra65493fb2016-01-29 22:23:21 +053026class KeystoneWrapperClient(object):
27 """Wrapper object for keystone client
28
Rabi Mishrae9dffe52016-02-05 14:36:58 +053029 This wraps keystone client, so we can encpasulate certain
Rabi Mishra65493fb2016-01-29 22:23:21 +053030 added properties like auth_token, project_id etc.
31 """
32 def __init__(self, auth_plugin, verify=True):
33 self.auth_plugin = auth_plugin
34 self.session = session.Session(
35 auth=auth_plugin,
36 verify=verify)
37
38 @property
39 def auth_token(self):
40 return self.auth_plugin.get_token(self.session)
41
42 @property
43 def auth_ref(self):
44 return self.auth_plugin.get_access(self.session)
45
46 @property
47 def project_id(self):
48 return self.auth_plugin.get_project_id(self.session)
49
50 def get_endpoint_url(self, service_type, region=None):
51 kwargs = {
52 'service_type': service_type,
Ethan Lynn07fac1f2016-05-10 23:09:51 +080053 'region_name': region}
Rabi Mishra65493fb2016-01-29 22:23:21 +053054 return self.auth_ref.service_catalog.url_for(**kwargs)
55
56
Steve Baker450aa7f2014-08-25 10:37:27 +120057class ClientManager(object):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030058 """Provides access to the official python clients for calling various APIs.
59
Steve Baker450aa7f2014-08-25 10:37:27 +120060 Manager that provides access to the official python clients for
61 calling various OpenStack APIs.
62 """
63
Rabi Mishraa30ac122015-09-29 11:47:52 +053064 CINDERCLIENT_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120065 HEATCLIENT_VERSION = '1'
huangtianhua15ad5322016-06-02 14:39:13 +080066 NOVA_API_VERSION = '2.1'
Angus Salkeld406bbd52015-05-13 14:24:04 +100067 CEILOMETER_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120068
Steve Bakerb752e912016-08-01 22:05:37 +000069 def __init__(self, conf, admin_credentials=False):
Steve Baker450aa7f2014-08-25 10:37:27 +120070 self.conf = conf
Steve Bakerb752e912016-08-01 22:05:37 +000071 self.admin_credentials = admin_credentials
72
rabi8fcf1922017-04-19 09:21:54 +053073 self.auth_version = self.conf.auth_version
74 if not self.auth_version:
rabid2916d02017-09-22 18:19:24 +053075 self.auth_version = self.conf.auth_url.split('/v')[1]
tyagi39aa11a2016-03-07 04:47:00 -080076 self.insecure = self.conf.disable_ssl_certificate_validation
77 self.ca_file = self.conf.ca_file
Rabi Mishra17f41de2016-05-03 11:19:41 +053078
Steve Baker450aa7f2014-08-25 10:37:27 +120079 self.identity_client = self._get_identity_client()
80 self.orchestration_client = self._get_orchestration_client()
81 self.compute_client = self._get_compute_client()
82 self.network_client = self._get_network_client()
83 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100084 self.object_client = self._get_object_client()
Angus Salkeld406bbd52015-05-13 14:24:04 +100085 self.metering_client = self._get_metering_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120086
Steve Bakerb752e912016-08-01 22:05:37 +000087 def _username(self):
88 if self.admin_credentials:
89 return self.conf.admin_username
90 return self.conf.username
91
92 def _password(self):
93 if self.admin_credentials:
94 return self.conf.admin_password
95 return self.conf.password
96
rabibe38c302017-04-11 09:54:07 +053097 def _project_name(self):
Steve Bakerb752e912016-08-01 22:05:37 +000098 if self.admin_credentials:
rabibe38c302017-04-11 09:54:07 +053099 return self.conf.admin_project_name
100 return self.conf.project_name
Steve Bakerb752e912016-08-01 22:05:37 +0000101
Steve Baker450aa7f2014-08-25 10:37:27 +1200102 def _get_orchestration_client(self):
Angus Salkeld70c2f282014-11-21 08:51:41 +1000103 endpoint = os.environ.get('HEAT_URL')
104 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
105 token = None
106 else:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530107 token = self.identity_client.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +1200108 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +1000109 if endpoint is None:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530110 endpoint = self.identity_client.get_endpoint_url(
111 'orchestration', self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530112 except kc_exceptions.EndpointNotFound:
Steve Baker450aa7f2014-08-25 10:37:27 +1200113 return None
114 else:
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530115 return heat_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200116 self.HEATCLIENT_VERSION,
117 endpoint,
118 token=token,
Steve Bakerb752e912016-08-01 22:05:37 +0000119 username=self._username(),
120 password=self._password())
Steve Baker450aa7f2014-08-25 10:37:27 +1200121
122 def _get_identity_client(self):
rabi133ee5f2016-12-01 09:54:37 +0530123 user_domain_id = self.conf.user_domain_id
124 project_domain_id = self.conf.project_domain_id
Rabi Mishra1d538742016-03-21 21:09:20 +0530125 user_domain_name = self.conf.user_domain_name
126 project_domain_name = self.conf.project_domain_name
Rabi Mishra65493fb2016-01-29 22:23:21 +0530127 kwargs = {
Steve Bakerb752e912016-08-01 22:05:37 +0000128 'username': self._username(),
129 'password': self._password(),
rabibe38c302017-04-11 09:54:07 +0530130 'project_name': self._project_name(),
Rabi Mishra65493fb2016-01-29 22:23:21 +0530131 'auth_url': self.conf.auth_url
132 }
133 # keystone v2 can't ignore domain details
134 if self.auth_version == '3':
135 kwargs.update({
rabi133ee5f2016-12-01 09:54:37 +0530136 'user_domain_id': user_domain_id,
137 'project_domain_id': project_domain_id,
Rabi Mishra1d538742016-03-21 21:09:20 +0530138 'user_domain_name': user_domain_name,
139 'project_domain_name': project_domain_name})
Rabi Mishra65493fb2016-01-29 22:23:21 +0530140 auth = password.Password(**kwargs)
tyagi39aa11a2016-03-07 04:47:00 -0800141 if self.insecure:
142 verify_cert = False
143 else:
144 verify_cert = self.ca_file or True
145
146 return KeystoneWrapperClient(auth, verify_cert)
Steve Baker450aa7f2014-08-25 10:37:27 +1200147
148 def _get_compute_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200149 # Create our default Nova client to use in testing
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530150 return nova_client.Client(
huangtianhua15ad5322016-06-02 14:39:13 +0800151 self.NOVA_API_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530152 session=self.identity_client.session,
Steve Baker450aa7f2014-08-25 10:37:27 +1200153 service_type='compute',
154 endpoint_type='publicURL',
rabi613f8152017-02-14 18:59:15 +0530155 region_name=self.conf.region,
rabi530626f2017-01-20 07:53:38 +0530156 os_cache=False,
Steve Baker450aa7f2014-08-25 10:37:27 +1200157 http_log_debug=True)
158
159 def _get_network_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200160
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530161 return neutron_client.Client(
Rabi Mishra17f41de2016-05-03 11:19:41 +0530162 session=self.identity_client.session,
rabi613f8152017-02-14 18:59:15 +0530163 service_type='network',
164 region_name=self.conf.region,
165 endpoint_type='publicURL')
Steve Baker450aa7f2014-08-25 10:37:27 +1200166
167 def _get_volume_client(self):
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530168 return cinder_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200169 self.CINDERCLIENT_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530170 session=self.identity_client.session,
rabi613f8152017-02-14 18:59:15 +0530171 endpoint_type='publicURL',
172 region_name=self.conf.region,
Steve Baker450aa7f2014-08-25 10:37:27 +1200173 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000174
175 def _get_object_client(self):
Angus Salkeld4408da32015-02-03 18:53:30 +1000176 args = {
Rabi Mishra65493fb2016-01-29 22:23:21 +0530177 'auth_version': self.auth_version,
rabi613f8152017-02-14 18:59:15 +0530178 'session': self.identity_client.session,
179 'os_options': {'endpoint_type': 'publicURL',
180 'region_name': self.conf.region,
181 'service_type': 'object-store'},
Angus Salkeld4408da32015-02-03 18:53:30 +1000182 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530183 return swift_client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000184
185 def _get_metering_client(self):
Steve Baker5e4d5f42015-05-26 13:28:28 +1200186 try:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530187 endpoint = self.identity_client.get_endpoint_url('metering',
188 self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530189 except kc_exceptions.EndpointNotFound:
Steve Baker5e4d5f42015-05-26 13:28:28 +1200190 return None
191 else:
192 args = {
Rabi Mishra17f41de2016-05-03 11:19:41 +0530193 'session': self.identity_client.session,
Steve Baker5e4d5f42015-05-26 13:28:28 +1200194 'region_name': self.conf.region,
195 'endpoint_type': 'publicURL',
196 'service_type': 'metering',
197 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530198 return ceilometer_client.Client(self.CEILOMETER_VERSION,
199 endpoint, **args)