blob: 872ab89d8d900a29d3d1773d40cbae7983c5213e [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import urlparse
Matteo Scandoloceccb1f2017-06-05 10:35:44 -07002
Scott Bakerb63ea792016-08-11 10:24:48 -07003try:
4 from keystoneauth1.identity import v2 as keystoneauth_v2
5 from keystoneauth1 import session as keystone_session
6 from keystoneclient.v2_0 import client as keystone_client
Matteo Scandoloceccb1f2017-06-05 10:35:44 -07007 # from glance import client as glance_client
Scott Bakerb63ea792016-08-11 10:24:48 -07008 import glanceclient
9 from novaclient.v2 import client as nova_client
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070010 from neutronclient.v2_0 import client as neutron_client
11
Scott Bakerb63ea792016-08-11 10:24:48 -070012 has_openstack = True
13except:
14 has_openstack = False
15
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070016from xosconfig import Config
17
Scott Bakerb63ea792016-08-11 10:24:48 -070018
19def require_enabled(callable):
20 def wrapper(*args, **kwds):
21 if has_openstack:
22 return callable(*args, **kwds)
23 else:
24 return None
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070025
Scott Bakerb63ea792016-08-11 10:24:48 -070026 return wrapper
27
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070028
Scott Bakerb63ea792016-08-11 10:24:48 -070029def parse_novarc(filename):
30 opts = {}
31 f = open(filename, 'r')
32 for line in f:
33 try:
34 line = line.replace('export', '').strip()
35 parts = line.split('=')
36 if len(parts) > 1:
37 value = parts[1].replace("\'", "")
38 value = value.replace('\"', '')
39 opts[parts[0]] = value
40 except:
41 pass
42 f.close()
43 return opts
44
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070045
Scott Bakerb63ea792016-08-11 10:24:48 -070046class Client:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070047 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None,
48 cacert=None, admin=True, *args, **kwds):
49
Scott Bakerb63ea792016-08-11 10:24:48 -070050 self.has_openstack = has_openstack
51 self.url = controller.auth_url
52 if admin:
53 self.username = controller.admin_user
54 self.password = controller.admin_password
55 self.tenant = controller.admin_tenant
56 else:
57 self.username = None
58 self.password = None
59 self.tenant = None
60
61 if username:
62 self.username = username
63 if password:
64 self.password = password
65 if tenant:
66 self.tenant = tenant
67 if url:
68 self.url = url
69 if token:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070070 self.token = token
Scott Bakerb63ea792016-08-11 10:24:48 -070071 if endpoint:
72 self.endpoint = endpoint
73
74 if cacert:
75 self.cacert = cacert
76 else:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070077 self.cacert = Config.get("nova.ca_ssl_cert")
Scott Bakerb63ea792016-08-11 10:24:48 -070078
Scott Bakerb63ea792016-08-11 10:24:48 -070079
80class KeystoneClient(Client):
81 def __init__(self, *args, **kwds):
82 Client.__init__(self, *args, **kwds)
83 if has_openstack:
84 auth = keystoneauth_v2.Password(username=self.username,
85 password=self.password,
86 tenant_name=self.tenant,
87 auth_url=self.url,
88 )
89 sess = keystone_session.Session(auth=auth, verify=self.cacert, )
90 self.client = keystone_client.Client(session=sess)
91
92 @require_enabled
93 def connect(self, *args, **kwds):
94 self.__init__(*args, **kwds)
95
96 @require_enabled
97 def __getattr__(self, name):
98 return getattr(self.client, name)
99
100
101class Glance(Client):
102 def __init__(self, *args, **kwds):
103 Client.__init__(self, *args, **kwds)
104 if has_openstack:
105 self.client = glanceclient.get_client(host='0.0.0.0',
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700106 username=self.username,
107 password=self.password,
108 tenant=self.tenant,
109 auth_url=self.url,
110 cacert=self.cacert
111 )
112
Scott Bakerb63ea792016-08-11 10:24:48 -0700113 @require_enabled
114 def __getattr__(self, name):
115 return getattr(self.client, name)
116
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700117
Scott Bakerb63ea792016-08-11 10:24:48 -0700118class GlanceClient(Client):
119 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
120 Client.__init__(self, *args, **kwds)
121 if has_openstack:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700122 self.client = glanceclient.Client(version,
123 endpoint=endpoint,
124 token=token,
125 cacert=cacert
126 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700127
128 @require_enabled
129 def __getattr__(self, name):
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700130 return getattr(self.client, name)
131
Scott Bakerb63ea792016-08-11 10:24:48 -0700132
133class NovaClient(Client):
134 def __init__(self, *args, **kwds):
135 Client.__init__(self, *args, **kwds)
136 if has_openstack:
137 self.client = nova_client.client.Client(
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700138 "2",
139 username=self.username,
140 api_key=self.password,
141 project_id=self.tenant,
142 auth_url=self.url,
143 region_name='',
144 extensions=[],
145 service_type='compute',
146 service_name='',
147 cacert=self.cacert
148 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700149
150 @require_enabled
151 def connect(self, *args, **kwds):
152 self.__init__(*args, **kwds)
153
154 @require_enabled
155 def __getattr__(self, name):
156 return getattr(self.client, name)
157
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700158
Scott Bakerb63ea792016-08-11 10:24:48 -0700159class NovaDB(Client):
160 def __init__(self, *args, **kwds):
161 Client.__init__(self, *args, **kwds)
162 if has_openstack:
163 self.ctx = get_admin_context()
164 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
165 self.client = nova_db_api
166
Scott Bakerb63ea792016-08-11 10:24:48 -0700167 @require_enabled
168 def connect(self, *args, **kwds):
169 self.__init__(*args, **kwds)
170
171 @require_enabled
172 def __getattr__(self, name):
173 return getattr(self.client, name)
174
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700175
Scott Bakerb63ea792016-08-11 10:24:48 -0700176class NeutronClient(Client):
177 def __init__(self, *args, **kwds):
178 Client.__init__(self, *args, **kwds)
179 if has_openstack:
180 self.client = neutron_client.Client(username=self.username,
181 password=self.password,
182 tenant_name=self.tenant,
183 auth_url=self.url,
184 ca_cert=self.cacert
185 )
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700186
Scott Bakerb63ea792016-08-11 10:24:48 -0700187 @require_enabled
188 def connect(self, *args, **kwds):
189 self.__init__(*args, **kwds)
190
191 @require_enabled
192 def __getattr__(self, name):
193 return getattr(self.client, name)
194
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700195
Scott Bakerb63ea792016-08-11 10:24:48 -0700196class OpenStackClient:
197 """
198 A simple native shell to the openstack backend services.
199 This class can receive all nova calls to the underlying testbed
200 """
201
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700202 def __init__(self, *args, **kwds):
Scott Bakerb63ea792016-08-11 10:24:48 -0700203 # instantiate managers
204 self.keystone = KeystoneClient(*args, **kwds)
205 url_parsed = urlparse.urlparse(self.keystone.url)
206 hostname = url_parsed.netloc.split(':')[0]
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700207 token = self.keystone.client.tokens.authenticate(username=self.keystone.username,
208 password=self.keystone.password,
209 tenant_name=self.keystone.tenant)
210 # glance_endpoint = self.keystone.client.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
211 # self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700212 self.nova = NovaClient(*args, **kwds)
213 # self.nova_db = NovaDB(*args, **kwds)
214 self.neutron = NeutronClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700215
216 @require_enabled
217 def connect(self, *args, **kwds):
218 self.__init__(*args, **kwds)
219
220 @require_enabled
221 def authenticate(self):
222 return self.keystone.authenticate()