blob: 1ba3a212ad9cd9f509f1ef9e9379bd1a6c27366b [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
Steve Baker450aa7f2014-08-25 10:37:27 +120015import cinderclient.client
16import heatclient.client
17import keystoneclient.exceptions
18import keystoneclient.v2_0.client
19import neutronclient.v2_0.client
20import novaclient.client
Angus Salkeld4408da32015-02-03 18:53:30 +100021import swiftclient
Steve Baker450aa7f2014-08-25 10:37:27 +120022
Steve Baker450aa7f2014-08-25 10:37:27 +120023
24class ClientManager(object):
25 """
26 Manager that provides access to the official python clients for
27 calling various OpenStack APIs.
28 """
29
30 CINDERCLIENT_VERSION = '1'
31 HEATCLIENT_VERSION = '1'
32 NOVACLIENT_VERSION = '2'
33
34 def __init__(self, conf):
35 self.conf = conf
36 self.identity_client = self._get_identity_client()
37 self.orchestration_client = self._get_orchestration_client()
38 self.compute_client = self._get_compute_client()
39 self.network_client = self._get_network_client()
40 self.volume_client = self._get_volume_client()
Angus Salkeld4408da32015-02-03 18:53:30 +100041 self.object_client = self._get_object_client()
Steve Baker450aa7f2014-08-25 10:37:27 +120042
43 def _get_orchestration_client(self):
Steve Baker450aa7f2014-08-25 10:37:27 +120044 region = self.conf.region
Angus Salkeld70c2f282014-11-21 08:51:41 +100045 endpoint = os.environ.get('HEAT_URL')
46 if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
47 token = None
48 else:
49 keystone = self._get_identity_client()
50 token = keystone.auth_token
Steve Baker450aa7f2014-08-25 10:37:27 +120051 try:
Angus Salkeld70c2f282014-11-21 08:51:41 +100052 if endpoint is None:
53 endpoint = keystone.service_catalog.url_for(
54 attr='region',
55 filter_value=region,
56 service_type='orchestration',
57 endpoint_type='publicURL')
Steve Baker450aa7f2014-08-25 10:37:27 +120058 except keystoneclient.exceptions.EndpointNotFound:
59 return None
60 else:
61 return heatclient.client.Client(
62 self.HEATCLIENT_VERSION,
63 endpoint,
64 token=token,
65 username=self.conf.username,
66 password=self.conf.password)
67
68 def _get_identity_client(self):
69 return keystoneclient.v2_0.client.Client(
70 username=self.conf.username,
71 password=self.conf.password,
72 tenant_name=self.conf.tenant_name,
73 auth_url=self.conf.auth_url,
74 insecure=self.conf.disable_ssl_certificate_validation)
75
76 def _get_compute_client(self):
77
78 dscv = self.conf.disable_ssl_certificate_validation
79 region = self.conf.region
80
81 client_args = (
82 self.conf.username,
83 self.conf.password,
84 self.conf.tenant_name,
85 self.conf.auth_url
86 )
87
88 # Create our default Nova client to use in testing
89 return novaclient.client.Client(
90 self.NOVACLIENT_VERSION,
91 *client_args,
92 service_type='compute',
93 endpoint_type='publicURL',
94 region_name=region,
95 no_cache=True,
96 insecure=dscv,
97 http_log_debug=True)
98
99 def _get_network_client(self):
100 auth_url = self.conf.auth_url
101 dscv = self.conf.disable_ssl_certificate_validation
102
103 return neutronclient.v2_0.client.Client(
104 username=self.conf.username,
105 password=self.conf.password,
106 tenant_name=self.conf.tenant_name,
107 endpoint_type='publicURL',
108 auth_url=auth_url,
109 insecure=dscv)
110
111 def _get_volume_client(self):
112 auth_url = self.conf.auth_url
113 region = self.conf.region
114 endpoint_type = 'publicURL'
115 dscv = self.conf.disable_ssl_certificate_validation
116 return cinderclient.client.Client(
117 self.CINDERCLIENT_VERSION,
118 self.conf.username,
119 self.conf.password,
120 self.conf.tenant_name,
121 auth_url,
122 region_name=region,
123 endpoint_type=endpoint_type,
124 insecure=dscv,
125 http_log_debug=True)
Angus Salkeld4408da32015-02-03 18:53:30 +1000126
127 def _get_object_client(self):
128 dscv = self.conf.disable_ssl_certificate_validation
129 args = {
130 'auth_version': '2.0',
131 'tenant_name': self.conf.tenant_name,
132 'user': self.conf.username,
133 'key': self.conf.password,
134 'authurl': self.conf.auth_url,
135 'os_options': {'endpoint_type': 'publicURL'},
136 'insecure': dscv,
137 }
138 return swiftclient.client.Connection(**args)