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 cinderclient import client as cinder_client |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 16 | from gnocchiclient import client as gnocchi_client |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 17 | from heatclient import client as heat_client |
Ethan Lynn | 07fac1f | 2016-05-10 23:09:51 +0800 | [diff] [blame] | 18 | from keystoneauth1.identity.generic import password |
| 19 | from keystoneauth1 import session |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 20 | from neutronclient.v2_0 import client as neutron_client |
| 21 | from novaclient import client as nova_client |
| 22 | from swiftclient import client as swift_client |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 23 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 24 | |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 25 | class KeystoneWrapperClient(object): |
| 26 | """Wrapper object for keystone client |
| 27 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 28 | This wraps keystone client, so we can encpasulate certain |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 29 | added properties like auth_token, project_id etc. |
| 30 | """ |
| 31 | def __init__(self, auth_plugin, verify=True): |
| 32 | self.auth_plugin = auth_plugin |
| 33 | self.session = session.Session( |
| 34 | auth=auth_plugin, |
| 35 | verify=verify) |
| 36 | |
| 37 | @property |
| 38 | def auth_token(self): |
| 39 | return self.auth_plugin.get_token(self.session) |
| 40 | |
| 41 | @property |
| 42 | def auth_ref(self): |
| 43 | return self.auth_plugin.get_access(self.session) |
| 44 | |
| 45 | @property |
| 46 | def project_id(self): |
| 47 | return self.auth_plugin.get_project_id(self.session) |
| 48 | |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 49 | def get_endpoint_url(self, service_type, region=None, |
| 50 | endpoint_type='public'): |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 51 | kwargs = { |
| 52 | 'service_type': service_type, |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 53 | 'region_name': region, |
| 54 | 'interface': endpoint_type} |
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 | |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 65 | CINDER_API_VERSION = '3' |
| 66 | GNOCCHI_API_VERSION = '1' |
| 67 | HEAT_API_VERSION = '1' |
| 68 | KEYSTONE_API_VERSION = '3' |
huangtianhua | 15ad532 | 2016-06-02 14:39:13 +0800 | [diff] [blame] | 69 | NOVA_API_VERSION = '2.1' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 70 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 71 | def __init__(self, conf, admin_credentials=False): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 72 | self.conf = conf |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 73 | self.admin_credentials = admin_credentials |
| 74 | |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 75 | self.insecure = self.conf.disable_ssl_certificate_validation |
| 76 | self.ca_file = self.conf.ca_file |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 77 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 78 | self.identity_client = self._get_identity_client() |
| 79 | self.orchestration_client = self._get_orchestration_client() |
| 80 | self.compute_client = self._get_compute_client() |
| 81 | self.network_client = self._get_network_client() |
| 82 | self.volume_client = self._get_volume_client() |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 83 | self.object_client = self._get_object_client() |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 84 | self.metric_client = self._get_metric_client() |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 85 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 86 | def _username(self): |
| 87 | if self.admin_credentials: |
| 88 | return self.conf.admin_username |
| 89 | return self.conf.username |
| 90 | |
| 91 | def _password(self): |
| 92 | if self.admin_credentials: |
| 93 | return self.conf.admin_password |
| 94 | return self.conf.password |
| 95 | |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 96 | def _project_name(self): |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 97 | if self.admin_credentials: |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 98 | return self.conf.admin_project_name |
| 99 | return self.conf.project_name |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 100 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 101 | def _get_orchestration_client(self): |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 102 | endpoint = os.environ.get('HEAT_URL') |
| 103 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
rabi | ff3fbd5 | 2017-02-18 13:02:20 +0530 | [diff] [blame] | 104 | session = None |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 105 | else: |
rabi | ff3fbd5 | 2017-02-18 13:02:20 +0530 | [diff] [blame] | 106 | session = self.identity_client.session |
| 107 | |
| 108 | return heat_client.Client( |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 109 | self.HEAT_API_VERSION, |
rabi | ff3fbd5 | 2017-02-18 13:02:20 +0530 | [diff] [blame] | 110 | endpoint, |
| 111 | session=session, |
Matthias Bastian | 092d8bd | 2018-07-12 12:16:30 +0200 | [diff] [blame] | 112 | endpoint_type=self.conf.endpoint_type, |
rabi | ff3fbd5 | 2017-02-18 13:02:20 +0530 | [diff] [blame] | 113 | service_type='orchestration', |
| 114 | region_name=self.conf.region, |
| 115 | username=self._username(), |
| 116 | password=self._password()) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 117 | |
| 118 | def _get_identity_client(self): |
rabi | 133ee5f | 2016-12-01 09:54:37 +0530 | [diff] [blame] | 119 | user_domain_id = self.conf.user_domain_id |
| 120 | project_domain_id = self.conf.project_domain_id |
Rabi Mishra | 1d53874 | 2016-03-21 21:09:20 +0530 | [diff] [blame] | 121 | user_domain_name = self.conf.user_domain_name |
| 122 | project_domain_name = self.conf.project_domain_name |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 123 | kwargs = { |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 124 | 'username': self._username(), |
| 125 | 'password': self._password(), |
rabi | be38c30 | 2017-04-11 09:54:07 +0530 | [diff] [blame] | 126 | 'project_name': self._project_name(), |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 127 | 'auth_url': self.conf.auth_url, |
| 128 | 'user_domain_id': user_domain_id, |
| 129 | 'user_domain_name': user_domain_name, |
| 130 | 'project_domain_id': project_domain_id, |
| 131 | 'project_domain_name': project_domain_name |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 132 | } |
Rabi Mishra | 65493fb | 2016-01-29 22:23:21 +0530 | [diff] [blame] | 133 | auth = password.Password(**kwargs) |
tyagi | 39aa11a | 2016-03-07 04:47:00 -0800 | [diff] [blame] | 134 | if self.insecure: |
| 135 | verify_cert = False |
| 136 | else: |
| 137 | verify_cert = self.ca_file or True |
| 138 | |
| 139 | return KeystoneWrapperClient(auth, verify_cert) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 140 | |
| 141 | def _get_compute_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 142 | # Create our default Nova client to use in testing |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 143 | return nova_client.Client( |
huangtianhua | 15ad532 | 2016-06-02 14:39:13 +0800 | [diff] [blame] | 144 | self.NOVA_API_VERSION, |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 145 | session=self.identity_client.session, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 146 | service_type='compute', |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 147 | endpoint_type=self.conf.endpoint_type, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 148 | region_name=self.conf.region, |
rabi | 530626f | 2017-01-20 07:53:38 +0530 | [diff] [blame] | 149 | os_cache=False, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 150 | http_log_debug=True) |
| 151 | |
| 152 | def _get_network_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 153 | |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 154 | return neutron_client.Client( |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 155 | session=self.identity_client.session, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 156 | service_type='network', |
| 157 | region_name=self.conf.region, |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 158 | endpoint_type=self.conf.endpoint_type) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 159 | |
| 160 | def _get_volume_client(self): |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 161 | return cinder_client.Client( |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 162 | self.CINDER_API_VERSION, |
Rabi Mishra | 17f41de | 2016-05-03 11:19:41 +0530 | [diff] [blame] | 163 | session=self.identity_client.session, |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 164 | endpoint_type=self.conf.endpoint_type, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 165 | region_name=self.conf.region, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 166 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 167 | |
| 168 | def _get_object_client(self): |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 169 | args = { |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 170 | 'auth_version': self.KEYSTONE_API_VERSION, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 171 | 'session': self.identity_client.session, |
Pavlo Shchelokovskyy | 01ba7fd | 2018-06-08 14:39:18 +0300 | [diff] [blame] | 172 | 'cacert': self.ca_file, |
| 173 | 'insecure': self.insecure, |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 174 | 'os_options': {'endpoint_type': self.conf.endpoint_type, |
rabi | 613f815 | 2017-02-14 18:59:15 +0530 | [diff] [blame] | 175 | 'region_name': self.conf.region, |
| 176 | 'service_type': 'object-store'}, |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 177 | } |
Rabi Mishra | e9dffe5 | 2016-02-05 14:36:58 +0530 | [diff] [blame] | 178 | return swift_client.Connection(**args) |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 179 | |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 180 | def _get_metric_client(self): |
| 181 | |
Colleen Murphy | 30b1fd6 | 2017-12-29 12:43:53 +0100 | [diff] [blame] | 182 | adapter_options = {'interface': self.conf.endpoint_type, |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 183 | 'region_name': self.conf.region} |
| 184 | args = { |
| 185 | 'session': self.identity_client.session, |
| 186 | 'adapter_options': adapter_options |
| 187 | } |
Takashi Kajinami | 7ecadfb | 2024-07-12 11:31:21 +0900 | [diff] [blame] | 188 | return gnocchi_client.Client(version=self.GNOCCHI_API_VERSION, |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 189 | **args) |