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 | |
| 13 | import cinderclient.client |
| 14 | import heatclient.client |
| 15 | import keystoneclient.exceptions |
| 16 | import keystoneclient.v2_0.client |
| 17 | import neutronclient.v2_0.client |
| 18 | import novaclient.client |
| 19 | |
| 20 | import logging |
| 21 | |
| 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 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' |
| 34 | |
| 35 | def __init__(self, conf): |
| 36 | self.conf = conf |
| 37 | self.identity_client = self._get_identity_client() |
| 38 | self.orchestration_client = self._get_orchestration_client() |
| 39 | self.compute_client = self._get_compute_client() |
| 40 | self.network_client = self._get_network_client() |
| 41 | self.volume_client = self._get_volume_client() |
| 42 | |
| 43 | def _get_orchestration_client(self): |
| 44 | keystone = self._get_identity_client() |
| 45 | region = self.conf.region |
| 46 | token = keystone.auth_token |
| 47 | try: |
| 48 | endpoint = keystone.service_catalog.url_for( |
| 49 | attr='region', |
| 50 | filter_value=region, |
| 51 | service_type='orchestration', |
| 52 | endpoint_type='publicURL') |
| 53 | except keystoneclient.exceptions.EndpointNotFound: |
| 54 | return None |
| 55 | else: |
| 56 | return heatclient.client.Client( |
| 57 | self.HEATCLIENT_VERSION, |
| 58 | endpoint, |
| 59 | token=token, |
| 60 | username=self.conf.username, |
| 61 | password=self.conf.password) |
| 62 | |
| 63 | def _get_identity_client(self): |
| 64 | return keystoneclient.v2_0.client.Client( |
| 65 | username=self.conf.username, |
| 66 | password=self.conf.password, |
| 67 | tenant_name=self.conf.tenant_name, |
| 68 | auth_url=self.conf.auth_url, |
| 69 | insecure=self.conf.disable_ssl_certificate_validation) |
| 70 | |
| 71 | def _get_compute_client(self): |
| 72 | |
| 73 | dscv = self.conf.disable_ssl_certificate_validation |
| 74 | region = self.conf.region |
| 75 | |
| 76 | client_args = ( |
| 77 | self.conf.username, |
| 78 | self.conf.password, |
| 79 | self.conf.tenant_name, |
| 80 | self.conf.auth_url |
| 81 | ) |
| 82 | |
| 83 | # Create our default Nova client to use in testing |
| 84 | return novaclient.client.Client( |
| 85 | self.NOVACLIENT_VERSION, |
| 86 | *client_args, |
| 87 | service_type='compute', |
| 88 | endpoint_type='publicURL', |
| 89 | region_name=region, |
| 90 | no_cache=True, |
| 91 | insecure=dscv, |
| 92 | http_log_debug=True) |
| 93 | |
| 94 | def _get_network_client(self): |
| 95 | auth_url = self.conf.auth_url |
| 96 | dscv = self.conf.disable_ssl_certificate_validation |
| 97 | |
| 98 | return neutronclient.v2_0.client.Client( |
| 99 | username=self.conf.username, |
| 100 | password=self.conf.password, |
| 101 | tenant_name=self.conf.tenant_name, |
| 102 | endpoint_type='publicURL', |
| 103 | auth_url=auth_url, |
| 104 | insecure=dscv) |
| 105 | |
| 106 | def _get_volume_client(self): |
| 107 | auth_url = self.conf.auth_url |
| 108 | region = self.conf.region |
| 109 | endpoint_type = 'publicURL' |
| 110 | dscv = self.conf.disable_ssl_certificate_validation |
| 111 | return cinderclient.client.Client( |
| 112 | self.CINDERCLIENT_VERSION, |
| 113 | self.conf.username, |
| 114 | self.conf.password, |
| 115 | self.conf.tenant_name, |
| 116 | auth_url, |
| 117 | region_name=region, |
| 118 | endpoint_type=endpoint_type, |
| 119 | insecure=dscv, |
| 120 | http_log_debug=True) |