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 | |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 15 | import ceilometerclient.client |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 16 | import cinderclient.client |
| 17 | import heatclient.client |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 18 | from keystoneclient.auth.identity.generic import password |
| 19 | import keystoneclient.client |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 20 | import keystoneclient.exceptions |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 21 | from keystoneclient import session |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 22 | import neutronclient.v2_0.client |
| 23 | import novaclient.client |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 24 | import swiftclient |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 25 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 26 | |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 27 | class KeystoneWrapperClient(object): |
| 28 | """Wrapper object for keystone client |
| 29 | |
| 30 | This Wraps keystone client,so we can encpasulate certain |
| 31 | added properties like auth_token, project_id etc. |
| 32 | """ |
| 33 | def __init__(self, auth_plugin, verify=True): |
| 34 | self.auth_plugin = auth_plugin |
| 35 | self.session = session.Session( |
| 36 | auth=auth_plugin, |
| 37 | verify=verify) |
| 38 | |
| 39 | @property |
| 40 | def auth_token(self): |
| 41 | return self.auth_plugin.get_token(self.session) |
| 42 | |
| 43 | @property |
| 44 | def auth_ref(self): |
| 45 | return self.auth_plugin.get_access(self.session) |
| 46 | |
| 47 | @property |
| 48 | def project_id(self): |
| 49 | return self.auth_plugin.get_project_id(self.session) |
| 50 | |
| 51 | def get_endpoint_url(self, service_type, region=None): |
| 52 | kwargs = { |
| 53 | 'service_type': service_type, |
| 54 | 'endpoint_type': 'publicURL'} |
| 55 | if region: |
| 56 | kwargs.update({'attr': 'region', |
| 57 | 'filter_value': region}) |
| 58 | return self.auth_ref.service_catalog.url_for(**kwargs) |
| 59 | |
| 60 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 61 | class ClientManager(object): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 62 | """Provides access to the official python clients for calling various APIs. |
| 63 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 64 | Manager that provides access to the official python clients for |
| 65 | calling various OpenStack APIs. |
| 66 | """ |
| 67 | |
Rabi Mishra | a30ac12 | 2015-09-29 11:47:52 +0530 | [diff] [blame] | 68 | CINDERCLIENT_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 69 | HEATCLIENT_VERSION = '1' |
| 70 | NOVACLIENT_VERSION = '2' |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 71 | CEILOMETER_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 72 | |
| 73 | def __init__(self, conf): |
| 74 | self.conf = conf |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 75 | self.v2_auth_url = self.conf.auth_url.replace('/v3', '/v2.0') |
| 76 | self.auth_version = self.conf.auth_url.split('/v')[1] |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 77 | self.identity_client = self._get_identity_client() |
| 78 | self.orchestration_client = self._get_orchestration_client() |
| 79 | self.compute_client = self._get_compute_client() |
| 80 | self.network_client = self._get_network_client() |
| 81 | self.volume_client = self._get_volume_client() |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 82 | self.object_client = self._get_object_client() |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 83 | self.metering_client = self._get_metering_client() |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 84 | |
| 85 | def _get_orchestration_client(self): |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 86 | endpoint = os.environ.get('HEAT_URL') |
| 87 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
| 88 | token = None |
| 89 | else: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 90 | token = self.identity_client.auth_token |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 91 | try: |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 92 | if endpoint is None: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 93 | endpoint = self.identity_client.get_endpoint_url( |
| 94 | 'orchestration', self.conf.region) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 95 | except keystoneclient.exceptions.EndpointNotFound: |
| 96 | return None |
| 97 | else: |
| 98 | return heatclient.client.Client( |
| 99 | self.HEATCLIENT_VERSION, |
| 100 | endpoint, |
| 101 | token=token, |
| 102 | username=self.conf.username, |
| 103 | password=self.conf.password) |
| 104 | |
| 105 | def _get_identity_client(self): |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 106 | domain = self.conf.domain_name |
| 107 | kwargs = { |
| 108 | 'username': self.conf.username, |
| 109 | 'password': self.conf.password, |
| 110 | 'tenant_name': self.conf.tenant_name, |
| 111 | 'auth_url': self.conf.auth_url |
| 112 | } |
| 113 | # keystone v2 can't ignore domain details |
| 114 | if self.auth_version == '3': |
| 115 | kwargs.update({ |
| 116 | 'project_domain_name': domain, |
| 117 | 'user_domain_name': domain}) |
| 118 | auth = password.Password(**kwargs) |
| 119 | return KeystoneWrapperClient( |
| 120 | auth, |
| 121 | not self.conf.disable_ssl_certificate_validation) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 122 | |
| 123 | def _get_compute_client(self): |
| 124 | |
| 125 | dscv = self.conf.disable_ssl_certificate_validation |
| 126 | region = self.conf.region |
| 127 | |
| 128 | client_args = ( |
| 129 | self.conf.username, |
| 130 | self.conf.password, |
| 131 | self.conf.tenant_name, |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 132 | # novaclient can not use v3 url |
| 133 | self.v2_auth_url |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 134 | ) |
| 135 | |
| 136 | # Create our default Nova client to use in testing |
| 137 | return novaclient.client.Client( |
| 138 | self.NOVACLIENT_VERSION, |
| 139 | *client_args, |
| 140 | service_type='compute', |
| 141 | endpoint_type='publicURL', |
| 142 | region_name=region, |
| 143 | no_cache=True, |
| 144 | insecure=dscv, |
| 145 | http_log_debug=True) |
| 146 | |
| 147 | def _get_network_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 148 | dscv = self.conf.disable_ssl_certificate_validation |
| 149 | |
| 150 | return neutronclient.v2_0.client.Client( |
| 151 | username=self.conf.username, |
| 152 | password=self.conf.password, |
| 153 | tenant_name=self.conf.tenant_name, |
| 154 | endpoint_type='publicURL', |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 155 | # neutronclient can not use v3 url |
| 156 | auth_url=self.v2_auth_url, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 157 | insecure=dscv) |
| 158 | |
| 159 | def _get_volume_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 160 | region = self.conf.region |
| 161 | endpoint_type = 'publicURL' |
| 162 | dscv = self.conf.disable_ssl_certificate_validation |
| 163 | return cinderclient.client.Client( |
| 164 | self.CINDERCLIENT_VERSION, |
| 165 | self.conf.username, |
| 166 | self.conf.password, |
| 167 | self.conf.tenant_name, |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 168 | # cinderclient can not use v3 url |
| 169 | self.v2_auth_url, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 170 | region_name=region, |
| 171 | endpoint_type=endpoint_type, |
| 172 | insecure=dscv, |
| 173 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 174 | |
| 175 | def _get_object_client(self): |
| 176 | dscv = self.conf.disable_ssl_certificate_validation |
| 177 | args = { |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 178 | 'auth_version': self.auth_version, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 179 | 'tenant_name': self.conf.tenant_name, |
| 180 | 'user': self.conf.username, |
| 181 | 'key': self.conf.password, |
| 182 | 'authurl': self.conf.auth_url, |
| 183 | 'os_options': {'endpoint_type': 'publicURL'}, |
| 184 | 'insecure': dscv, |
| 185 | } |
| 186 | return swiftclient.client.Connection(**args) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 187 | |
| 188 | def _get_metering_client(self): |
| 189 | dscv = self.conf.disable_ssl_certificate_validation |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 190 | domain = self.conf.domain_name |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 191 | try: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 192 | endpoint = self.identity_client.get_endpoint_url('metering', |
| 193 | self.conf.region) |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 194 | except keystoneclient.exceptions.EndpointNotFound: |
| 195 | return None |
| 196 | else: |
| 197 | args = { |
| 198 | 'username': self.conf.username, |
| 199 | 'password': self.conf.password, |
| 200 | 'tenant_name': self.conf.tenant_name, |
| 201 | 'auth_url': self.conf.auth_url, |
| 202 | 'insecure': dscv, |
| 203 | 'region_name': self.conf.region, |
| 204 | 'endpoint_type': 'publicURL', |
| 205 | 'service_type': 'metering', |
| 206 | } |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 207 | # ceilometerclient can't ignore domain details for |
| 208 | # v2 auth_url |
| 209 | if self.auth_version == '3': |
| 210 | args.update( |
| 211 | {'user_domain_name': domain, |
| 212 | 'project_domain_name': domain}) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 213 | |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 214 | return ceilometerclient.client.Client(self.CEILOMETER_VERSION, |
| 215 | endpoint, **args) |