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
17 def require_enabled(callable):
18 def wrapper(*args, **kwds):
20 return callable(*args, **kwds)
25 def parse_novarc(filename):
27 f = open(filename, 'r')
30 line = line.replace('export', '').strip()
31 parts = line.split('=')
33 value = parts[1].replace("\'", "")
34 value = value.replace('\"', '')
35 opts[parts[0]] = value
42 def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds):
44 config = Config(config)
47 self.has_openstack = has_openstack
48 self.username = config.nova_admin_user
49 self.password = config.nova_admin_password
50 self.tenant = config.nova_admin_tenant
51 self.url = config.nova_url
54 self.username = username
56 self.password = password
62 if '@' in self.username:
63 self.username = self.username[:self.username.index('@')]
67 def get_session(self):
68 return core.Base().get_session()
71 def get_metadata(self):
72 session = self.get_session()
73 return session.query(Metadata).all()
76 class KeystoneClient(Client):
77 def __init__(self, *args, **kwds):
78 Client.__init__(self, *args, **kwds)
80 self.client = keystone_client.Client(username=self.username,
81 password=self.password,
82 tenant_name=self.tenant,
86 def connect(self, *args, **kwds):
87 self.__init__(*args, **kwds)
90 def __getattr__(self, name):
91 return getattr(self.client, name)
94 class GlanceClient(Client):
95 def __init__(self, *args, **kwds):
96 Client.__init__(self, *args, **kwds)
98 self.client = glance_client.get_client(host='0.0.0.0',
99 username=self.username,
100 password=self.password,
104 def __getattr__(self, name):
105 return getattr(self.client, name)
107 class NovaClient(Client):
108 def __init__(self, *args, **kwds):
109 Client.__init__(self, *args, **kwds)
111 self.client = nova_client.Client(username=self.username,
112 api_key=self.password,
113 project_id=self.tenant,
117 service_type='compute',
122 def connect(self, *args, **kwds):
123 self.__init__(*args, **kwds)
126 def __getattr__(self, name):
127 return getattr(self.client, name)
129 class NovaDB(Client):
130 def __init__(self, *args, **kwds):
131 Client.__init__(self, *args, **kwds)
133 self.ctx = get_admin_context()
134 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
135 self.client = nova_db_api
139 def connect(self, *args, **kwds):
140 self.__init__(*args, **kwds)
143 def __getattr__(self, name):
144 return getattr(self.client, name)
146 class QuantumClient(Client):
147 def __init__(self, *args, **kwds):
148 Client.__init__(self, *args, **kwds)
150 self.client = quantum_client.Client(username=self.username,
151 password=self.password,
152 tenant_name=self.tenant,
155 def connect(self, *args, **kwds):
156 self.__init__(*args, **kwds)
159 def __getattr__(self, name):
160 return getattr(self.client, name)
162 class OpenStackClient:
164 A simple native shell to the openstack backend services.
165 This class can receive all nova calls to the underlying testbed
168 def __init__ ( self, *args, **kwds) :
169 # instantiate managers
170 self.keystone = KeystoneClient(*args, **kwds)
171 self.keystone_db = KeystoneDB()
172 self.glance = GlanceClient(*args, **kwds)
173 self.nova = NovaClient(*args, **kwds)
174 self.nova_db = NovaDB(*args, **kwds)
175 self.quantum = QuantumClient(*args, **kwds)
178 def connect(self, *args, **kwds):
179 self.__init__(*args, **kwds)
182 def authenticate(self):
183 return self.keystone.authenticate()