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
6 from nova.db.sqlalchemy import api as nova_db_api
7 from nova.context import get_admin_context
8 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
15 from planetstack.config import Config
16 from deployment_auth import deployment_auth
18 def require_enabled(callable):
19 def wrapper(*args, **kwds):
21 return callable(*args, **kwds)
26 def parse_novarc(filename):
28 f = open(filename, 'r')
31 line = line.replace('export', '').strip()
32 parts = line.split('=')
34 value = parts[1].replace("\'", "")
35 value = value.replace('\"', '')
36 opts[parts[0]] = value
43 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
46 if not deployment or deployment not in deployment_auth:
47 auth = deployment_auth['default']
49 auth = deployment_auth[deployment]
51 self.has_openstack = has_openstack
53 self.url = auth['url']
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']
66 self.username = username
68 self.password = password
76 self.endpoint = endpoint
78 #if '@' in self.username:
79 # self.username = self.username[:self.username.index('@')]
83 def get_session(self):
84 return core.Base().get_session()
87 def get_metadata(self):
88 session = self.get_session()
89 return session.query(Metadata).all()
92 class KeystoneClient(Client):
93 def __init__(self, *args, **kwds):
94 Client.__init__(self, *args, **kwds)
96 self.client = keystone_client.Client(username=self.username,
97 password=self.password,
98 tenant_name=self.tenant,
100 endpoint=self.endpoint,
105 def connect(self, *args, **kwds):
106 self.__init__(*args, **kwds)
109 def __getattr__(self, name):
110 return getattr(self.client, name)
113 class GlanceClient(Client):
114 def __init__(self, *args, **kwds):
115 Client.__init__(self, *args, **kwds)
117 self.client = glance_client.get_client(host='0.0.0.0',
118 username=self.username,
119 password=self.password,
123 def __getattr__(self, name):
124 return getattr(self.client, name)
126 class NovaClient(Client):
127 def __init__(self, *args, **kwds):
128 Client.__init__(self, *args, **kwds)
130 self.client = nova_client.Client(username=self.username,
131 api_key=self.password,
132 project_id=self.tenant,
136 service_type='compute',
141 def connect(self, *args, **kwds):
142 self.__init__(*args, **kwds)
145 def __getattr__(self, name):
146 return getattr(self.client, name)
148 class NovaDB(Client):
149 def __init__(self, *args, **kwds):
150 Client.__init__(self, *args, **kwds)
152 self.ctx = get_admin_context()
153 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
154 self.client = nova_db_api
158 def connect(self, *args, **kwds):
159 self.__init__(*args, **kwds)
162 def __getattr__(self, name):
163 return getattr(self.client, name)
165 class QuantumClient(Client):
166 def __init__(self, *args, **kwds):
167 Client.__init__(self, *args, **kwds)
169 self.client = quantum_client.Client(username=self.username,
170 password=self.password,
171 tenant_name=self.tenant,
174 def connect(self, *args, **kwds):
175 self.__init__(*args, **kwds)
178 def __getattr__(self, name):
179 return getattr(self.client, name)
181 class OpenStackClient:
183 A simple native shell to the openstack backend services.
184 This class can receive all nova calls to the underlying testbed
187 def __init__ ( self, *args, **kwds) :
188 # instantiate managers
189 self.keystone = KeystoneClient(*args, **kwds)
190 self.keystone_db = KeystoneDB()
191 self.glance = GlanceClient(*args, **kwds)
192 self.nova = NovaClient(*args, **kwds)
193 self.nova_db = NovaDB(*args, **kwds)
194 self.quantum = QuantumClient(*args, **kwds)
197 def connect(self, *args, **kwds):
198 self.__init__(*args, **kwds)
201 def authenticate(self):
202 return self.keystone.authenticate()