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 |
Ethan Lynn | 07fac1f | 2016-05-10 23:09:51 +0800 | [diff] [blame] | 19 | from keystoneauth1 import exceptions as kc_exceptions |
| 20 | from keystoneauth1.identity.generic import password |
| 21 | from keystoneauth1 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, |
Ethan Lynn | 07fac1f | 2016-05-10 23:09:51 +0800 | [diff] [blame] | 54 | 'region_name': region} |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 55 | return self.auth_ref.service_catalog.url_for(**kwargs) |
| 56 | |
| 57 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 58 | class ClientManager(object): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 59 | """Provides access to the official python clients for calling various APIs. |
| 60 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 61 | Manager that provides access to the official python clients for |
| 62 | calling various OpenStack APIs. |
| 63 | """ |
| 64 | |
Rabi Mishra | a30ac12 | 2015-09-29 11:47:52 +0530 | [diff] [blame] | 65 | CINDERCLIENT_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 66 | HEATCLIENT_VERSION = '1' |
huangtianhua | 15ad532 | 2016-06-02 14:39:13 +0800 | [diff] [blame] | 67 | NOVA_API_VERSION = '2.1' |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 68 | CEILOMETER_VERSION = '2' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 69 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 70 | def __init__(self, conf, admin_credentials=False): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 71 | self.conf = conf |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 72 | self.admin_credentials = admin_credentials |
| 73 | |
rabi | 8fcf192 | 2017-04-19 09:21:54 +0530 | [diff] [blame] | 74 | self.auth_version = self.conf.auth_version |
| 75 | if not self.auth_version: |
| 76 | try: |
| 77 | self.auth_version = self.conf.auth_url.split('/v')[1] |
| 78 | except IndexError: |
| 79 | raise ValueError(_('Please specify version in auth_url or ' |
| 80 | 'auth_version in config.')) |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 81 | self.insecure = self.conf.disable_ssl_certificate_validation |
| 82 | self.ca_file = self.conf.ca_file |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 83 | |
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 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 92 | def _username(self): |
| 93 | if self.admin_credentials: |
| 94 | return self.conf.admin_username |
| 95 | return self.conf.username |
| 96 | |
| 97 | def _password(self): |
| 98 | if self.admin_credentials: |
| 99 | return self.conf.admin_password |
| 100 | return self.conf.password |
| 101 | |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 102 | def _project_name(self): |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 103 | if self.admin_credentials: |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 104 | return self.conf.admin_project_name |
| 105 | return self.conf.project_name |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 106 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 107 | def _get_orchestration_client(self): |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 108 | endpoint = os.environ.get('HEAT_URL') |
| 109 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
| 110 | token = None |
| 111 | else: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 112 | token = self.identity_client.auth_token |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 113 | try: |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 114 | if endpoint is None: |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 115 | endpoint = self.identity_client.get_endpoint_url( |
| 116 | 'orchestration', self.conf.region) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 117 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 118 | return None |
| 119 | else: |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 120 | return heat_client.Client( |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 121 | self.HEATCLIENT_VERSION, |
| 122 | endpoint, |
| 123 | token=token, |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 124 | username=self._username(), |
| 125 | password=self._password()) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 126 | |
| 127 | def _get_identity_client(self): |
rabi | 133ee5f | 2016-12-01 09:54:37 +0530 | [diff] [blame] | 128 | user_domain_id = self.conf.user_domain_id |
| 129 | project_domain_id = self.conf.project_domain_id |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame] | 130 | user_domain_name = self.conf.user_domain_name |
| 131 | project_domain_name = self.conf.project_domain_name |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 132 | kwargs = { |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 133 | 'username': self._username(), |
| 134 | 'password': self._password(), |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 135 | 'project_name': self._project_name(), |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 136 | 'auth_url': self.conf.auth_url |
| 137 | } |
| 138 | # keystone v2 can't ignore domain details |
| 139 | if self.auth_version == '3': |
| 140 | kwargs.update({ |
rabi | 133ee5f | 2016-12-01 09:54:37 +0530 | [diff] [blame] | 141 | 'user_domain_id': user_domain_id, |
| 142 | 'project_domain_id': project_domain_id, |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame] | 143 | 'user_domain_name': user_domain_name, |
| 144 | 'project_domain_name': project_domain_name}) |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 145 | auth = password.Password(**kwargs) |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 146 | if self.insecure: |
| 147 | verify_cert = False |
| 148 | else: |
| 149 | verify_cert = self.ca_file or True |
| 150 | |
| 151 | return KeystoneWrapperClient(auth, verify_cert) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 152 | |
| 153 | def _get_compute_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 154 | # Create our default Nova client to use in testing |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 155 | return nova_client.Client( |
huangtianhua | 15ad532 | 2016-06-02 14:39:13 +0800 | [diff] [blame] | 156 | self.NOVA_API_VERSION, |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 157 | session=self.identity_client.session, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 158 | service_type='compute', |
| 159 | endpoint_type='publicURL', |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 160 | region_name=self.conf.region, |
rabi | 530626f | 2017-01-20 07:53:38 +0530 | [diff] [blame] | 161 | os_cache=False, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 162 | http_log_debug=True) |
| 163 | |
| 164 | def _get_network_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 165 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 166 | return neutron_client.Client( |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 167 | session=self.identity_client.session, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 168 | service_type='network', |
| 169 | region_name=self.conf.region, |
| 170 | endpoint_type='publicURL') |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 171 | |
| 172 | def _get_volume_client(self): |
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, |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 175 | session=self.identity_client.session, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 176 | endpoint_type='publicURL', |
| 177 | region_name=self.conf.region, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 178 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 179 | |
| 180 | def _get_object_client(self): |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 181 | args = { |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 182 | 'auth_version': self.auth_version, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 183 | 'session': self.identity_client.session, |
| 184 | 'os_options': {'endpoint_type': 'publicURL', |
| 185 | 'region_name': self.conf.region, |
| 186 | 'service_type': 'object-store'}, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 187 | } |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 188 | return swift_client.Connection(**args) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 189 | |
| 190 | def _get_metering_client(self): |
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) |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 194 | except kc_exceptions.EndpointNotFound: |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 195 | return None |
| 196 | else: |
| 197 | args = { |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 198 | 'session': self.identity_client.session, |
Steve Baker | 5e4d5f4 | 2015-05-26 13:28:28 +1200 | [diff] [blame] | 199 | 'region_name': self.conf.region, |
| 200 | 'endpoint_type': 'publicURL', |
| 201 | 'service_type': 'metering', |
| 202 | } |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 203 | return ceilometer_client.Client(self.CEILOMETER_VERSION, |
| 204 | endpoint, **args) |