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