initialize glance client with ca_ssl_cert
[plstackapi.git] / planetstack / openstack / client.py
index f3abbb2..0aa6c7d 100644 (file)
@@ -1,8 +1,10 @@
+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 neutronclient.v2_0 import client as quantum_client
     has_openstack = True
 except:
     has_openstack = False
@@ -34,16 +36,18 @@ def parse_novarc(filename):
     return opts
 
 class Client:
-    def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds):
-        if config:
-            config = Config(config)
-        else:
-            config = Config()
+    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 = config.nova_admin_user
-        self.password = config.nova_admin_password
-        self.tenant = config.nova_admin_tenant
-        self.url = config.nova_url
+        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
@@ -53,9 +57,13 @@ class Client:
             self.tenant = tenant
         if url:
             self.url = url
+        if token:
+            self.token = token    
+        if endpoint:
+            self.endpoint = endpoint
 
-        if '@' in self.username:
-            self.username = self.username[:self.username.index('@')]
+        #if '@' in self.username:
+        #    self.username = self.username[:self.username.index('@')]
 
 class KeystoneClient(Client):
     def __init__(self, *args, **kwds):
@@ -64,7 +72,8 @@ class KeystoneClient(Client):
             self.client = keystone_client.Client(username=self.username,
                                                  password=self.password,
                                                  tenant_name=self.tenant,
-                                                 auth_url=self.url)
+                                                 auth_url=self.url
+                                                )
 
     @require_enabled
     def connect(self, *args, **kwds):
@@ -75,11 +84,11 @@ class KeystoneClient(Client):
         return getattr(self.client, name)
 
 
-class GlanceClient(Client):
+class Glance(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,
@@ -88,6 +97,20 @@ class GlanceClient(Client):
     def __getattr__(self, name):
         return getattr(self.client, name)
 
+class GlanceClient(Client):
+    def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
+        Client.__init__(self, *args, **kwds)
+        if has_openstack:
+            self.client = glanceclient.Client(version, 
+                endpoint=endpoint, 
+                token=token,
+                cacert=cacert
+            )
+
+    @require_enabled
+    def __getattr__(self, name):
+        return getattr(self.client, name)        
+
 class NovaClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
@@ -110,6 +133,23 @@ class NovaClient(Client):
     def __getattr__(self, name):
         return getattr(self.client, name)
 
+class NovaDB(Client):
+    def __init__(self, *args, **kwds):
+        Client.__init__(self, *args, **kwds)
+        if has_openstack:
+            self.ctx = get_admin_context()
+            nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
+            self.client = nova_db_api
+
+
+    @require_enabled
+    def connect(self, *args, **kwds):
+        self.__init__(*args, **kwds)
+
+    @require_enabled
+    def __getattr__(self, name):
+        return getattr(self.client, name)
+
 class QuantumClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
@@ -135,9 +175,16 @@ class OpenStackClient:
     def __init__ ( self, *args, **kwds) :
         # instantiate managers
         self.keystone = KeystoneClient(*args, **kwds)
-        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)
+        glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
+        
+        self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds)
         self.nova = NovaClient(*args, **kwds)
+        # self.nova_db = NovaDB(*args, **kwds)
         self.quantum = QuantumClient(*args, **kwds)
+    
 
     @require_enabled
     def connect(self, *args, **kwds):