blob: c50020421042a92bee8644eb241788569ce8d552 [file] [log] [blame]
Tony Mackf8a1a612014-05-06 23:42:40 -04001import urlparse
Siobhan Tully30fd4292013-05-10 08:59:56 -04002try:
3 from keystoneclient.v2_0 import client as keystone_client
Sapan Bhatia475c5972014-11-05 10:32:41 -05004 #from glance import client as glance_client
Tony Mackf8a1a612014-05-06 23:42:40 -04005 import glanceclient
Siobhan Tully30fd4292013-05-10 08:59:56 -04006 from novaclient.v1_1 import client as nova_client
Sapan Bhatia475c5972014-11-05 10:32:41 -05007 from neutronclient.v2_0 import client as quantum_client
Siobhan Tully30fd4292013-05-10 08:59:56 -04008 has_openstack = True
9except:
10 has_openstack = False
11
Scott Baker76a840e2015-02-11 21:38:09 -080012from xos.config import Config
Siobhan Tully30fd4292013-05-10 08:59:56 -040013
14def require_enabled(callable):
15 def wrapper(*args, **kwds):
16 if has_openstack:
17 return callable(*args, **kwds)
18 else:
19 return None
20 return wrapper
21
22def parse_novarc(filename):
23 opts = {}
24 f = open(filename, 'r')
25 for line in f:
26 try:
27 line = line.replace('export', '').strip()
28 parts = line.split('=')
29 if len(parts) > 1:
30 value = parts[1].replace("\'", "")
31 value = value.replace('\"', '')
32 opts[parts[0]] = value
33 except:
34 pass
35 f.close()
36 return opts
37
38class Client:
Andy Bavier13653fe2015-10-05 17:01:26 -040039 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, cacert=None, admin=True, *args, **kwds):
Tony Mack94466922014-06-19 20:09:40 -040040
Siobhan Tully30fd4292013-05-10 08:59:56 -040041 self.has_openstack = has_openstack
Tony Mackf6911322014-12-26 13:57:08 -050042 self.url = controller.auth_url
Tony Mack49e839c2014-04-07 19:49:01 -040043 if admin:
Tony Mackf6911322014-12-26 13:57:08 -050044 self.username = controller.admin_user
45 self.password = controller.admin_password
46 self.tenant = controller.admin_tenant
Tony Mack49e839c2014-04-07 19:49:01 -040047 else:
48 self.username = None
49 self.password = None
50 self.tenant = None
Siobhan Tully30fd4292013-05-10 08:59:56 -040051
52 if username:
53 self.username = username
54 if password:
55 self.password = password
56 if tenant:
57 self.tenant = tenant
58 if url:
59 self.url = url
Tony Macke4be32f2014-03-11 20:45:25 -040060 if token:
61 self.token = token
62 if endpoint:
63 self.endpoint = endpoint
Siobhan Tully30fd4292013-05-10 08:59:56 -040064
Andy Bavier13653fe2015-10-05 17:01:26 -040065 self.cacert = cacert
66
Tony Mack976d7742014-03-18 22:00:52 -040067 #if '@' in self.username:
68 # self.username = self.username[:self.username.index('@')]
Siobhan Tully30fd4292013-05-10 08:59:56 -040069
70class KeystoneClient(Client):
71 def __init__(self, *args, **kwds):
72 Client.__init__(self, *args, **kwds)
73 if has_openstack:
74 self.client = keystone_client.Client(username=self.username,
75 password=self.password,
76 tenant_name=self.tenant,
Tony Mackdd240952014-06-03 23:02:00 -040077 auth_url=self.url
Tony Macke4be32f2014-03-11 20:45:25 -040078 )
Siobhan Tully30fd4292013-05-10 08:59:56 -040079
80 @require_enabled
81 def connect(self, *args, **kwds):
82 self.__init__(*args, **kwds)
83
84 @require_enabled
85 def __getattr__(self, name):
86 return getattr(self.client, name)
87
88
Tony Macke93322e2015-01-08 21:10:55 -050089class Glance(Client):
Siobhan Tully30fd4292013-05-10 08:59:56 -040090 def __init__(self, *args, **kwds):
91 Client.__init__(self, *args, **kwds)
92 if has_openstack:
Sapan Bhatia475c5972014-11-05 10:32:41 -050093 self.client = glanceclient.get_client(host='0.0.0.0',
Siobhan Tully30fd4292013-05-10 08:59:56 -040094 username=self.username,
95 password=self.password,
96 tenant=self.tenant,
97 auth_url=self.url)
98 @require_enabled
99 def __getattr__(self, name):
100 return getattr(self.client, name)
101
Tony Macke93322e2015-01-08 21:10:55 -0500102class GlanceClient(Client):
103 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
Tony Mackf8a1a612014-05-06 23:42:40 -0400104 Client.__init__(self, *args, **kwds)
105 if has_openstack:
Tony Macke93322e2015-01-08 21:10:55 -0500106 self.client = glanceclient.Client(version,
107 endpoint=endpoint,
108 token=token,
109 cacert=cacert
110 )
Tony Mackf8a1a612014-05-06 23:42:40 -0400111
112 @require_enabled
113 def __getattr__(self, name):
114 return getattr(self.client, name)
115
Siobhan Tully30fd4292013-05-10 08:59:56 -0400116class NovaClient(Client):
117 def __init__(self, *args, **kwds):
118 Client.__init__(self, *args, **kwds)
119 if has_openstack:
120 self.client = nova_client.Client(username=self.username,
121 api_key=self.password,
122 project_id=self.tenant,
123 auth_url=self.url,
124 region_name='',
125 extensions=[],
126 service_type='compute',
127 service_name='',
128 )
129
130 @require_enabled
131 def connect(self, *args, **kwds):
132 self.__init__(*args, **kwds)
133
134 @require_enabled
135 def __getattr__(self, name):
136 return getattr(self.client, name)
137
Tony Mackb0d97422013-06-10 09:57:45 -0400138class NovaDB(Client):
139 def __init__(self, *args, **kwds):
140 Client.__init__(self, *args, **kwds)
141 if has_openstack:
142 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400143 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400144 self.client = nova_db_api
145
146
147 @require_enabled
148 def connect(self, *args, **kwds):
149 self.__init__(*args, **kwds)
150
151 @require_enabled
152 def __getattr__(self, name):
153 return getattr(self.client, name)
154
Siobhan Tully30fd4292013-05-10 08:59:56 -0400155class QuantumClient(Client):
156 def __init__(self, *args, **kwds):
157 Client.__init__(self, *args, **kwds)
158 if has_openstack:
159 self.client = quantum_client.Client(username=self.username,
160 password=self.password,
161 tenant_name=self.tenant,
Andy Bavier13653fe2015-10-05 17:01:26 -0400162 auth_url=self.url,
163 ca_cert=self.cacert)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400164 @require_enabled
165 def connect(self, *args, **kwds):
166 self.__init__(*args, **kwds)
167
168 @require_enabled
169 def __getattr__(self, name):
170 return getattr(self.client, name)
171
172class OpenStackClient:
173 """
174 A simple native shell to the openstack backend services.
175 This class can receive all nova calls to the underlying testbed
176 """
177
178 def __init__ ( self, *args, **kwds) :
179 # instantiate managers
180 self.keystone = KeystoneClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400181 url_parsed = urlparse.urlparse(self.keystone.url)
182 hostname = url_parsed.netloc.split(':')[0]
183 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
Tony Macke93322e2015-01-08 21:10:55 -0500184 glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
Tony Mackf8a1a612014-05-06 23:42:40 -0400185
Tony Macke93322e2015-01-08 21:10:55 -0500186 self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400187 self.nova = NovaClient(*args, **kwds)
Sapan Bhatia475c5972014-11-05 10:32:41 -0500188 # self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400189 self.quantum = QuantumClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400190
Siobhan Tully30fd4292013-05-10 08:59:56 -0400191
192 @require_enabled
193 def connect(self, *args, **kwds):
194 self.__init__(*args, **kwds)
195
196 @require_enabled
197 def authenticate(self):
198 return self.keystone.authenticate()