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