blob: cff3bf261ff0c04ba686c73615ce68701c4b9acd [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
Rabi Mishra65493fb2016-01-29 22:23:21 +053018from keystoneclient.auth.identity.generic import password
Rabi Mishrae9dffe52016-02-05 14:36:58 +053019from keystoneclient import exceptions as kc_exceptions
Rabi Mishra65493fb2016-01-29 22:23:21 +053020from keystoneclient 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,
53 'endpoint_type': 'publicURL'}
54 if region:
55 kwargs.update({'attr': 'region',
56 'filter_value': region})
57 return self.auth_ref.service_catalog.url_for(**kwargs)
58
59
Steve Baker450aa7f2014-08-25 10:37:27 +120060class ClientManager(object):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030061 """Provides access to the official python clients for calling various APIs.
62
Steve Baker450aa7f2014-08-25 10:37:27 +120063 Manager that provides access to the official python clients for
64 calling various OpenStack APIs.
65 """
66
Rabi Mishraa30ac122015-09-29 11:47:52 +053067 CINDERCLIENT_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120068 HEATCLIENT_VERSION = '1'
69 NOVACLIENT_VERSION = '2'
Angus Salkeld406bbd52015-05-13 14:24:04 +100070 CEILOMETER_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120071
72 def __init__(self, conf):
73 self.conf = conf
Rabi Mishra65493fb2016-01-29 22:23:21 +053074 self.v2_auth_url = self.conf.auth_url.replace('/v3', '/v2.0')
75 self.auth_version = self.conf.auth_url.split('/v')[1]
Steve Baker450aa7f2014-08-25 10:37:27 +120076 self.identity_client = self._get_identity_client()
77 self.orchestration_client = self._get_orchestration_client()
78 self.compute_client = self._get_compute_client()
79 self.network_client = self._get_network_client()
80 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100081 self.object_client = self._get_object_client()
Angus Salkeld406bbd52015-05-13 14:24:04 +100082 self.metering_client = self._get_metering_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120083
84 def _get_orchestration_client(self):
Angus Salkeld70c2f282014-11-21 08:51:41 +100085 endpoint = os.environ.get('HEAT_URL')
86 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
87 token = None
88 else:
Rabi Mishra65493fb2016-01-29 22:23:21 +053089 token = self.identity_client.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +120090 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +100091 if endpoint is None:
Rabi Mishra65493fb2016-01-29 22:23:21 +053092 endpoint = self.identity_client.get_endpoint_url(
93 'orchestration', self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +053094 except kc_exceptions.EndpointNotFound:
Steve Baker450aa7f2014-08-25 10:37:27 +120095 return None
96 else:
Rabi Mishrae9dffe52016-02-05 14:36:58 +053097 return heat_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +120098 self.HEATCLIENT_VERSION,
99 endpoint,
100 token=token,
101 username=self.conf.username,
102 password=self.conf.password)
103
104 def _get_identity_client(self):
Rabi Mishra65493fb2016-01-29 22:23:21 +0530105 domain = self.conf.domain_name
106 kwargs = {
107 'username': self.conf.username,
108 'password': self.conf.password,
109 'tenant_name': self.conf.tenant_name,
110 'auth_url': self.conf.auth_url
111 }
112 # keystone v2 can't ignore domain details
113 if self.auth_version == '3':
114 kwargs.update({
115 'project_domain_name': domain,
116 'user_domain_name': domain})
117 auth = password.Password(**kwargs)
118 return KeystoneWrapperClient(
119 auth,
120 not self.conf.disable_ssl_certificate_validation)
Steve Baker450aa7f2014-08-25 10:37:27 +1200121
122 def _get_compute_client(self):
123
124 dscv = self.conf.disable_ssl_certificate_validation
125 region = self.conf.region
126
127 client_args = (
128 self.conf.username,
129 self.conf.password,
130 self.conf.tenant_name,
Rabi Mishra65493fb2016-01-29 22:23:21 +0530131 # novaclient can not use v3 url
132 self.v2_auth_url
Steve Baker450aa7f2014-08-25 10:37:27 +1200133 )
134
135 # Create our default Nova client to use in testing
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530136 return nova_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200137 self.NOVACLIENT_VERSION,
138 *client_args,
139 service_type='compute',
140 endpoint_type='publicURL',
141 region_name=region,
142 no_cache=True,
143 insecure=dscv,
144 http_log_debug=True)
145
146 def _get_network_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200147 dscv = self.conf.disable_ssl_certificate_validation
148
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530149 return neutron_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200150 username=self.conf.username,
151 password=self.conf.password,
152 tenant_name=self.conf.tenant_name,
153 endpoint_type='publicURL',
Rabi Mishra65493fb2016-01-29 22:23:21 +0530154 # neutronclient can not use v3 url
155 auth_url=self.v2_auth_url,
Steve Baker450aa7f2014-08-25 10:37:27 +1200156 insecure=dscv)
157
158 def _get_volume_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +1200159 region = self.conf.region
160 endpoint_type = 'publicURL'
161 dscv = self.conf.disable_ssl_certificate_validation
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530162 return cinder_client.Client(
Steve Baker450aa7f2014-08-25 10:37:27 +1200163 self.CINDERCLIENT_VERSION,
164 self.conf.username,
165 self.conf.password,
166 self.conf.tenant_name,
Rabi Mishra65493fb2016-01-29 22:23:21 +0530167 # cinderclient can not use v3 url
168 self.v2_auth_url,
Steve Baker450aa7f2014-08-25 10:37:27 +1200169 region_name=region,
170 endpoint_type=endpoint_type,
171 insecure=dscv,
172 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000173
174 def _get_object_client(self):
175 dscv = self.conf.disable_ssl_certificate_validation
176 args = {
Rabi Mishra65493fb2016-01-29 22:23:21 +0530177 'auth_version': self.auth_version,
Angus Salkeld4408da32015-02-03 18:53:30 +1000178 'tenant_name': self.conf.tenant_name,
179 'user': self.conf.username,
180 'key': self.conf.password,
181 'authurl': self.conf.auth_url,
182 'os_options': {'endpoint_type': 'publicURL'},
183 'insecure': dscv,
184 }
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530185 return swift_client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000186
187 def _get_metering_client(self):
188 dscv = self.conf.disable_ssl_certificate_validation
Rabi Mishra65493fb2016-01-29 22:23:21 +0530189 domain = self.conf.domain_name
Steve Baker5e4d5f42015-05-26 13:28:28 +1200190 try:
Rabi Mishra65493fb2016-01-29 22:23:21 +0530191 endpoint = self.identity_client.get_endpoint_url('metering',
192 self.conf.region)
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530193 except kc_exceptions.EndpointNotFound:
Steve Baker5e4d5f42015-05-26 13:28:28 +1200194 return None
195 else:
196 args = {
197 'username': self.conf.username,
198 'password': self.conf.password,
199 'tenant_name': self.conf.tenant_name,
200 'auth_url': self.conf.auth_url,
201 'insecure': dscv,
202 'region_name': self.conf.region,
203 'endpoint_type': 'publicURL',
204 'service_type': 'metering',
205 }
Rabi Mishra65493fb2016-01-29 22:23:21 +0530206 # ceilometerclient can't ignore domain details for
207 # v2 auth_url
208 if self.auth_version == '3':
209 args.update(
210 {'user_domain_name': domain,
211 'project_domain_name': domain})
Angus Salkeld406bbd52015-05-13 14:24:04 +1000212
Rabi Mishrae9dffe52016-02-05 14:36:58 +0530213 return ceilometer_client.Client(self.CEILOMETER_VERSION,
214 endpoint, **args)