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 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 15 | import cinderclient.client |
| 16 | import heatclient.client |
| 17 | import keystoneclient.exceptions |
| 18 | import keystoneclient.v2_0.client |
| 19 | import neutronclient.v2_0.client |
| 20 | import novaclient.client |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame^] | 21 | import swiftclient |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 22 | |
| 23 | import logging |
| 24 | |
| 25 | LOG = logging.getLogger(__name__) |
| 26 | |
| 27 | |
| 28 | class ClientManager(object): |
| 29 | """ |
| 30 | Manager that provides access to the official python clients for |
| 31 | calling various OpenStack APIs. |
| 32 | """ |
| 33 | |
| 34 | CINDERCLIENT_VERSION = '1' |
| 35 | HEATCLIENT_VERSION = '1' |
| 36 | NOVACLIENT_VERSION = '2' |
| 37 | |
| 38 | def __init__(self, conf): |
| 39 | self.conf = conf |
| 40 | self.identity_client = self._get_identity_client() |
| 41 | self.orchestration_client = self._get_orchestration_client() |
| 42 | self.compute_client = self._get_compute_client() |
| 43 | self.network_client = self._get_network_client() |
| 44 | self.volume_client = self._get_volume_client() |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame^] | 45 | self.object_client = self._get_object_client() |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 46 | |
| 47 | def _get_orchestration_client(self): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 48 | region = self.conf.region |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 49 | endpoint = os.environ.get('HEAT_URL') |
| 50 | if os.environ.get('OS_NO_CLIENT_AUTH') == 'True': |
| 51 | token = None |
| 52 | else: |
| 53 | keystone = self._get_identity_client() |
| 54 | token = keystone.auth_token |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 55 | try: |
Angus Salkeld | 70c2f28 | 2014-11-21 08:51:41 +1000 | [diff] [blame] | 56 | if endpoint is None: |
| 57 | endpoint = keystone.service_catalog.url_for( |
| 58 | attr='region', |
| 59 | filter_value=region, |
| 60 | service_type='orchestration', |
| 61 | endpoint_type='publicURL') |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 62 | except keystoneclient.exceptions.EndpointNotFound: |
| 63 | return None |
| 64 | else: |
| 65 | return heatclient.client.Client( |
| 66 | self.HEATCLIENT_VERSION, |
| 67 | endpoint, |
| 68 | token=token, |
| 69 | username=self.conf.username, |
| 70 | password=self.conf.password) |
| 71 | |
| 72 | def _get_identity_client(self): |
| 73 | return keystoneclient.v2_0.client.Client( |
| 74 | username=self.conf.username, |
| 75 | password=self.conf.password, |
| 76 | tenant_name=self.conf.tenant_name, |
| 77 | auth_url=self.conf.auth_url, |
| 78 | insecure=self.conf.disable_ssl_certificate_validation) |
| 79 | |
| 80 | def _get_compute_client(self): |
| 81 | |
| 82 | dscv = self.conf.disable_ssl_certificate_validation |
| 83 | region = self.conf.region |
| 84 | |
| 85 | client_args = ( |
| 86 | self.conf.username, |
| 87 | self.conf.password, |
| 88 | self.conf.tenant_name, |
| 89 | self.conf.auth_url |
| 90 | ) |
| 91 | |
| 92 | # Create our default Nova client to use in testing |
| 93 | return novaclient.client.Client( |
| 94 | self.NOVACLIENT_VERSION, |
| 95 | *client_args, |
| 96 | service_type='compute', |
| 97 | endpoint_type='publicURL', |
| 98 | region_name=region, |
| 99 | no_cache=True, |
| 100 | insecure=dscv, |
| 101 | http_log_debug=True) |
| 102 | |
| 103 | def _get_network_client(self): |
| 104 | auth_url = self.conf.auth_url |
| 105 | dscv = self.conf.disable_ssl_certificate_validation |
| 106 | |
| 107 | return neutronclient.v2_0.client.Client( |
| 108 | username=self.conf.username, |
| 109 | password=self.conf.password, |
| 110 | tenant_name=self.conf.tenant_name, |
| 111 | endpoint_type='publicURL', |
| 112 | auth_url=auth_url, |
| 113 | insecure=dscv) |
| 114 | |
| 115 | def _get_volume_client(self): |
| 116 | auth_url = self.conf.auth_url |
| 117 | region = self.conf.region |
| 118 | endpoint_type = 'publicURL' |
| 119 | dscv = self.conf.disable_ssl_certificate_validation |
| 120 | return cinderclient.client.Client( |
| 121 | self.CINDERCLIENT_VERSION, |
| 122 | self.conf.username, |
| 123 | self.conf.password, |
| 124 | self.conf.tenant_name, |
| 125 | auth_url, |
| 126 | region_name=region, |
| 127 | endpoint_type=endpoint_type, |
| 128 | insecure=dscv, |
| 129 | http_log_debug=True) |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame^] | 130 | |
| 131 | def _get_object_client(self): |
| 132 | dscv = self.conf.disable_ssl_certificate_validation |
| 133 | args = { |
| 134 | 'auth_version': '2.0', |
| 135 | 'tenant_name': self.conf.tenant_name, |
| 136 | 'user': self.conf.username, |
| 137 | 'key': self.conf.password, |
| 138 | 'authurl': self.conf.auth_url, |
| 139 | 'os_options': {'endpoint_type': 'publicURL'}, |
| 140 | 'insecure': dscv, |
| 141 | } |
| 142 | return swiftclient.client.Connection(**args) |