Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 1 | # 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 Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 13 | import os |
| 14 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 15 | from ceilometerclient import client as ceilometer_client |
| 16 | from cinderclient import client as cinder_client |
| 17 | from heatclient import client as heat_client |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 18 | from keystoneclient.auth.identity.generic import password |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 19 | from keystoneclient import exceptions as kc_exceptions |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 20 | from keystoneclient import session |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 21 | from neutronclient.v2_0 import client as neutron_client |
| 22 | from novaclient import client as nova_client |
| 23 | from swiftclient import client as swift_client |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 24 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 25 | |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 26 | class KeystoneWrapperClient(object): |
| 27 | """Wrapper object for keystone client |
| 28 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 29 | This wraps keystone client, so we can encpasulate certain |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 30 | 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 Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 60 | class ClientManager(object): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 61 | """Provides access to the official python clients for calling various APIs. |
| 62 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 63 | Manager that provides access to the official python clients for |
| 64 | calling various OpenStack APIs. |
| 65 | """ |
| 66 | |
Rabi Mishra | a30ac12 | 2015-09-29 11:47:52 +0530 | [diff] [blame] | 67 | CINDERCLIENT_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 68 | HEATCLIENT_VERSION = '1' |
| 69 | NOVACLIENT_VERSION = '2' |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 70 | CEILOMETER_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 71 | |
| 72 | def __init__(self, conf): |
| 73 | self.conf = conf |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 74 | self.v2_auth_url = self.conf.auth_url.replace('/v3', '/v2.0') |
| 75 | self.auth_version = self.conf.auth_url.split('/v')[1] |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 76 | 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 Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 81 | self.object_client = self._get_object_client() |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 82 | self.metering_client = self._get_metering_client() |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 83 | |
| 84 | def _get_orchestration_client(self): |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 85 | endpoint = os.environ.get('HEAT_URL') |
| 86 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
| 87 | token = None |
| 88 | else: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 89 | token = self.identity_client.auth_token |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 90 | try: |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 91 | if endpoint is None: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 92 | endpoint = self.identity_client.get_endpoint_url( |
| 93 | 'orchestration', self.conf.region) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 94 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 95 | return None |
| 96 | else: |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 97 | return heat_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 98 | 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 Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 105 | 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 Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 121 | |
| 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 Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 131 | # novaclient can not use v3 url |
| 132 | self.v2_auth_url |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 133 | ) |
| 134 | |
| 135 | # Create our default Nova client to use in testing |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 136 | return nova_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 137 | 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 Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 147 | dscv = self.conf.disable_ssl_certificate_validation |
| 148 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 149 | return neutron_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 150 | username=self.conf.username, |
| 151 | password=self.conf.password, |
| 152 | tenant_name=self.conf.tenant_name, |
| 153 | endpoint_type='publicURL', |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 154 | # neutronclient can not use v3 url |
| 155 | auth_url=self.v2_auth_url, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 156 | insecure=dscv) |
| 157 | |
| 158 | def _get_volume_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 159 | region = self.conf.region |
| 160 | endpoint_type = 'publicURL' |
| 161 | dscv = self.conf.disable_ssl_certificate_validation |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 162 | return cinder_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 163 | self.CINDERCLIENT_VERSION, |
| 164 | self.conf.username, |
| 165 | self.conf.password, |
| 166 | self.conf.tenant_name, |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 167 | # cinderclient can not use v3 url |
| 168 | self.v2_auth_url, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 169 | region_name=region, |
| 170 | endpoint_type=endpoint_type, |
| 171 | insecure=dscv, |
| 172 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 173 | |
| 174 | def _get_object_client(self): |
| 175 | dscv = self.conf.disable_ssl_certificate_validation |
| 176 | args = { |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 177 | 'auth_version': self.auth_version, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 178 | '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 Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 185 | return swift_client.Connection(**args) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 186 | |
| 187 | def _get_metering_client(self): |
| 188 | dscv = self.conf.disable_ssl_certificate_validation |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 189 | domain = self.conf.domain_name |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 190 | try: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 191 | endpoint = self.identity_client.get_endpoint_url('metering', |
| 192 | self.conf.region) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 193 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 194 | 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 Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 206 | # 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 Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 212 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 213 | return ceilometer_client.Client(self.CEILOMETER_VERSION, |
| 214 | endpoint, **args) |