blob: c543947106ef8f09614680819e3795d56c10972c [file] [log] [blame]
Siobhan Tully30fd4292013-05-10 08:59:56 -04001try:
2 from keystoneclient.v2_0 import client as keystone_client
3 from glance import client as glance_client
4 from novaclient.v1_1 import client as nova_client
5 from quantumclient.v2_0 import client as quantum_client
Tony Mackb0d97422013-06-10 09:57:45 -04006 from nova.db.sqlalchemy import api as nova_db_api
7 from nova.context import get_admin_context
Tony Mack79a49c82013-06-15 23:51:57 -04008 from keystone.common.sql import core
9 core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf'])
10 from keystone.identity.backends.sql import Metadata
Siobhan Tully30fd4292013-05-10 08:59:56 -040011 has_openstack = True
12except:
13 has_openstack = False
14
15from planetstack.config import Config
Tony Macke4be32f2014-03-11 20:45:25 -040016from deployment_auth import deployment_auth
Siobhan Tully30fd4292013-05-10 08:59:56 -040017
18def require_enabled(callable):
19 def wrapper(*args, **kwds):
20 if has_openstack:
21 return callable(*args, **kwds)
22 else:
23 return None
24 return wrapper
25
26def parse_novarc(filename):
27 opts = {}
28 f = open(filename, 'r')
29 for line in f:
30 try:
31 line = line.replace('export', '').strip()
32 parts = line.split('=')
33 if len(parts) > 1:
34 value = parts[1].replace("\'", "")
35 value = value.replace('\"', '')
36 opts[parts[0]] = value
37 except:
38 pass
39 f.close()
40 return opts
41
42class Client:
Tony Mack49e839c2014-04-07 19:49:01 -040043 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 -040044
45
46 if not deployment or deployment not in deployment_auth:
47 auth = deployment_auth['default']
Siobhan Tully30fd4292013-05-10 08:59:56 -040048 else:
Tony Macke4be32f2014-03-11 20:45:25 -040049 auth = deployment_auth[deployment]
50
Siobhan Tully30fd4292013-05-10 08:59:56 -040051 self.has_openstack = has_openstack
Tony Mack49e839c2014-04-07 19:49:01 -040052
Tony Macke4be32f2014-03-11 20:45:25 -040053 self.url = auth['url']
Tony Mack49e839c2014-04-07 19:49:01 -040054 if admin:
55 self.username = auth['user']
56 self.password = auth['password']
57 self.tenant = auth['tenant']
58 self.endpoint = auth['endpoint']
59 self.token = auth['token']
60 else:
61 self.username = None
62 self.password = None
63 self.tenant = None
Siobhan Tully30fd4292013-05-10 08:59:56 -040064
65 if username:
66 self.username = username
67 if password:
68 self.password = password
69 if tenant:
70 self.tenant = tenant
71 if url:
72 self.url = url
Tony Macke4be32f2014-03-11 20:45:25 -040073 if token:
74 self.token = token
75 if endpoint:
76 self.endpoint = endpoint
Siobhan Tully30fd4292013-05-10 08:59:56 -040077
Tony Mack976d7742014-03-18 22:00:52 -040078 #if '@' in self.username:
79 # self.username = self.username[:self.username.index('@')]
Siobhan Tully30fd4292013-05-10 08:59:56 -040080
Tony Mack79a49c82013-06-15 23:51:57 -040081class KeystoneDB:
82 @require_enabled
83 def get_session(self):
84 return core.Base().get_session()
85
86 @require_enabled
87 def get_metadata(self):
88 session = self.get_session()
89 return session.query(Metadata).all()
90
91
Siobhan Tully30fd4292013-05-10 08:59:56 -040092class KeystoneClient(Client):
93 def __init__(self, *args, **kwds):
94 Client.__init__(self, *args, **kwds)
95 if has_openstack:
96 self.client = keystone_client.Client(username=self.username,
97 password=self.password,
98 tenant_name=self.tenant,
Tony Macke4be32f2014-03-11 20:45:25 -040099 auth_url=self.url,
100 endpoint=self.endpoint,
101 token=self.token
102 )
Siobhan Tully30fd4292013-05-10 08:59:56 -0400103
104 @require_enabled
105 def connect(self, *args, **kwds):
106 self.__init__(*args, **kwds)
107
108 @require_enabled
109 def __getattr__(self, name):
110 return getattr(self.client, name)
111
112
113class GlanceClient(Client):
114 def __init__(self, *args, **kwds):
115 Client.__init__(self, *args, **kwds)
116 if has_openstack:
117 self.client = glance_client.get_client(host='0.0.0.0',
118 username=self.username,
119 password=self.password,
120 tenant=self.tenant,
121 auth_url=self.url)
122 @require_enabled
123 def __getattr__(self, name):
124 return getattr(self.client, name)
125
126class NovaClient(Client):
127 def __init__(self, *args, **kwds):
128 Client.__init__(self, *args, **kwds)
129 if has_openstack:
130 self.client = nova_client.Client(username=self.username,
131 api_key=self.password,
132 project_id=self.tenant,
133 auth_url=self.url,
134 region_name='',
135 extensions=[],
136 service_type='compute',
137 service_name='',
138 )
139
140 @require_enabled
141 def connect(self, *args, **kwds):
142 self.__init__(*args, **kwds)
143
144 @require_enabled
145 def __getattr__(self, name):
146 return getattr(self.client, name)
147
Tony Mackb0d97422013-06-10 09:57:45 -0400148class NovaDB(Client):
149 def __init__(self, *args, **kwds):
150 Client.__init__(self, *args, **kwds)
151 if has_openstack:
152 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400153 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400154 self.client = nova_db_api
155
156
157 @require_enabled
158 def connect(self, *args, **kwds):
159 self.__init__(*args, **kwds)
160
161 @require_enabled
162 def __getattr__(self, name):
163 return getattr(self.client, name)
164
Siobhan Tully30fd4292013-05-10 08:59:56 -0400165class QuantumClient(Client):
166 def __init__(self, *args, **kwds):
167 Client.__init__(self, *args, **kwds)
168 if has_openstack:
169 self.client = quantum_client.Client(username=self.username,
170 password=self.password,
171 tenant_name=self.tenant,
172 auth_url=self.url)
173 @require_enabled
174 def connect(self, *args, **kwds):
175 self.__init__(*args, **kwds)
176
177 @require_enabled
178 def __getattr__(self, name):
179 return getattr(self.client, name)
180
181class OpenStackClient:
182 """
183 A simple native shell to the openstack backend services.
184 This class can receive all nova calls to the underlying testbed
185 """
186
187 def __init__ ( self, *args, **kwds) :
188 # instantiate managers
189 self.keystone = KeystoneClient(*args, **kwds)
Tony Mack79a49c82013-06-15 23:51:57 -0400190 self.keystone_db = KeystoneDB()
Siobhan Tully30fd4292013-05-10 08:59:56 -0400191 self.glance = GlanceClient(*args, **kwds)
192 self.nova = NovaClient(*args, **kwds)
Tony Mackb0d97422013-06-10 09:57:45 -0400193 self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400194 self.quantum = QuantumClient(*args, **kwds)
195
196 @require_enabled
197 def connect(self, *args, **kwds):
198 self.__init__(*args, **kwds)
199
200 @require_enabled
201 def authenticate(self):
202 return self.keystone.authenticate()