blob: 465425d9fb3a5c44c40c0fe91d278d98066c5c2b [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
4 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
7 from quantumclient.v2_0 import client as quantum_client
Tony Mackb0d97422013-06-10 09:57:45 -04008 from nova.db.sqlalchemy import api as nova_db_api
9 from nova.context import get_admin_context
Tony Mack79a49c82013-06-15 23:51:57 -040010 from keystone.common.sql import core
11 core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf'])
12 from keystone.identity.backends.sql import Metadata
Siobhan Tully30fd4292013-05-10 08:59:56 -040013 has_openstack = True
14except:
15 has_openstack = False
16
17from planetstack.config import Config
Tony Macke4be32f2014-03-11 20:45:25 -040018from deployment_auth import deployment_auth
Siobhan Tully30fd4292013-05-10 08:59:56 -040019
20def require_enabled(callable):
21 def wrapper(*args, **kwds):
22 if has_openstack:
23 return callable(*args, **kwds)
24 else:
25 return None
26 return wrapper
27
28def parse_novarc(filename):
29 opts = {}
30 f = open(filename, 'r')
31 for line in f:
32 try:
33 line = line.replace('export', '').strip()
34 parts = line.split('=')
35 if len(parts) > 1:
36 value = parts[1].replace("\'", "")
37 value = value.replace('\"', '')
38 opts[parts[0]] = value
39 except:
40 pass
41 f.close()
42 return opts
43
44class Client:
Tony Mack49e839c2014-04-07 19:49:01 -040045 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
Tony Macke4be32f2014-03-11 20:45:25 -040046
47
48 if not deployment or deployment not in deployment_auth:
49 auth = deployment_auth['default']
Siobhan Tully30fd4292013-05-10 08:59:56 -040050 else:
Tony Macke4be32f2014-03-11 20:45:25 -040051 auth = deployment_auth[deployment]
52
Siobhan Tully30fd4292013-05-10 08:59:56 -040053 self.has_openstack = has_openstack
Tony Mack49e839c2014-04-07 19:49:01 -040054
Tony Macke4be32f2014-03-11 20:45:25 -040055 self.url = auth['url']
Tony Mack49e839c2014-04-07 19:49:01 -040056 if admin:
57 self.username = auth['user']
58 self.password = auth['password']
59 self.tenant = auth['tenant']
60 self.endpoint = auth['endpoint']
61 self.token = auth['token']
62 else:
63 self.username = None
64 self.password = None
65 self.tenant = None
Siobhan Tully30fd4292013-05-10 08:59:56 -040066
67 if username:
68 self.username = username
69 if password:
70 self.password = password
71 if tenant:
72 self.tenant = tenant
73 if url:
74 self.url = url
Tony Macke4be32f2014-03-11 20:45:25 -040075 if token:
76 self.token = token
77 if endpoint:
78 self.endpoint = endpoint
Siobhan Tully30fd4292013-05-10 08:59:56 -040079
Tony Mack976d7742014-03-18 22:00:52 -040080 #if '@' in self.username:
81 # self.username = self.username[:self.username.index('@')]
Siobhan Tully30fd4292013-05-10 08:59:56 -040082
Tony Mack79a49c82013-06-15 23:51:57 -040083class KeystoneDB:
84 @require_enabled
85 def get_session(self):
86 return core.Base().get_session()
87
88 @require_enabled
89 def get_metadata(self):
90 session = self.get_session()
91 return session.query(Metadata).all()
92
93
Siobhan Tully30fd4292013-05-10 08:59:56 -040094class KeystoneClient(Client):
95 def __init__(self, *args, **kwds):
96 Client.__init__(self, *args, **kwds)
97 if has_openstack:
98 self.client = keystone_client.Client(username=self.username,
99 password=self.password,
100 tenant_name=self.tenant,
Tony Macke4be32f2014-03-11 20:45:25 -0400101 auth_url=self.url,
102 endpoint=self.endpoint,
103 token=self.token
104 )
Siobhan Tully30fd4292013-05-10 08:59:56 -0400105
106 @require_enabled
107 def connect(self, *args, **kwds):
108 self.__init__(*args, **kwds)
109
110 @require_enabled
111 def __getattr__(self, name):
112 return getattr(self.client, name)
113
114
115class GlanceClient(Client):
116 def __init__(self, *args, **kwds):
117 Client.__init__(self, *args, **kwds)
118 if has_openstack:
119 self.client = glance_client.get_client(host='0.0.0.0',
120 username=self.username,
121 password=self.password,
122 tenant=self.tenant,
123 auth_url=self.url)
124 @require_enabled
125 def __getattr__(self, name):
126 return getattr(self.client, name)
127
Tony Mackf8a1a612014-05-06 23:42:40 -0400128class GlanceClientNew(Client):
129 def __init__(self, version, endpoint, token, *args, **kwds):
130 Client.__init__(self, *args, **kwds)
131 if has_openstack:
132 self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
133
134 @require_enabled
135 def __getattr__(self, name):
136 return getattr(self.client, name)
137
Siobhan Tully30fd4292013-05-10 08:59:56 -0400138class NovaClient(Client):
139 def __init__(self, *args, **kwds):
140 Client.__init__(self, *args, **kwds)
141 if has_openstack:
142 self.client = nova_client.Client(username=self.username,
143 api_key=self.password,
144 project_id=self.tenant,
145 auth_url=self.url,
146 region_name='',
147 extensions=[],
148 service_type='compute',
149 service_name='',
150 )
151
152 @require_enabled
153 def connect(self, *args, **kwds):
154 self.__init__(*args, **kwds)
155
156 @require_enabled
157 def __getattr__(self, name):
158 return getattr(self.client, name)
159
Tony Mackb0d97422013-06-10 09:57:45 -0400160class NovaDB(Client):
161 def __init__(self, *args, **kwds):
162 Client.__init__(self, *args, **kwds)
163 if has_openstack:
164 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400165 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400166 self.client = nova_db_api
167
168
169 @require_enabled
170 def connect(self, *args, **kwds):
171 self.__init__(*args, **kwds)
172
173 @require_enabled
174 def __getattr__(self, name):
175 return getattr(self.client, name)
176
Siobhan Tully30fd4292013-05-10 08:59:56 -0400177class QuantumClient(Client):
178 def __init__(self, *args, **kwds):
179 Client.__init__(self, *args, **kwds)
180 if has_openstack:
181 self.client = quantum_client.Client(username=self.username,
182 password=self.password,
183 tenant_name=self.tenant,
184 auth_url=self.url)
185 @require_enabled
186 def connect(self, *args, **kwds):
187 self.__init__(*args, **kwds)
188
189 @require_enabled
190 def __getattr__(self, name):
191 return getattr(self.client, name)
192
193class OpenStackClient:
194 """
195 A simple native shell to the openstack backend services.
196 This class can receive all nova calls to the underlying testbed
197 """
198
199 def __init__ ( self, *args, **kwds) :
200 # instantiate managers
201 self.keystone = KeystoneClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400202 url_parsed = urlparse.urlparse(self.keystone.url)
203 hostname = url_parsed.netloc.split(':')[0]
204 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
Tony Mack79a49c82013-06-15 23:51:57 -0400205 self.keystone_db = KeystoneDB()
Siobhan Tully30fd4292013-05-10 08:59:56 -0400206 self.glance = GlanceClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400207
208 self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400209 self.nova = NovaClient(*args, **kwds)
Tony Mackb0d97422013-06-10 09:57:45 -0400210 self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400211 self.quantum = QuantumClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400212
Siobhan Tully30fd4292013-05-10 08:59:56 -0400213
214 @require_enabled
215 def connect(self, *args, **kwds):
216 self.__init__(*args, **kwds)
217
218 @require_enabled
219 def authenticate(self):
220 return self.keystone.authenticate()