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 |
Peter Razumovsky | 2810bb5 | 2016-03-03 15:36:12 +0300 | [diff] [blame] | 17 | from heat.common.i18n import _ |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 18 | from heatclient import client as heat_client |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 19 | from keystoneclient.auth.identity.generic import password |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 20 | from keystoneclient import exceptions as kc_exceptions |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 21 | from keystoneclient import session |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 22 | from neutronclient.v2_0 import client as neutron_client |
| 23 | from novaclient import client as nova_client |
| 24 | from swiftclient import client as swift_client |
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 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 30 | This wraps keystone client, so we can encpasulate certain |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 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 |
Peter Razumovsky | 2810bb5 | 2016-03-03 15:36:12 +0300 | [diff] [blame] | 75 | if self.conf.auth_url.find('/v'): |
| 76 | self.v2_auth_url = self.conf.auth_url.replace('/v3', '/v2.0') |
| 77 | self.auth_version = self.conf.auth_url.split('/v')[1] |
| 78 | else: |
| 79 | raise ValueError(_('Incorrectly specified auth_url config: no ' |
| 80 | 'version found.')) |
| 81 | |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 82 | self.insecure = self.conf.disable_ssl_certificate_validation |
| 83 | self.ca_file = self.conf.ca_file |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 84 | self.identity_client = self._get_identity_client() |
| 85 | self.orchestration_client = self._get_orchestration_client() |
| 86 | self.compute_client = self._get_compute_client() |
| 87 | self.network_client = self._get_network_client() |
| 88 | self.volume_client = self._get_volume_client() |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 89 | self.object_client = self._get_object_client() |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 90 | self.metering_client = self._get_metering_client() |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 91 | |
| 92 | def _get_orchestration_client(self): |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 93 | endpoint = os.environ.get('HEAT_URL') |
| 94 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
| 95 | token = None |
| 96 | else: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 97 | token = self.identity_client.auth_token |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 98 | try: |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 99 | if endpoint is None: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 100 | endpoint = self.identity_client.get_endpoint_url( |
| 101 | 'orchestration', self.conf.region) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 102 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 103 | return None |
| 104 | else: |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 105 | return heat_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 106 | self.HEATCLIENT_VERSION, |
| 107 | endpoint, |
| 108 | token=token, |
| 109 | username=self.conf.username, |
| 110 | password=self.conf.password) |
| 111 | |
| 112 | def _get_identity_client(self): |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame^] | 113 | user_domain_name = self.conf.user_domain_name |
| 114 | project_domain_name = self.conf.project_domain_name |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 115 | kwargs = { |
| 116 | 'username': self.conf.username, |
| 117 | 'password': self.conf.password, |
| 118 | 'tenant_name': self.conf.tenant_name, |
| 119 | 'auth_url': self.conf.auth_url |
| 120 | } |
| 121 | # keystone v2 can't ignore domain details |
| 122 | if self.auth_version == '3': |
| 123 | kwargs.update({ |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame^] | 124 | 'user_domain_name': user_domain_name, |
| 125 | 'project_domain_name': project_domain_name}) |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 126 | auth = password.Password(**kwargs) |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 127 | if self.insecure: |
| 128 | verify_cert = False |
| 129 | else: |
| 130 | verify_cert = self.ca_file or True |
| 131 | |
| 132 | return KeystoneWrapperClient(auth, verify_cert) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 133 | |
| 134 | def _get_compute_client(self): |
| 135 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 136 | region = self.conf.region |
| 137 | |
| 138 | client_args = ( |
| 139 | self.conf.username, |
| 140 | self.conf.password, |
| 141 | self.conf.tenant_name, |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 142 | # novaclient can not use v3 url |
| 143 | self.v2_auth_url |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 144 | ) |
| 145 | |
| 146 | # Create our default Nova client to use in testing |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 147 | return nova_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 148 | self.NOVACLIENT_VERSION, |
| 149 | *client_args, |
| 150 | service_type='compute', |
| 151 | endpoint_type='publicURL', |
| 152 | region_name=region, |
| 153 | no_cache=True, |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 154 | insecure=self.insecure, |
| 155 | cacert=self.ca_file, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 156 | http_log_debug=True) |
| 157 | |
| 158 | def _get_network_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 159 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 160 | return neutron_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 161 | username=self.conf.username, |
| 162 | password=self.conf.password, |
| 163 | tenant_name=self.conf.tenant_name, |
| 164 | endpoint_type='publicURL', |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 165 | # neutronclient can not use v3 url |
| 166 | auth_url=self.v2_auth_url, |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 167 | insecure=self.insecure, |
| 168 | ca_cert=self.ca_file) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 169 | |
| 170 | def _get_volume_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 171 | region = self.conf.region |
| 172 | endpoint_type = 'publicURL' |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 173 | return cinder_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 174 | self.CINDERCLIENT_VERSION, |
| 175 | self.conf.username, |
| 176 | self.conf.password, |
| 177 | self.conf.tenant_name, |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 178 | # cinderclient can not use v3 url |
| 179 | self.v2_auth_url, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 180 | region_name=region, |
| 181 | endpoint_type=endpoint_type, |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 182 | insecure=self.insecure, |
| 183 | cacert=self.ca_file, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 184 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 185 | |
| 186 | def _get_object_client(self): |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 187 | args = { |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 188 | 'auth_version': self.auth_version, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 189 | 'tenant_name': self.conf.tenant_name, |
| 190 | 'user': self.conf.username, |
| 191 | 'key': self.conf.password, |
| 192 | 'authurl': self.conf.auth_url, |
| 193 | 'os_options': {'endpoint_type': 'publicURL'}, |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 194 | 'insecure': self.insecure, |
| 195 | 'cacert': self.ca_file, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 196 | } |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 197 | return swift_client.Connection(**args) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 198 | |
| 199 | def _get_metering_client(self): |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame^] | 200 | user_domain_name = self.conf.user_domain_name |
| 201 | project_domain_name = self.conf.project_domain_name |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 202 | try: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 203 | endpoint = self.identity_client.get_endpoint_url('metering', |
| 204 | self.conf.region) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 205 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 206 | return None |
| 207 | else: |
| 208 | args = { |
| 209 | 'username': self.conf.username, |
| 210 | 'password': self.conf.password, |
| 211 | 'tenant_name': self.conf.tenant_name, |
| 212 | 'auth_url': self.conf.auth_url, |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 213 | 'insecure': self.insecure, |
| 214 | 'cacert': self.ca_file, |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 215 | 'region_name': self.conf.region, |
| 216 | 'endpoint_type': 'publicURL', |
| 217 | 'service_type': 'metering', |
| 218 | } |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 219 | # ceilometerclient can't ignore domain details for |
| 220 | # v2 auth_url |
| 221 | if self.auth_version == '3': |
| 222 | args.update( |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame^] | 223 | {'user_domain_name': user_domain_name, |
| 224 | 'project_domain_name': project_domain_name}) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 225 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 226 | return ceilometer_client.Client(self.CEILOMETER_VERSION, |
| 227 | endpoint, **args) |