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