blob: 9718d036d06867d09ff55bc04d773ef1c2cb43b0 [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
Angus Salkeld406bbd52015-05-13 14:24:04 +100015import ceilometerclient.client
Steve Baker450aa7f2014-08-25 10:37:27 +120016import cinderclient.client
17import heatclient.client
18import keystoneclient.exceptions
19import keystoneclient.v2_0.client
20import neutronclient.v2_0.client
21import novaclient.client
Angus Salkeld4408da32015-02-03 18:53:30 +100022import swiftclient
Steve Baker450aa7f2014-08-25 10:37:27 +120023
Steve Baker450aa7f2014-08-25 10:37:27 +120024
25class ClientManager(object):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030026 """Provides access to the official python clients for calling various APIs.
27
Steve Baker450aa7f2014-08-25 10:37:27 +120028 Manager that provides access to the official python clients for
29 calling various OpenStack APIs.
30 """
31
Rabi Mishraa30ac122015-09-29 11:47:52 +053032 CINDERCLIENT_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120033 HEATCLIENT_VERSION = '1'
34 NOVACLIENT_VERSION = '2'
Angus Salkeld406bbd52015-05-13 14:24:04 +100035 CEILOMETER_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120036
37 def __init__(self, conf):
38 self.conf = conf
39 self.identity_client = self._get_identity_client()
40 self.orchestration_client = self._get_orchestration_client()
41 self.compute_client = self._get_compute_client()
42 self.network_client = self._get_network_client()
43 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100044 self.object_client = self._get_object_client()
Angus Salkeld406bbd52015-05-13 14:24:04 +100045 self.metering_client = self._get_metering_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120046
47 def _get_orchestration_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +120048 region = self.conf.region
Angus Salkeld70c2f282014-11-21 08:51:41 +100049 endpoint = os.environ.get('HEAT_URL')
50 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
51 token = None
52 else:
53 keystone = self._get_identity_client()
54 token = keystone.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +120055 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +100056 if endpoint is None:
57 endpoint = keystone.service_catalog.url_for(
58 attr='region',
59 filter_value=region,
60 service_type='orchestration',
61 endpoint_type='publicURL')
Steve Baker450aa7f2014-08-25 10:37:27 +120062 except keystoneclient.exceptions.EndpointNotFound:
63 return None
64 else:
65 return heatclient.client.Client(
66 self.HEATCLIENT_VERSION,
67 endpoint,
68 token=token,
69 username=self.conf.username,
70 password=self.conf.password)
71
72 def _get_identity_client(self):
73 return keystoneclient.v2_0.client.Client(
74 username=self.conf.username,
75 password=self.conf.password,
76 tenant_name=self.conf.tenant_name,
77 auth_url=self.conf.auth_url,
78 insecure=self.conf.disable_ssl_certificate_validation)
79
80 def _get_compute_client(self):
81
82 dscv = self.conf.disable_ssl_certificate_validation
83 region = self.conf.region
84
85 client_args = (
86 self.conf.username,
87 self.conf.password,
88 self.conf.tenant_name,
89 self.conf.auth_url
90 )
91
92 # Create our default Nova client to use in testing
93 return novaclient.client.Client(
94 self.NOVACLIENT_VERSION,
95 *client_args,
96 service_type='compute',
97 endpoint_type='publicURL',
98 region_name=region,
99 no_cache=True,
100 insecure=dscv,
101 http_log_debug=True)
102
103 def _get_network_client(self):
104 auth_url = self.conf.auth_url
105 dscv = self.conf.disable_ssl_certificate_validation
106
107 return neutronclient.v2_0.client.Client(
108 username=self.conf.username,
109 password=self.conf.password,
110 tenant_name=self.conf.tenant_name,
111 endpoint_type='publicURL',
112 auth_url=auth_url,
113 insecure=dscv)
114
115 def _get_volume_client(self):
116 auth_url = self.conf.auth_url
117 region = self.conf.region
118 endpoint_type = 'publicURL'
119 dscv = self.conf.disable_ssl_certificate_validation
120 return cinderclient.client.Client(
121 self.CINDERCLIENT_VERSION,
122 self.conf.username,
123 self.conf.password,
124 self.conf.tenant_name,
125 auth_url,
126 region_name=region,
127 endpoint_type=endpoint_type,
128 insecure=dscv,
129 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000130
131 def _get_object_client(self):
132 dscv = self.conf.disable_ssl_certificate_validation
133 args = {
134 'auth_version': '2.0',
135 'tenant_name': self.conf.tenant_name,
136 'user': self.conf.username,
137 'key': self.conf.password,
138 'authurl': self.conf.auth_url,
139 'os_options': {'endpoint_type': 'publicURL'},
140 'insecure': dscv,
141 }
142 return swiftclient.client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000143
144 def _get_metering_client(self):
145 dscv = self.conf.disable_ssl_certificate_validation
146
147 keystone = self._get_identity_client()
Steve Baker5e4d5f42015-05-26 13:28:28 +1200148 try:
149 endpoint = keystone.service_catalog.url_for(
150 attr='region',
151 filter_value=self.conf.region,
152 service_type='metering',
153 endpoint_type='publicURL')
Angus Salkeld406bbd52015-05-13 14:24:04 +1000154
Steve Baker5e4d5f42015-05-26 13:28:28 +1200155 except keystoneclient.exceptions.EndpointNotFound:
156 return None
157 else:
158 args = {
159 'username': self.conf.username,
160 'password': self.conf.password,
161 'tenant_name': self.conf.tenant_name,
162 'auth_url': self.conf.auth_url,
163 'insecure': dscv,
164 'region_name': self.conf.region,
165 'endpoint_type': 'publicURL',
166 'service_type': 'metering',
167 }
Angus Salkeld406bbd52015-05-13 14:24:04 +1000168
Steve Baker5e4d5f42015-05-26 13:28:28 +1200169 return ceilometerclient.client.Client(self.CEILOMETER_VERSION,
170 endpoint, **args)