3 from keystoneclient.v2_0 import client as keystone_client
4 from glance import client as glance_client
6 from novaclient.v1_1 import client as nova_client
7 from quantumclient.v2_0 import client as quantum_client
8 from nova.db.sqlalchemy import api as nova_db_api
9 from nova.context import get_admin_context
10 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
17 from planetstack.config import Config
18 from deployment_auth import deployment_auth
20 def require_enabled(callable):
21 def wrapper(*args, **kwds):
23 return callable(*args, **kwds)
28 def parse_novarc(filename):
30 f = open(filename, 'r')
33 line = line.replace('export', '').strip()
34 parts = line.split('=')
36 value = parts[1].replace("\'", "")
37 value = value.replace('\"', '')
38 opts[parts[0]] = value
45 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
48 if not deployment or deployment not in deployment_auth:
49 auth = deployment_auth['default']
51 auth = deployment_auth[deployment]
53 self.has_openstack = has_openstack
55 self.url = auth['url']
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']
68 self.username = username
70 self.password = password
78 self.endpoint = endpoint
80 #if '@' in self.username:
81 # self.username = self.username[:self.username.index('@')]
85 def get_session(self):
86 return core.Base().get_session()
89 def get_metadata(self):
90 session = self.get_session()
91 return session.query(Metadata).all()
94 class KeystoneClient(Client):
95 def __init__(self, *args, **kwds):
96 Client.__init__(self, *args, **kwds)
98 self.client = keystone_client.Client(username=self.username,
99 password=self.password,
100 tenant_name=self.tenant,
102 endpoint=self.endpoint,
107 def connect(self, *args, **kwds):
108 self.__init__(*args, **kwds)
111 def __getattr__(self, name):
112 return getattr(self.client, name)
115 class GlanceClient(Client):
116 def __init__(self, *args, **kwds):
117 Client.__init__(self, *args, **kwds)
119 self.client = glance_client.get_client(host='0.0.0.0',
120 username=self.username,
121 password=self.password,
125 def __getattr__(self, name):
126 return getattr(self.client, name)
128 class GlanceClientNew(Client):
129 def __init__(self, version, endpoint, token, *args, **kwds):
130 Client.__init__(self, *args, **kwds)
132 self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
135 def __getattr__(self, name):
136 return getattr(self.client, name)
138 class NovaClient(Client):
139 def __init__(self, *args, **kwds):
140 Client.__init__(self, *args, **kwds)
142 self.client = nova_client.Client(username=self.username,
143 api_key=self.password,
144 project_id=self.tenant,
148 service_type='compute',
153 def connect(self, *args, **kwds):
154 self.__init__(*args, **kwds)
157 def __getattr__(self, name):
158 return getattr(self.client, name)
160 class NovaDB(Client):
161 def __init__(self, *args, **kwds):
162 Client.__init__(self, *args, **kwds)
164 self.ctx = get_admin_context()
165 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
166 self.client = nova_db_api
170 def connect(self, *args, **kwds):
171 self.__init__(*args, **kwds)
174 def __getattr__(self, name):
175 return getattr(self.client, name)
177 class QuantumClient(Client):
178 def __init__(self, *args, **kwds):
179 Client.__init__(self, *args, **kwds)
181 self.client = quantum_client.Client(username=self.username,
182 password=self.password,
183 tenant_name=self.tenant,
186 def connect(self, *args, **kwds):
187 self.__init__(*args, **kwds)
190 def __getattr__(self, name):
191 return getattr(self.client, name)
193 class OpenStackClient:
195 A simple native shell to the openstack backend services.
196 This class can receive all nova calls to the underlying testbed
199 def __init__ ( self, *args, **kwds) :
200 # instantiate managers
201 self.keystone = KeystoneClient(*args, **kwds)
202 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)
205 self.keystone_db = KeystoneDB()
206 self.glance = GlanceClient(*args, **kwds)
208 self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id)
209 self.nova = NovaClient(*args, **kwds)
210 self.nova_db = NovaDB(*args, **kwds)
211 self.quantum = QuantumClient(*args, **kwds)
215 def connect(self, *args, **kwds):
216 self.__init__(*args, **kwds)
219 def authenticate(self):
220 return self.keystone.authenticate()