blob: 9949d3d96d5544bc306f7f45948d65b033f47489 [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
Peter Razumovsky2810bb52016-03-03 15:36:12 +030017from heat.common.i18n import _
Rabi Mishrae9dffe52016-02-05 14:36:58 +053018from heatclient import client as heat_client
Ethan Lynn07fac1f2016-05-10 23:09:51 +080019from keystoneauth1 import exceptions as kc_exceptions
20from keystoneauth1.identity.generic import password
21from keystoneauth1 import session
Rabi Mishrae9dffe52016-02-05 14:36:58 +053022from neutronclient.v2_0 import client as neutron_client
23from novaclient import client as nova_client
24from swiftclient import client as swift_client
Steve Baker450aa7f2014-08-25 10:37:27 +120025
Steve Baker450aa7f2014-08-25 10:37:27 +120026
Rabi Mishra65493fb2016-01-29 22:23:21 +053027class KeystoneWrapperClient(object):
28 """Wrapper object for keystone client
29
Rabi Mishrae9dffe52016-02-05 14:36:58 +053030 This wraps keystone client, so we can encpasulate certain
Rabi Mishra65493fb2016-01-29 22:23:21 +053031 added properties like auth_token, project_id etc.
32 """
33 def __init__(self, auth_plugin, verify=True):
34 self.auth_plugin = auth_plugin
35 self.session = session.Session(
36 auth=auth_plugin,
37 verify=verify)
38
39 @property
40 def auth_token(self):
41 return self.auth_plugin.get_token(self.session)
42
43 @property
44 def auth_ref(self):
45 return self.auth_plugin.get_access(self.session)
46
47 @property
48 def project_id(self):
49 return self.auth_plugin.get_project_id(self.session)
50
51 def get_endpoint_url(self, service_type, region=None):
52 kwargs = {
53 'service_type': service_type,
Ethan Lynn07fac1f2016-05-10 23:09:51 +080054 'region_name': region}
Rabi Mishra65493fb2016-01-29 22:23:21 +053055 return self.auth_ref.service_catalog.url_for(**kwargs)
56
57
Steve Baker450aa7f2014-08-25 10:37:27 +120058class ClientManager(object):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030059 """Provides access to the official python clients for calling various APIs.
60
Steve Baker450aa7f2014-08-25 10:37:27 +120061 Manager that provides access to the official python clients for
62 calling various OpenStack APIs.
63 """
64
Rabi Mishraa30ac122015-09-29 11:47:52 +053065 CINDERCLIENT_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120066 HEATCLIENT_VERSION = '1'
huangtianhua15ad5322016-06-02 14:39:13 +080067 NOVA_API_VERSION = '2.1'
Angus Salkeld406bbd52015-05-13 14:24:04 +100068 CEILOMETER_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120069
Steve Bakerb752e912016-08-01 22:05:37 +000070 def __init__(self, conf, admin_credentials=False):
Steve Baker450aa7f2014-08-25 10:37:27 +120071 self.conf = conf
Steve Bakerb752e912016-08-01 22:05:37 +000072 self.admin_credentials = admin_credentials
73
Peter Razumovsky2810bb52016-03-03 15:36:12 +030074 if self.conf.auth_url.find('/v'):
Peter Razumovsky2810bb52016-03-03 15:36:12 +030075 self.auth_version = self.conf.auth_url.split('/v')[1]
76 else:
77 raise ValueError(_('Incorrectly specified auth_url config: no '
78 'version found.'))
tyagi39aa11a2016-03-07 04:47:00 -080079 self.insecure = self.conf.disable_ssl_certificate_validation
80 self.ca_file = self.conf.ca_file
Rabi Mishra17f41de2016-05-03 11:19:41 +053081
Steve Baker450aa7f2014-08-25 10:37:27 +120082 self.identity_client = self._get_identity_client()
83 self.orchestration_client = self._get_orchestration_client()
84 self.compute_client = self._get_compute_client()
85 self.network_client = self._get_network_client()
86 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100087 self.object_client = self._get_object_client()
Angus Salkeld406bbd52015-05-13 14:24:04 +100088 self.metering_client = self._get_metering_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120089
Steve Bakerb752e912016-08-01 22:05:37 +000090 def _username(self):
91 if self.admin_credentials:
92 return self.conf.admin_username
93 return self.conf.username
94
95 def _password(self):
96 if self.admin_credentials:
97 return self.conf.admin_password
98 return self.conf.password
99
100 def _tenant_name(self):
101 if self.admin_credentials:
102 return self.conf.admin_tenant_name
103 return self.conf.tenant_name
104
Steve Baker450aa7f2014-08-25 10:37:27 +1200105 def _get_orchestration_client(self):
Angus Salkeld70c2f282014-11-21 08:51:41 +1000106 endpoint = os.environ.get('HEAT_URL')
107 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
108 token = None
109 else:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530110 token = self.identity_client.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +1200111 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +1000112 if endpoint is None:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530113 endpoint = self.identity_client.get_endpoint_url(
114 'orchestration', self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530115 except kc_exceptions.EndpointNotFound:
Steve Baker450aa7f2014-08-25 10:37:27 +1200116 return None
117 else:
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530118 return heat_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200119 self.HEATCLIENT_VERSION,
120 endpoint,
121 token=token,
Steve Bakerb752e912016-08-01 22:05:37 +0000122 username=self._username(),
123 password=self._password())
Steve Baker450aa7f2014-08-25 10:37:27 +1200124
125 def _get_identity_client(self):
rabi133ee5f2016-12-01 09:54:37 +0530126 user_domain_id = self.conf.user_domain_id
127 project_domain_id = self.conf.project_domain_id
Rabi Mishra1d538742016-03-21 21:09:20 +0530128 user_domain_name = self.conf.user_domain_name
129 project_domain_name = self.conf.project_domain_name
Rabi Mishra65493fb2016-01-29 22:23:21 +0530130 kwargs = {
Steve Bakerb752e912016-08-01 22:05:37 +0000131 'username': self._username(),
132 'password': self._password(),
133 'tenant_name': self._tenant_name(),
Rabi Mishra65493fb2016-01-29 22:23:21 +0530134 'auth_url': self.conf.auth_url
135 }
136 # keystone v2 can't ignore domain details
137 if self.auth_version == '3':
138 kwargs.update({
rabi133ee5f2016-12-01 09:54:37 +0530139 'user_domain_id': user_domain_id,
140 'project_domain_id': project_domain_id,
Rabi Mishra1d538742016-03-21 21:09:20 +0530141 'user_domain_name': user_domain_name,
142 'project_domain_name': project_domain_name})
Rabi Mishra65493fb2016-01-29 22:23:21 +0530143 auth = password.Password(**kwargs)
tyagi39aa11a2016-03-07 04:47:00 -0800144 if self.insecure:
145 verify_cert = False
146 else:
147 verify_cert = self.ca_file or True
148
149 return KeystoneWrapperClient(auth, verify_cert)
Steve Baker450aa7f2014-08-25 10:37:27 +1200150
151 def _get_compute_client(self):
152
Steve Baker450aa7f2014-08-25 10:37:27 +1200153 region = self.conf.region
Steve Baker450aa7f2014-08-25 10:37:27 +1200154 # Create our default Nova client to use in testing
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530155 return nova_client.Client(
huangtianhua15ad5322016-06-02 14:39:13 +0800156 self.NOVA_API_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530157 session=self.identity_client.session,
Steve Baker450aa7f2014-08-25 10:37:27 +1200158 service_type='compute',
159 endpoint_type='publicURL',
160 region_name=region,
rabi530626f2017-01-20 07:53:38 +0530161 os_cache=False,
tyagi39aa11a2016-03-07 04:47:00 -0800162 insecure=self.insecure,
163 cacert=self.ca_file,
Steve Baker450aa7f2014-08-25 10:37:27 +1200164 http_log_debug=True)
165
166 def _get_network_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200167
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530168 return neutron_client.Client(
Rabi Mishra17f41de2016-05-03 11:19:41 +0530169 session=self.identity_client.session,
Steve Baker450aa7f2014-08-25 10:37:27 +1200170 endpoint_type='publicURL',
tyagi39aa11a2016-03-07 04:47:00 -0800171 insecure=self.insecure,
172 ca_cert=self.ca_file)
Steve Baker450aa7f2014-08-25 10:37:27 +1200173
174 def _get_volume_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200175 region = self.conf.region
176 endpoint_type = 'publicURL'
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530177 return cinder_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200178 self.CINDERCLIENT_VERSION,
Rabi Mishra17f41de2016-05-03 11:19:41 +0530179 session=self.identity_client.session,
Steve Baker450aa7f2014-08-25 10:37:27 +1200180 region_name=region,
181 endpoint_type=endpoint_type,
tyagi39aa11a2016-03-07 04:47:00 -0800182 insecure=self.insecure,
183 cacert=self.ca_file,
Steve Baker450aa7f2014-08-25 10:37:27 +1200184 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000185
186 def _get_object_client(self):
Rabi Mishra17f41de2016-05-03 11:19:41 +0530187 # swiftclient does not support keystone sessions yet
Angus Salkeld4408da32015-02-03 18:53:30 +1000188 args = {
Rabi Mishra65493fb2016-01-29 22:23:21 +0530189 'auth_version': self.auth_version,
Steve Bakerb752e912016-08-01 22:05:37 +0000190 'tenant_name': self._tenant_name(),
191 'user': self._username(),
Angus Salkeld4408da32015-02-03 18:53:30 +1000192 'key': self.conf.password,
193 'authurl': self.conf.auth_url,
194 'os_options': {'endpoint_type': 'publicURL'},
tyagi39aa11a2016-03-07 04:47:00 -0800195 'insecure': self.insecure,
196 'cacert': self.ca_file,
Angus Salkeld4408da32015-02-03 18:53:30 +1000197 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530198 return swift_client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000199
200 def _get_metering_client(self):
Steve Baker5e4d5f42015-05-26 13:28:28 +1200201 try:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530202 endpoint = self.identity_client.get_endpoint_url('metering',
203 self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530204 except kc_exceptions.EndpointNotFound:
Steve Baker5e4d5f42015-05-26 13:28:28 +1200205 return None
206 else:
207 args = {
Rabi Mishra17f41de2016-05-03 11:19:41 +0530208 'session': self.identity_client.session,
tyagi39aa11a2016-03-07 04:47:00 -0800209 'insecure': self.insecure,
210 'cacert': self.ca_file,
Steve Baker5e4d5f42015-05-26 13:28:28 +1200211 'region_name': self.conf.region,
212 'endpoint_type': 'publicURL',
213 'service_type': 'metering',
214 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530215 return ceilometer_client.Client(self.CEILOMETER_VERSION,
216 endpoint, **args)