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 | |
| 16 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 17 | import urlparse |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 18 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 19 | try: |
| 20 | from keystoneauth1.identity import v2 as keystoneauth_v2 |
| 21 | from keystoneauth1 import session as keystone_session |
| 22 | from keystoneclient.v2_0 import client as keystone_client |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 23 | # from glance import client as glance_client |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 24 | import glanceclient |
| 25 | from novaclient.v2 import client as nova_client |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 26 | from neutronclient.v2_0 import client as neutron_client |
| 27 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 28 | has_openstack = True |
| 29 | except: |
| 30 | has_openstack = False |
| 31 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 32 | from xosconfig import Config |
| 33 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 34 | |
| 35 | def require_enabled(callable): |
| 36 | def wrapper(*args, **kwds): |
| 37 | if has_openstack: |
| 38 | return callable(*args, **kwds) |
| 39 | else: |
| 40 | return None |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 41 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 42 | return wrapper |
| 43 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 44 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 45 | def parse_novarc(filename): |
| 46 | opts = {} |
| 47 | f = open(filename, 'r') |
| 48 | for line in f: |
| 49 | try: |
| 50 | line = line.replace('export', '').strip() |
| 51 | parts = line.split('=') |
| 52 | if len(parts) > 1: |
| 53 | value = parts[1].replace("\'", "") |
| 54 | value = value.replace('\"', '') |
| 55 | opts[parts[0]] = value |
| 56 | except: |
| 57 | pass |
| 58 | f.close() |
| 59 | return opts |
| 60 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 61 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 62 | class Client: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 63 | def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, |
| 64 | cacert=None, admin=True, *args, **kwds): |
| 65 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 66 | self.has_openstack = has_openstack |
| 67 | self.url = controller.auth_url |
| 68 | if admin: |
| 69 | self.username = controller.admin_user |
| 70 | self.password = controller.admin_password |
| 71 | self.tenant = controller.admin_tenant |
| 72 | else: |
| 73 | self.username = None |
| 74 | self.password = None |
| 75 | self.tenant = None |
| 76 | |
| 77 | if username: |
| 78 | self.username = username |
| 79 | if password: |
| 80 | self.password = password |
| 81 | if tenant: |
| 82 | self.tenant = tenant |
| 83 | if url: |
| 84 | self.url = url |
| 85 | if token: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 86 | self.token = token |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 87 | if endpoint: |
| 88 | self.endpoint = endpoint |
| 89 | |
| 90 | if cacert: |
| 91 | self.cacert = cacert |
| 92 | else: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 93 | self.cacert = Config.get("nova.ca_ssl_cert") |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 94 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 95 | |
| 96 | class KeystoneClient(Client): |
| 97 | def __init__(self, *args, **kwds): |
| 98 | Client.__init__(self, *args, **kwds) |
| 99 | if has_openstack: |
| 100 | auth = keystoneauth_v2.Password(username=self.username, |
| 101 | password=self.password, |
| 102 | tenant_name=self.tenant, |
| 103 | auth_url=self.url, |
| 104 | ) |
| 105 | sess = keystone_session.Session(auth=auth, verify=self.cacert, ) |
| 106 | self.client = keystone_client.Client(session=sess) |
| 107 | |
| 108 | @require_enabled |
| 109 | def connect(self, *args, **kwds): |
| 110 | self.__init__(*args, **kwds) |
| 111 | |
| 112 | @require_enabled |
| 113 | def __getattr__(self, name): |
| 114 | return getattr(self.client, name) |
| 115 | |
| 116 | |
| 117 | class Glance(Client): |
| 118 | def __init__(self, *args, **kwds): |
| 119 | Client.__init__(self, *args, **kwds) |
| 120 | if has_openstack: |
| 121 | self.client = glanceclient.get_client(host='0.0.0.0', |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 122 | username=self.username, |
| 123 | password=self.password, |
| 124 | tenant=self.tenant, |
| 125 | auth_url=self.url, |
| 126 | cacert=self.cacert |
| 127 | ) |
| 128 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 129 | @require_enabled |
| 130 | def __getattr__(self, name): |
| 131 | return getattr(self.client, name) |
| 132 | |
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 | class GlanceClient(Client): |
| 135 | def __init__(self, version, endpoint, token, cacert=None, *args, **kwds): |
| 136 | Client.__init__(self, *args, **kwds) |
| 137 | if has_openstack: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 138 | self.client = glanceclient.Client(version, |
| 139 | endpoint=endpoint, |
| 140 | token=token, |
| 141 | cacert=cacert |
| 142 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 143 | |
| 144 | @require_enabled |
| 145 | def __getattr__(self, name): |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 146 | return getattr(self.client, name) |
| 147 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 148 | |
| 149 | class NovaClient(Client): |
| 150 | def __init__(self, *args, **kwds): |
| 151 | Client.__init__(self, *args, **kwds) |
| 152 | if has_openstack: |
| 153 | self.client = nova_client.client.Client( |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 154 | "2", |
| 155 | username=self.username, |
| 156 | api_key=self.password, |
| 157 | project_id=self.tenant, |
| 158 | auth_url=self.url, |
| 159 | region_name='', |
| 160 | extensions=[], |
| 161 | service_type='compute', |
| 162 | service_name='', |
| 163 | cacert=self.cacert |
| 164 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 165 | |
| 166 | @require_enabled |
| 167 | def connect(self, *args, **kwds): |
| 168 | self.__init__(*args, **kwds) |
| 169 | |
| 170 | @require_enabled |
| 171 | def __getattr__(self, name): |
| 172 | return getattr(self.client, name) |
| 173 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 174 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 175 | class NovaDB(Client): |
| 176 | def __init__(self, *args, **kwds): |
| 177 | Client.__init__(self, *args, **kwds) |
| 178 | if has_openstack: |
| 179 | self.ctx = get_admin_context() |
| 180 | nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf']) |
| 181 | self.client = nova_db_api |
| 182 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 183 | @require_enabled |
| 184 | def connect(self, *args, **kwds): |
| 185 | self.__init__(*args, **kwds) |
| 186 | |
| 187 | @require_enabled |
| 188 | def __getattr__(self, name): |
| 189 | return getattr(self.client, name) |
| 190 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 191 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 192 | class NeutronClient(Client): |
| 193 | def __init__(self, *args, **kwds): |
| 194 | Client.__init__(self, *args, **kwds) |
| 195 | if has_openstack: |
| 196 | self.client = neutron_client.Client(username=self.username, |
| 197 | password=self.password, |
| 198 | tenant_name=self.tenant, |
| 199 | auth_url=self.url, |
| 200 | ca_cert=self.cacert |
| 201 | ) |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 202 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 203 | @require_enabled |
| 204 | def connect(self, *args, **kwds): |
| 205 | self.__init__(*args, **kwds) |
| 206 | |
| 207 | @require_enabled |
| 208 | def __getattr__(self, name): |
| 209 | return getattr(self.client, name) |
| 210 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 211 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 212 | class OpenStackClient: |
| 213 | """ |
| 214 | A simple native shell to the openstack backend services. |
| 215 | This class can receive all nova calls to the underlying testbed |
| 216 | """ |
| 217 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 218 | def __init__(self, *args, **kwds): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 219 | # instantiate managers |
| 220 | self.keystone = KeystoneClient(*args, **kwds) |
| 221 | url_parsed = urlparse.urlparse(self.keystone.url) |
| 222 | hostname = url_parsed.netloc.split(':')[0] |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 223 | token = self.keystone.client.tokens.authenticate(username=self.keystone.username, |
| 224 | password=self.keystone.password, |
| 225 | tenant_name=self.keystone.tenant) |
| 226 | # glance_endpoint = self.keystone.client.service_catalog.url_for(service_type='image', endpoint_type='publicURL') |
| 227 | # self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 228 | self.nova = NovaClient(*args, **kwds) |
| 229 | # self.nova_db = NovaDB(*args, **kwds) |
| 230 | self.neutron = NeutronClient(*args, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 231 | |
| 232 | @require_enabled |
| 233 | def connect(self, *args, **kwds): |
| 234 | self.__init__(*args, **kwds) |
| 235 | |
| 236 | @require_enabled |
| 237 | def authenticate(self): |
| 238 | return self.keystone.authenticate() |