blob: c7a7f60e6cf45d26c6119542ab40388bb7cfed8a [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):
26 """
27 Manager that provides access to the official python clients for
28 calling various OpenStack APIs.
29 """
30
31 CINDERCLIENT_VERSION = '1'
32 HEATCLIENT_VERSION = '1'
33 NOVACLIENT_VERSION = '2'
Angus Salkeld406bbd52015-05-13 14:24:04 +100034 CEILOMETER_VERSION = '2'
Steve Baker450aa7f2014-08-25 10:37:27 +120035
36 def __init__(self, conf):
37 self.conf = conf
38 self.identity_client = self._get_identity_client()
39 self.orchestration_client = self._get_orchestration_client()
40 self.compute_client = self._get_compute_client()
41 self.network_client = self._get_network_client()
42 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100043 self.object_client = self._get_object_client()
Angus Salkeld406bbd52015-05-13 14:24:04 +100044 self.metering_client = self._get_metering_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120045
46 def _get_orchestration_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +120047 region = self.conf.region
Angus Salkeld70c2f282014-11-21 08:51:41 +100048 endpoint = os.environ.get('HEAT_URL')
49 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
50 token = None
51 else:
52 keystone = self._get_identity_client()
53 token = keystone.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +120054 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +100055 if endpoint is None:
56 endpoint = keystone.service_catalog.url_for(
57 attr='region',
58 filter_value=region,
59 service_type='orchestration',
60 endpoint_type='publicURL')
Steve Baker450aa7f2014-08-25 10:37:27 +120061 except keystoneclient.exceptions.EndpointNotFound:
62 return None
63 else:
64 return heatclient.client.Client(
65 self.HEATCLIENT_VERSION,
66 endpoint,
67 token=token,
68 username=self.conf.username,
69 password=self.conf.password)
70
71 def _get_identity_client(self):
72 return keystoneclient.v2_0.client.Client(
73 username=self.conf.username,
74 password=self.conf.password,
75 tenant_name=self.conf.tenant_name,
76 auth_url=self.conf.auth_url,
77 insecure=self.conf.disable_ssl_certificate_validation)
78
79 def _get_compute_client(self):
80
81 dscv = self.conf.disable_ssl_certificate_validation
82 region = self.conf.region
83
84 client_args = (
85 self.conf.username,
86 self.conf.password,
87 self.conf.tenant_name,
88 self.conf.auth_url
89 )
90
91 # Create our default Nova client to use in testing
92 return novaclient.client.Client(
93 self.NOVACLIENT_VERSION,
94 *client_args,
95 service_type='compute',
96 endpoint_type='publicURL',
97 region_name=region,
98 no_cache=True,
99 insecure=dscv,
100 http_log_debug=True)
101
102 def _get_network_client(self):
103 auth_url = self.conf.auth_url
104 dscv = self.conf.disable_ssl_certificate_validation
105
106 return neutronclient.v2_0.client.Client(
107 username=self.conf.username,
108 password=self.conf.password,
109 tenant_name=self.conf.tenant_name,
110 endpoint_type='publicURL',
111 auth_url=auth_url,
112 insecure=dscv)
113
114 def _get_volume_client(self):
115 auth_url = self.conf.auth_url
116 region = self.conf.region
117 endpoint_type = 'publicURL'
118 dscv = self.conf.disable_ssl_certificate_validation
119 return cinderclient.client.Client(
120 self.CINDERCLIENT_VERSION,
121 self.conf.username,
122 self.conf.password,
123 self.conf.tenant_name,
124 auth_url,
125 region_name=region,
126 endpoint_type=endpoint_type,
127 insecure=dscv,
128 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000129
130 def _get_object_client(self):
131 dscv = self.conf.disable_ssl_certificate_validation
132 args = {
133 'auth_version': '2.0',
134 'tenant_name': self.conf.tenant_name,
135 'user': self.conf.username,
136 'key': self.conf.password,
137 'authurl': self.conf.auth_url,
138 'os_options': {'endpoint_type': 'publicURL'},
139 'insecure': dscv,
140 }
141 return swiftclient.client.Connection(**args)
Angus Salkeld406bbd52015-05-13 14:24:04 +1000142
143 def _get_metering_client(self):
144 dscv = self.conf.disable_ssl_certificate_validation
145
146 keystone = self._get_identity_client()
Steve Baker5e4d5f42015-05-26 13:28:28 +1200147 try:
148 endpoint = keystone.service_catalog.url_for(
149 attr='region',
150 filter_value=self.conf.region,
151 service_type='metering',
152 endpoint_type='publicURL')
Angus Salkeld406bbd52015-05-13 14:24:04 +1000153
Steve Baker5e4d5f42015-05-26 13:28:28 +1200154 except keystoneclient.exceptions.EndpointNotFound:
155 return None
156 else:
157 args = {
158 'username': self.conf.username,
159 'password': self.conf.password,
160 'tenant_name': self.conf.tenant_name,
161 'auth_url': self.conf.auth_url,
162 'insecure': dscv,
163 'region_name': self.conf.region,
164 'endpoint_type': 'publicURL',
165 'service_type': 'metering',
166 }
Angus Salkeld406bbd52015-05-13 14:24:04 +1000167
Steve Baker5e4d5f42015-05-26 13:28:28 +1200168 return ceilometerclient.client.Client(self.CEILOMETER_VERSION,
169 endpoint, **args)