blob: 16f6b2e53b4111ac4476665b835db9af0b98b833 [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 Macke4be32f2014-03-11 20:45:25 -040043 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, *args, **kwds):
44
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
51
Siobhan Tully30fd4292013-05-10 08:59:56 -040052 self.has_openstack = has_openstack
Tony Macke4be32f2014-03-11 20:45:25 -040053 self.username = auth['user']
54 self.password = auth['password']
55 self.tenant = auth['tenant']
56 self.url = auth['url']
57 self.endpoint = auth['endpoint']
58 self.token = auth['token']
Siobhan Tully30fd4292013-05-10 08:59:56 -040059
60 if username:
61 self.username = username
62 if password:
63 self.password = password
64 if tenant:
65 self.tenant = tenant
66 if url:
67 self.url = url
Tony Macke4be32f2014-03-11 20:45:25 -040068 if token:
69 self.token = token
70 if endpoint:
71 self.endpoint = endpoint
Siobhan Tully30fd4292013-05-10 08:59:56 -040072
Tony Mack976d7742014-03-18 22:00:52 -040073 #if '@' in self.username:
74 # self.username = self.username[:self.username.index('@')]
Siobhan Tully30fd4292013-05-10 08:59:56 -040075
Tony Mack79a49c82013-06-15 23:51:57 -040076class KeystoneDB:
77 @require_enabled
78 def get_session(self):
79 return core.Base().get_session()
80
81 @require_enabled
82 def get_metadata(self):
83 session = self.get_session()
84 return session.query(Metadata).all()
85
86
Siobhan Tully30fd4292013-05-10 08:59:56 -040087class KeystoneClient(Client):
88 def __init__(self, *args, **kwds):
89 Client.__init__(self, *args, **kwds)
90 if has_openstack:
91 self.client = keystone_client.Client(username=self.username,
92 password=self.password,
93 tenant_name=self.tenant,
Tony Macke4be32f2014-03-11 20:45:25 -040094 auth_url=self.url,
95 endpoint=self.endpoint,
96 token=self.token
97 )
Siobhan Tully30fd4292013-05-10 08:59:56 -040098
99 @require_enabled
100 def connect(self, *args, **kwds):
101 self.__init__(*args, **kwds)
102
103 @require_enabled
104 def __getattr__(self, name):
105 return getattr(self.client, name)
106
107
108class GlanceClient(Client):
109 def __init__(self, *args, **kwds):
110 Client.__init__(self, *args, **kwds)
111 if has_openstack:
112 self.client = glance_client.get_client(host='0.0.0.0',
113 username=self.username,
114 password=self.password,
115 tenant=self.tenant,
116 auth_url=self.url)
117 @require_enabled
118 def __getattr__(self, name):
119 return getattr(self.client, name)
120
121class NovaClient(Client):
122 def __init__(self, *args, **kwds):
123 Client.__init__(self, *args, **kwds)
124 if has_openstack:
125 self.client = nova_client.Client(username=self.username,
126 api_key=self.password,
127 project_id=self.tenant,
128 auth_url=self.url,
129 region_name='',
130 extensions=[],
131 service_type='compute',
132 service_name='',
133 )
134
135 @require_enabled
136 def connect(self, *args, **kwds):
137 self.__init__(*args, **kwds)
138
139 @require_enabled
140 def __getattr__(self, name):
141 return getattr(self.client, name)
142
Tony Mackb0d97422013-06-10 09:57:45 -0400143class NovaDB(Client):
144 def __init__(self, *args, **kwds):
145 Client.__init__(self, *args, **kwds)
146 if has_openstack:
147 self.ctx = get_admin_context()
Tony Mack7b0dad02013-06-10 13:42:21 -0400148 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
Tony Mackb0d97422013-06-10 09:57:45 -0400149 self.client = nova_db_api
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
Siobhan Tully30fd4292013-05-10 08:59:56 -0400160class QuantumClient(Client):
161 def __init__(self, *args, **kwds):
162 Client.__init__(self, *args, **kwds)
163 if has_openstack:
164 self.client = quantum_client.Client(username=self.username,
165 password=self.password,
166 tenant_name=self.tenant,
167 auth_url=self.url)
168 @require_enabled
169 def connect(self, *args, **kwds):
170 self.__init__(*args, **kwds)
171
172 @require_enabled
173 def __getattr__(self, name):
174 return getattr(self.client, name)
175
176class OpenStackClient:
177 """
178 A simple native shell to the openstack backend services.
179 This class can receive all nova calls to the underlying testbed
180 """
181
182 def __init__ ( self, *args, **kwds) :
183 # instantiate managers
184 self.keystone = KeystoneClient(*args, **kwds)
Tony Mack79a49c82013-06-15 23:51:57 -0400185 self.keystone_db = KeystoneDB()
Siobhan Tully30fd4292013-05-10 08:59:56 -0400186 self.glance = GlanceClient(*args, **kwds)
187 self.nova = NovaClient(*args, **kwds)
Tony Mackb0d97422013-06-10 09:57:45 -0400188 self.nova_db = NovaDB(*args, **kwds)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400189 self.quantum = QuantumClient(*args, **kwds)
190
191 @require_enabled
192 def connect(self, *args, **kwds):
193 self.__init__(*args, **kwds)
194
195 @require_enabled
196 def authenticate(self):
197 return self.keystone.authenticate()