blob: 0aa6c7d88ba641318fa6d44c45867f0ee2526e03 [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 Bhatia14356b72014-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 Bhatia14356b72014-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
12from planetstack.config import Config
13
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:
Sapan Bhatiaa554a012014-12-22 01:38:33 -050039 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=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 Mack4cc17882014-12-26 13:57:08 -050042 self.url = controller.auth_url
Tony Mack49e839c2014-04-07 19:49:01 -040043 if admin:
Tony Mack4cc17882014-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
Tony Mack976d7742014-03-18 22:00:52 -040065 #if '@' in self.username:
66 # self.username = self.username[:self.username.index('@')]
Siobhan Tully30fd4292013-05-10 08:59:56 -040067
68class KeystoneClient(Client):
69 def __init__(self, *args, **kwds):
70 Client.__init__(self, *args, **kwds)
71 if has_openstack:
72 self.client = keystone_client.Client(username=self.username,
73 password=self.password,
74 tenant_name=self.tenant,
Tony Mackdd240952014-06-03 23:02:00 -040075 auth_url=self.url
Tony Macke4be32f2014-03-11 20:45:25 -040076 )
Siobhan Tully30fd4292013-05-10 08:59:56 -040077
78 @require_enabled
79 def connect(self, *args, **kwds):
80 self.__init__(*args, **kwds)
81
82 @require_enabled
83 def __getattr__(self, name):
84 return getattr(self.client, name)
85
86
Tony Mack1fbfccc2015-01-08 21:10:55 -050087class Glance(Client):
Siobhan Tully30fd4292013-05-10 08:59:56 -040088 def __init__(self, *args, **kwds):
89 Client.__init__(self, *args, **kwds)
90 if has_openstack:
Sapan Bhatia14356b72014-11-05 10:32:41 -050091 self.client = glanceclient.get_client(host='0.0.0.0',
Siobhan Tully30fd4292013-05-10 08:59:56 -040092 username=self.username,
93 password=self.password,
94 tenant=self.tenant,
95 auth_url=self.url)
96 @require_enabled
97 def __getattr__(self, name):
98 return getattr(self.client, name)
99
Tony Mack1fbfccc2015-01-08 21:10:55 -0500100class GlanceClient(Client):
101 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
Tony Mackf8a1a612014-05-06 23:42:40 -0400102 Client.__init__(self, *args, **kwds)
103 if has_openstack:
Tony Mack1fbfccc2015-01-08 21:10:55 -0500104 self.client = glanceclient.Client(version,
105 endpoint=endpoint,
106 token=token,
107 cacert=cacert
108 )
Tony Mackf8a1a612014-05-06 23:42:40 -0400109
110 @require_enabled
111 def __getattr__(self, name):
112 return getattr(self.client, name)
113
Siobhan Tully30fd4292013-05-10 08:59:56 -0400114class NovaClient(Client):
115 def __init__(self, *args, **kwds):
116 Client.__init__(self, *args, **kwds)
117 if has_openstack:
118 self.client = nova_client.Client(username=self.username,
119 api_key=self.password,
120 project_id=self.tenant,
121 auth_url=self.url,
122 region_name='',
123 extensions=[],
124 service_type='compute',
125 service_name='',
126 )
127
128 @require_enabled
129 def connect(self, *args, **kwds):
130 self.__init__(*args, **kwds)
131
132 @require_enabled
133 def __getattr__(self, name):
134 return getattr(self.client, name)
135
Tony Mackb0d97422013-06-10 09:57:45 -0400136class NovaDB(Client):
137 def __init__(self, *args, **kwds):
138 Client.__init__(self, *args, **kwds)
139 if has_openstack:
140 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400141 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400142 self.client = nova_db_api
143
144
145 @require_enabled
146 def connect(self, *args, **kwds):
147 self.__init__(*args, **kwds)
148
149 @require_enabled
150 def __getattr__(self, name):
151 return getattr(self.client, name)
152
Siobhan Tully30fd4292013-05-10 08:59:56 -0400153class QuantumClient(Client):
154 def __init__(self, *args, **kwds):
155 Client.__init__(self, *args, **kwds)
156 if has_openstack:
157 self.client = quantum_client.Client(username=self.username,
158 password=self.password,
159 tenant_name=self.tenant,
160 auth_url=self.url)
161 @require_enabled
162 def connect(self, *args, **kwds):
163 self.__init__(*args, **kwds)
164
165 @require_enabled
166 def __getattr__(self, name):
167 return getattr(self.client, name)
168
169class OpenStackClient:
170 """
171 A simple native shell to the openstack backend services.
172 This class can receive all nova calls to the underlying testbed
173 """
174
175 def __init__ ( self, *args, **kwds) :
176 # instantiate managers
177 self.keystone = KeystoneClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400178 url_parsed = urlparse.urlparse(self.keystone.url)
179 hostname = url_parsed.netloc.split(':')[0]
180 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
Tony Mack1fbfccc2015-01-08 21:10:55 -0500181 glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
Tony Mackf8a1a612014-05-06 23:42:40 -0400182
Tony Mack1fbfccc2015-01-08 21:10:55 -0500183 self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400184 self.nova = NovaClient(*args, **kwds)
Sapan Bhatia14356b72014-11-05 10:32:41 -0500185 # self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400186 self.quantum = QuantumClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400187
Siobhan Tully30fd4292013-05-10 08:59:56 -0400188
189 @require_enabled
190 def connect(self, *args, **kwds):
191 self.__init__(*args, **kwds)
192
193 @require_enabled
194 def authenticate(self):
195 return self.keystone.authenticate()