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 neutronclient.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
14 from planetstack.config import Config
16 def require_enabled(callable):
17 def wrapper(*args, **kwds):
19 return callable(*args, **kwds)
24 def parse_novarc(filename):
26 f = open(filename, 'r')
29 line = line.replace('export', '').strip()
30 parts = line.split('=')
32 value = parts[1].replace("\'", "")
33 value = value.replace('\"', '')
34 opts[parts[0]] = value
41 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, admin=True, *args, **kwds):
43 deployment = controller
45 self.has_openstack = has_openstack
46 self.url = deployment.auth_url
48 self.username = deployment.admin_user
49 self.password = deployment.admin_password
50 self.tenant = deployment.admin_tenant
57 self.username = username
59 self.password = password
67 self.endpoint = endpoint
69 #if '@' in self.username:
70 # self.username = self.username[:self.username.index('@')]
72 class KeystoneClient(Client):
73 def __init__(self, *args, **kwds):
74 Client.__init__(self, *args, **kwds)
76 self.client = keystone_client.Client(username=self.username,
77 password=self.password,
78 tenant_name=self.tenant,
83 def connect(self, *args, **kwds):
84 self.__init__(*args, **kwds)
87 def __getattr__(self, name):
88 return getattr(self.client, name)
91 class GlanceClient(Client):
92 def __init__(self, *args, **kwds):
93 Client.__init__(self, *args, **kwds)
95 self.client = glanceclient.get_client(host='0.0.0.0',
96 username=self.username,
97 password=self.password,
101 def __getattr__(self, name):
102 return getattr(self.client, name)
104 class GlanceClientNew(Client):
105 def __init__(self, version, endpoint, token, *args, **kwds):
106 Client.__init__(self, *args, **kwds)
108 self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
111 def __getattr__(self, name):
112 return getattr(self.client, name)
114 class NovaClient(Client):
115 def __init__(self, *args, **kwds):
116 Client.__init__(self, *args, **kwds)
118 self.client = nova_client.Client(username=self.username,
119 api_key=self.password,
120 project_id=self.tenant,
124 service_type='compute',
129 def connect(self, *args, **kwds):
130 self.__init__(*args, **kwds)
133 def __getattr__(self, name):
134 return getattr(self.client, name)
136 class NovaDB(Client):
137 def __init__(self, *args, **kwds):
138 Client.__init__(self, *args, **kwds)
140 self.ctx = get_admin_context()
141 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
142 self.client = nova_db_api
146 def connect(self, *args, **kwds):
147 self.__init__(*args, **kwds)
150 def __getattr__(self, name):
151 return getattr(self.client, name)
153 class QuantumClient(Client):
154 def __init__(self, *args, **kwds):
155 Client.__init__(self, *args, **kwds)
157 self.client = quantum_client.Client(username=self.username,
158 password=self.password,
159 tenant_name=self.tenant,
162 def connect(self, *args, **kwds):
163 self.__init__(*args, **kwds)
166 def __getattr__(self, name):
167 return getattr(self.client, name)
169 class OpenStackClient:
171 A simple native shell to the openstack backend services.
172 This class can receive all nova calls to the underlying testbed
175 def __init__ ( self, *args, **kwds) :
176 # instantiate managers
177 self.keystone = KeystoneClient(*args, **kwds)
178 url_parsed = urlparse.urlparse(self.keystone.url)
179 hostname = url_parsed.netloc.split(':')[0]
180 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
181 #self.glance = GlanceClient(*args, **kwds)
183 self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id, **kwds)
184 self.nova = NovaClient(*args, **kwds)
185 # self.nova_db = NovaDB(*args, **kwds)
186 self.quantum = QuantumClient(*args, **kwds)
190 def connect(self, *args, **kwds):
191 self.__init__(*args, **kwds)
194 def authenticate(self):
195 return self.keystone.authenticate()