Matteo Scandolo | f044103 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 16 | try: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 17 | import glanceclient |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 18 | from keystoneauth1 import identity |
| 19 | from keystoneauth1 import session |
| 20 | from keystoneclient import client |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 21 | from novaclient.v2 import client as nova_client |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 22 | from neutronclient.v2_0 import client as neutron_client |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | has_openstack = True |
| 24 | except: |
| 25 | has_openstack = False |
| 26 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 27 | from xosconfig import Config |
| 28 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 29 | |
| 30 | def require_enabled(callable): |
| 31 | def wrapper(*args, **kwds): |
| 32 | if has_openstack: |
| 33 | return callable(*args, **kwds) |
| 34 | else: |
| 35 | return None |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 36 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 37 | return wrapper |
| 38 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 39 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 40 | def parse_novarc(filename): |
| 41 | opts = {} |
| 42 | f = open(filename, 'r') |
| 43 | for line in f: |
| 44 | try: |
| 45 | line = line.replace('export', '').strip() |
| 46 | parts = line.split('=') |
| 47 | if len(parts) > 1: |
| 48 | value = parts[1].replace("\'", "") |
| 49 | value = value.replace('\"', '') |
| 50 | opts[parts[0]] = value |
| 51 | except: |
| 52 | pass |
| 53 | f.close() |
| 54 | return opts |
| 55 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 56 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 57 | class Client: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 58 | def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, |
| 59 | cacert=None, admin=True, *args, **kwds): |
| 60 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 61 | self.has_openstack = has_openstack |
| 62 | self.url = controller.auth_url |
| 63 | if admin: |
| 64 | self.username = controller.admin_user |
| 65 | self.password = controller.admin_password |
| 66 | self.tenant = controller.admin_tenant |
| 67 | else: |
| 68 | self.username = None |
| 69 | self.password = None |
| 70 | self.tenant = None |
| 71 | |
| 72 | if username: |
| 73 | self.username = username |
| 74 | if password: |
| 75 | self.password = password |
| 76 | if tenant: |
| 77 | self.tenant = tenant |
| 78 | if url: |
| 79 | self.url = url |
| 80 | if token: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 81 | self.token = token |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 82 | if endpoint: |
| 83 | self.endpoint = endpoint |
| 84 | |
| 85 | if cacert: |
| 86 | self.cacert = cacert |
| 87 | else: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 88 | self.cacert = Config.get("nova.ca_ssl_cert") |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 89 | |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 90 | def get_session(self): |
| 91 | if has_openstack: |
| 92 | version = self.url.rpartition('/')[2] |
| 93 | if version == 'v2.0': |
| 94 | auth_plugin = identity.v2.Password(username=self.username, |
| 95 | password=self.password, |
| 96 | tenant_name=self.tenant, |
| 97 | auth_url=self.url,) |
| 98 | else: |
| 99 | auth_plugin = identity.v3.Password( |
| 100 | auth_url=self.url, |
| 101 | username=self.username, |
| 102 | password=self.password, |
| 103 | project_name=self.tenant, |
| 104 | user_domain_id='default', |
| 105 | project_domain_id='default') |
| 106 | return session.Session(auth=auth_plugin, verify=self.cacert) |
| 107 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 108 | |
| 109 | class KeystoneClient(Client): |
| 110 | def __init__(self, *args, **kwds): |
| 111 | Client.__init__(self, *args, **kwds) |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 112 | self.client = client.Client(session=self.get_session()) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 113 | |
| 114 | @require_enabled |
| 115 | def connect(self, *args, **kwds): |
| 116 | self.__init__(*args, **kwds) |
| 117 | |
| 118 | @require_enabled |
| 119 | def __getattr__(self, name): |
gongysh | 6c0804c | 2018-01-27 16:38:28 +0800 | [diff] [blame] | 120 | if 'version' == name: |
| 121 | version = self.url.rpartition('/')[2] |
| 122 | return 'v2.0' if 'v2.0' == version else 'v3.0' |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 123 | return getattr(self.client, name) |
| 124 | |
| 125 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 126 | class GlanceClient(Client): |
| 127 | def __init__(self, version, endpoint, token, cacert=None, *args, **kwds): |
| 128 | Client.__init__(self, *args, **kwds) |
| 129 | if has_openstack: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 130 | self.client = glanceclient.Client(version, |
| 131 | endpoint=endpoint, |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 132 | session=self.get_session() |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 133 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 134 | |
| 135 | @require_enabled |
| 136 | def __getattr__(self, name): |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 137 | return getattr(self.client, name) |
| 138 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 139 | |
| 140 | class NovaClient(Client): |
| 141 | def __init__(self, *args, **kwds): |
| 142 | Client.__init__(self, *args, **kwds) |
| 143 | if has_openstack: |
| 144 | self.client = nova_client.client.Client( |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 145 | "2", |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 146 | session=self.get_session(), |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 147 | username=self.username, |
| 148 | api_key=self.password, |
| 149 | project_id=self.tenant, |
| 150 | auth_url=self.url, |
| 151 | region_name='', |
| 152 | extensions=[], |
| 153 | service_type='compute', |
| 154 | service_name='', |
| 155 | cacert=self.cacert |
| 156 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 157 | |
| 158 | @require_enabled |
| 159 | def connect(self, *args, **kwds): |
| 160 | self.__init__(*args, **kwds) |
| 161 | |
| 162 | @require_enabled |
| 163 | def __getattr__(self, name): |
| 164 | return getattr(self.client, name) |
| 165 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 166 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 167 | class NeutronClient(Client): |
| 168 | def __init__(self, *args, **kwds): |
| 169 | Client.__init__(self, *args, **kwds) |
| 170 | if has_openstack: |
yong sheng gong | fe5d315 | 2018-01-04 16:03:41 +0800 | [diff] [blame] | 171 | self.client = neutron_client.Client(session=self.get_session()) |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 172 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 173 | @require_enabled |
| 174 | def connect(self, *args, **kwds): |
| 175 | self.__init__(*args, **kwds) |
| 176 | |
| 177 | @require_enabled |
| 178 | def __getattr__(self, name): |
| 179 | return getattr(self.client, name) |
| 180 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 181 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 182 | class OpenStackClient: |
| 183 | """ |
| 184 | A simple native shell to the openstack backend services. |
| 185 | This class can receive all nova calls to the underlying testbed |
| 186 | """ |
| 187 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 188 | def __init__(self, *args, **kwds): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 189 | # instantiate managers |
| 190 | self.keystone = KeystoneClient(*args, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 191 | self.nova = NovaClient(*args, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 192 | self.neutron = NeutronClient(*args, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 193 | |
| 194 | @require_enabled |
| 195 | def connect(self, *args, **kwds): |
| 196 | self.__init__(*args, **kwds) |
| 197 | |
| 198 | @require_enabled |
| 199 | def authenticate(self): |
| 200 | return self.keystone.authenticate() |