blob: 84afc350b44b4dc63770401e1763da9627b8ead1 [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -07001
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 Bakerb63ea792016-08-11 10:24:48 -070017import urlparse
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070018
Scott Bakerb63ea792016-08-11 10:24:48 -070019try:
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 Scandoloceccb1f2017-06-05 10:35:44 -070023 # from glance import client as glance_client
Scott Bakerb63ea792016-08-11 10:24:48 -070024 import glanceclient
25 from novaclient.v2 import client as nova_client
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070026 from neutronclient.v2_0 import client as neutron_client
27
Scott Bakerb63ea792016-08-11 10:24:48 -070028 has_openstack = True
29except:
30 has_openstack = False
31
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070032from xosconfig import Config
33
Scott Bakerb63ea792016-08-11 10:24:48 -070034
35def require_enabled(callable):
36 def wrapper(*args, **kwds):
37 if has_openstack:
38 return callable(*args, **kwds)
39 else:
40 return None
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070041
Scott Bakerb63ea792016-08-11 10:24:48 -070042 return wrapper
43
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070044
Scott Bakerb63ea792016-08-11 10:24:48 -070045def 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 Scandoloceccb1f2017-06-05 10:35:44 -070061
Scott Bakerb63ea792016-08-11 10:24:48 -070062class Client:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070063 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 Bakerb63ea792016-08-11 10:24:48 -070066 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 Scandoloceccb1f2017-06-05 10:35:44 -070086 self.token = token
Scott Bakerb63ea792016-08-11 10:24:48 -070087 if endpoint:
88 self.endpoint = endpoint
89
90 if cacert:
91 self.cacert = cacert
92 else:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070093 self.cacert = Config.get("nova.ca_ssl_cert")
Scott Bakerb63ea792016-08-11 10:24:48 -070094
Scott Bakerb63ea792016-08-11 10:24:48 -070095
96class 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
117class 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 Scandoloceccb1f2017-06-05 10:35:44 -0700122 username=self.username,
123 password=self.password,
124 tenant=self.tenant,
125 auth_url=self.url,
126 cacert=self.cacert
127 )
128
Scott Bakerb63ea792016-08-11 10:24:48 -0700129 @require_enabled
130 def __getattr__(self, name):
131 return getattr(self.client, name)
132
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700133
Scott Bakerb63ea792016-08-11 10:24:48 -0700134class GlanceClient(Client):
135 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
136 Client.__init__(self, *args, **kwds)
137 if has_openstack:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700138 self.client = glanceclient.Client(version,
139 endpoint=endpoint,
140 token=token,
141 cacert=cacert
142 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700143
144 @require_enabled
145 def __getattr__(self, name):
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700146 return getattr(self.client, name)
147
Scott Bakerb63ea792016-08-11 10:24:48 -0700148
149class 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 Scandoloceccb1f2017-06-05 10:35:44 -0700154 "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 Bakerb63ea792016-08-11 10:24:48 -0700165
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 Scandoloceccb1f2017-06-05 10:35:44 -0700174
Scott Bakerb63ea792016-08-11 10:24:48 -0700175class 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 Bakerb63ea792016-08-11 10:24:48 -0700183 @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 Scandoloceccb1f2017-06-05 10:35:44 -0700191
Scott Bakerb63ea792016-08-11 10:24:48 -0700192class 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 Scandoloceccb1f2017-06-05 10:35:44 -0700202
Scott Bakerb63ea792016-08-11 10:24:48 -0700203 @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 Scandoloceccb1f2017-06-05 10:35:44 -0700211
Scott Bakerb63ea792016-08-11 10:24:48 -0700212class 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 Scandoloceccb1f2017-06-05 10:35:44 -0700218 def __init__(self, *args, **kwds):
Scott Bakerb63ea792016-08-11 10:24:48 -0700219 # instantiate managers
220 self.keystone = KeystoneClient(*args, **kwds)
221 url_parsed = urlparse.urlparse(self.keystone.url)
222 hostname = url_parsed.netloc.split(':')[0]
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700223 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 Bakerb63ea792016-08-11 10:24:48 -0700228 self.nova = NovaClient(*args, **kwds)
229 # self.nova_db = NovaDB(*args, **kwds)
230 self.neutron = NeutronClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700231
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()