blob: 6974dad479e10ed0d29f37f4d81ff8a079fd2145 [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
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
Siobhan Tully30fd4292013-05-10 08:59:56 -040010 has_openstack = True
11except:
12 has_openstack = False
13
14from planetstack.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:
Tony Mack49e839c2014-04-07 19:49:01 -040041 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
Tony Mack94466922014-06-19 20:09:40 -040042
Siobhan Tully30fd4292013-05-10 08:59:56 -040043 self.has_openstack = has_openstack
Tony Mackdd240952014-06-03 23:02:00 -040044 self.url = deployment.auth_url
Tony Mack49e839c2014-04-07 19:49:01 -040045 if admin:
Tony Mackdd240952014-06-03 23:02:00 -040046 self.username = deployment.admin_user
47 self.password = deployment.admin_password
48 self.tenant = deployment.admin_tenant
Tony Mack49e839c2014-04-07 19:49:01 -040049 else:
50 self.username = None
51 self.password = None
52 self.tenant = None
Siobhan Tully30fd4292013-05-10 08:59:56 -040053
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
Tony Macke4be32f2014-03-11 20:45:25 -040062 if token:
63 self.token = token
64 if endpoint:
65 self.endpoint = endpoint
Siobhan Tully30fd4292013-05-10 08:59:56 -040066
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
89class GlanceClient(Client):
90 def __init__(self, *args, **kwds):
91 Client.__init__(self, *args, **kwds)
92 if has_openstack:
Sapan Bhatia14356b72014-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 Mackf8a1a612014-05-06 23:42:40 -0400102class GlanceClientNew(Client):
103 def __init__(self, version, endpoint, token, *args, **kwds):
104 Client.__init__(self, *args, **kwds)
105 if has_openstack:
106 self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
107
108 @require_enabled
109 def __getattr__(self, name):
110 return getattr(self.client, name)
111
Siobhan Tully30fd4292013-05-10 08:59:56 -0400112class NovaClient(Client):
113 def __init__(self, *args, **kwds):
114 Client.__init__(self, *args, **kwds)
115 if has_openstack:
116 self.client = nova_client.Client(username=self.username,
117 api_key=self.password,
118 project_id=self.tenant,
119 auth_url=self.url,
120 region_name='',
121 extensions=[],
122 service_type='compute',
123 service_name='',
124 )
125
126 @require_enabled
127 def connect(self, *args, **kwds):
128 self.__init__(*args, **kwds)
129
130 @require_enabled
131 def __getattr__(self, name):
132 return getattr(self.client, name)
133
Tony Mackb0d97422013-06-10 09:57:45 -0400134class NovaDB(Client):
135 def __init__(self, *args, **kwds):
136 Client.__init__(self, *args, **kwds)
137 if has_openstack:
138 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400139 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400140 self.client = nova_db_api
141
142
143 @require_enabled
144 def connect(self, *args, **kwds):
145 self.__init__(*args, **kwds)
146
147 @require_enabled
148 def __getattr__(self, name):
149 return getattr(self.client, name)
150
Siobhan Tully30fd4292013-05-10 08:59:56 -0400151class QuantumClient(Client):
152 def __init__(self, *args, **kwds):
153 Client.__init__(self, *args, **kwds)
154 if has_openstack:
155 self.client = quantum_client.Client(username=self.username,
156 password=self.password,
157 tenant_name=self.tenant,
158 auth_url=self.url)
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 OpenStackClient:
168 """
169 A simple native shell to the openstack backend services.
170 This class can receive all nova calls to the underlying testbed
171 """
172
173 def __init__ ( self, *args, **kwds) :
174 # instantiate managers
175 self.keystone = KeystoneClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400176 url_parsed = urlparse.urlparse(self.keystone.url)
177 hostname = url_parsed.netloc.split(':')[0]
178 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
Sapan Bhatia14356b72014-11-05 10:32:41 -0500179 #self.glance = GlanceClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400180
Tony Mack94466922014-06-19 20:09:40 -0400181 self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400182 self.nova = NovaClient(*args, **kwds)
Sapan Bhatia14356b72014-11-05 10:32:41 -0500183 # self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400184 self.quantum = QuantumClient(*args, **kwds)
Tony Mackf8a1a612014-05-06 23:42:40 -0400185
Siobhan Tully30fd4292013-05-10 08:59:56 -0400186
187 @require_enabled
188 def connect(self, *args, **kwds):
189 self.__init__(*args, **kwds)
190
191 @require_enabled
192 def authenticate(self):
193 return self.keystone.authenticate()