bugfixes and cleanup
[plstackapi.git] / planetstack / openstack / client.py
index c6b6b16..72c5cb4 100644 (file)
@@ -1,19 +1,15 @@
+import urlparse
 try:
     from keystoneclient.v2_0 import client as keystone_client
-    from glance import client as glance_client
+    #from glance import client as glance_client
+    import glanceclient
     from novaclient.v1_1 import client as nova_client
-    from quantumclient.v2_0 import client as quantum_client
-    from nova.db.sqlalchemy import api as nova_db_api 
-    from nova.context import get_admin_context
-    from keystone.common.sql import core  
-    core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf'])
-    from keystone.identity.backends.sql import Metadata
+    from neutronclient.v2_0 import client as quantum_client
     has_openstack = True
 except:
     has_openstack = False
 
 from planetstack.config import Config
-from deployment_auth import deployment_auth
 
 def require_enabled(callable):
     def wrapper(*args, **kwds):
@@ -40,22 +36,18 @@ def parse_novarc(filename):
     return opts
 
 class Client:
-    def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, *args, **kwds):
-        
-            
-        if not deployment or deployment not in deployment_auth:
-            auth = deployment_auth['default']
-        else:
-            auth = deployment_auth[deployment]
-            
-            
+    def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, admin=True, *args, **kwds):
+       
         self.has_openstack = has_openstack
-        self.username = auth['user']
-        self.password = auth['password']
-        self.tenant = auth['tenant']
-        self.url = auth['url']
-        self.endpoint = auth['endpoint']
-        self.token = auth['token']  
+        self.url = controller.auth_url
+        if admin:
+            self.username = controller.admin_user
+            self.password = controller.admin_password
+            self.tenant = controller.admin_tenant
+        else:
+            self.username = None
+            self.password = None
+            self.tenant = None
 
         if username:
             self.username = username
@@ -70,19 +62,8 @@ class Client:
         if endpoint:
             self.endpoint = endpoint
 
-        if '@' in self.username:
-            self.username = self.username[:self.username.index('@')]
-
-class KeystoneDB:
-    @require_enabled
-    def get_session(self):
-        return core.Base().get_session()
-
-    @require_enabled
-    def get_metadata(self):
-        session = self.get_session()
-        return session.query(Metadata).all()     
-
+        #if '@' in self.username:
+        #    self.username = self.username[:self.username.index('@')]
 
 class KeystoneClient(Client):
     def __init__(self, *args, **kwds):
@@ -91,9 +72,7 @@ class KeystoneClient(Client):
             self.client = keystone_client.Client(username=self.username,
                                                  password=self.password,
                                                  tenant_name=self.tenant,
-                                                 auth_url=self.url,
-                                                 endpoint=self.endpoint,
-                                                 token=self.token
+                                                 auth_url=self.url
                                                 )
 
     @require_enabled
@@ -109,7 +88,7 @@ class GlanceClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
         if has_openstack:
-            self.client = glance_client.get_client(host='0.0.0.0',
+            self.client = glanceclient.get_client(host='0.0.0.0',
                                                    username=self.username,
                                                    password=self.password,
                                                    tenant=self.tenant,
@@ -118,6 +97,16 @@ class GlanceClient(Client):
     def __getattr__(self, name):
         return getattr(self.client, name)
 
+class GlanceClientNew(Client):
+    def __init__(self, version, endpoint, token, *args, **kwds):
+        Client.__init__(self, *args, **kwds)
+        if has_openstack:
+            self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
+
+    @require_enabled
+    def __getattr__(self, name):
+        return getattr(self.client, name)        
+
 class NovaClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
@@ -182,11 +171,16 @@ class OpenStackClient:
     def __init__ ( self, *args, **kwds) :
         # instantiate managers
         self.keystone = KeystoneClient(*args, **kwds)
-        self.keystone_db = KeystoneDB()
-        self.glance = GlanceClient(*args, **kwds)
+        url_parsed = urlparse.urlparse(self.keystone.url)
+        hostname = url_parsed.netloc.split(':')[0]
+        token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
+        #self.glance = GlanceClient(*args, **kwds)
+        
+        self.glanceclient = GlanceClientNew('1', endpoint='https://%s:9292' % hostname, token=token.id, **kwds)
         self.nova = NovaClient(*args, **kwds)
-        self.nova_db = NovaDB(*args, **kwds)
+        self.nova_db = NovaDB(*args, **kwds)
         self.quantum = QuantumClient(*args, **kwds)
+    
 
     @require_enabled
     def connect(self, *args, **kwds):