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, *args, **kwds):
46 if not deployment or deployment not in deployment_auth:
47 auth = deployment_auth['default']
49 auth = deployment_auth[deployment]
52 self.has_openstack = has_openstack
53 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']
61 self.username = username
63 self.password = password
71 self.endpoint = endpoint
73 if '@' in self.username:
74 self.username = self.username[:self.username.index('@')]
78 def get_session(self):
79 return core.Base().get_session()
82 def get_metadata(self):
83 session = self.get_session()
84 return session.query(Metadata).all()
87 class KeystoneClient(Client):
88 def __init__(self, *args, **kwds):
89 Client.__init__(self, *args, **kwds)
91 self.client = keystone_client.Client(username=self.username,
92 password=self.password,
93 tenant_name=self.tenant,
95 endpoint=self.endpoint,
100 def connect(self, *args, **kwds):
101 self.__init__(*args, **kwds)
104 def __getattr__(self, name):
105 return getattr(self.client, name)
108 class GlanceClient(Client):
109 def __init__(self, *args, **kwds):
110 Client.__init__(self, *args, **kwds)
112 self.client = glance_client.get_client(host='0.0.0.0',
113 username=self.username,
114 password=self.password,
118 def __getattr__(self, name):
119 return getattr(self.client, name)
121 class NovaClient(Client):
122 def __init__(self, *args, **kwds):
123 Client.__init__(self, *args, **kwds)
125 self.client = nova_client.Client(username=self.username,
126 api_key=self.password,
127 project_id=self.tenant,
131 service_type='compute',
136 def connect(self, *args, **kwds):
137 self.__init__(*args, **kwds)
140 def __getattr__(self, name):
141 return getattr(self.client, name)
143 class NovaDB(Client):
144 def __init__(self, *args, **kwds):
145 Client.__init__(self, *args, **kwds)
147 self.ctx = get_admin_context()
148 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
149 self.client = nova_db_api
153 def connect(self, *args, **kwds):
154 self.__init__(*args, **kwds)
157 def __getattr__(self, name):
158 return getattr(self.client, name)
160 class QuantumClient(Client):
161 def __init__(self, *args, **kwds):
162 Client.__init__(self, *args, **kwds)
164 self.client = quantum_client.Client(username=self.username,
165 password=self.password,
166 tenant_name=self.tenant,
169 def connect(self, *args, **kwds):
170 self.__init__(*args, **kwds)
173 def __getattr__(self, name):
174 return getattr(self.client, name)
176 class OpenStackClient:
178 A simple native shell to the openstack backend services.
179 This class can receive all nova calls to the underlying testbed
182 def __init__ ( self, *args, **kwds) :
183 # instantiate managers
184 self.keystone = KeystoneClient(*args, **kwds)
185 self.keystone_db = KeystoneDB()
186 self.glance = GlanceClient(*args, **kwds)
187 self.nova = NovaClient(*args, **kwds)
188 self.nova_db = NovaDB(*args, **kwds)
189 self.quantum = QuantumClient(*args, **kwds)
192 def connect(self, *args, **kwds):
193 self.__init__(*args, **kwds)
196 def authenticate(self):
197 return self.keystone.authenticate()