Refactor to /opt/planetstack, final tweaks to make sure planetstack can run in non...
[plstackapi.git] / planetstack / openstack / client.py
similarity index 53%
rename from plstackapi/openstack/client.py
rename to planetstack/openstack/client.py
index 04aef25..f3abbb2 100644 (file)
@@ -1,9 +1,21 @@
-from keystoneclient.v2_0 import client as keystone_client
-from glance import client as glance_client
-from novaclient.v1_1 import client as nova_client
-from quantumclient.v2_0 import client as quantum_client
-
-from plstackapi.planetstack.config import Config
+try:
+    from keystoneclient.v2_0 import client as keystone_client
+    from glance import client as glance_client
+    from novaclient.v1_1 import client as nova_client
+    from quantumclient.v2_0 import client as quantum_client
+    has_openstack = True
+except:
+    has_openstack = False
+
+from planetstack.config import Config
+
+def require_enabled(callable):
+    def wrapper(*args, **kwds):
+        if has_openstack:
+            return callable(*args, **kwds)
+        else:
+            return None
+    return wrapper
 
 def parse_novarc(filename):
     opts = {}
@@ -27,6 +39,7 @@ class Client:
             config = Config(config)
         else:
             config = Config()
+        self.has_openstack = has_openstack
         self.username = config.nova_admin_user
         self.password = config.nova_admin_password
         self.tenant = config.nova_admin_tenant
@@ -47,14 +60,17 @@ class Client:
 class KeystoneClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
-        self.client = keystone_client.Client(username=self.username,
-                                             password=self.password,
-                                             tenant_name=self.tenant,
-                                             auth_url=self.url)
+        if has_openstack:
+            self.client = keystone_client.Client(username=self.username,
+                                                 password=self.password,
+                                                 tenant_name=self.tenant,
+                                                 auth_url=self.url)
 
+    @require_enabled
     def connect(self, *args, **kwds):
         self.__init__(*args, **kwds)
 
+    @require_enabled
     def __getattr__(self, name):
         return getattr(self.client, name)
 
@@ -62,43 +78,51 @@ class KeystoneClient(Client):
 class GlanceClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
-        self.client = glance_client.get_client(host='0.0.0.0',
-                                               username=self.username,
-                                               password=self.password,
-                                               tenant=self.tenant,
-                                               auth_url=self.url)
+        if has_openstack:
+            self.client = glance_client.get_client(host='0.0.0.0',
+                                                   username=self.username,
+                                                   password=self.password,
+                                                   tenant=self.tenant,
+                                                   auth_url=self.url)
+    @require_enabled
     def __getattr__(self, name):
         return getattr(self.client, name)
 
 class NovaClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
-        self.client = nova_client.Client(username=self.username,
-                                         api_key=self.password,
-                                         project_id=self.tenant,
-                                         auth_url=self.url,
-                                         region_name='',
-                                         extensions=[],
-                                         service_type='compute',
-                                         service_name='',
-                                         )
-
+        if has_openstack:
+            self.client = nova_client.Client(username=self.username,
+                                             api_key=self.password,
+                                             project_id=self.tenant,
+                                             auth_url=self.url,
+                                             region_name='',
+                                             extensions=[],
+                                             service_type='compute',
+                                             service_name='',
+                                             )
+
+    @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)
-        self.client = quantum_client.Client(username=self.username,
-                                            password=self.password,
-                                            tenant_name=self.tenant,
-                                            auth_url=self.url)
+        if has_openstack:
+            self.client = quantum_client.Client(username=self.username,
+                                                password=self.password,
+                                                tenant_name=self.tenant,
+                                                auth_url=self.url)
+    @require_enabled
     def connect(self, *args, **kwds):
         self.__init__(*args, **kwds)
 
+    @require_enabled
     def __getattr__(self, name):
         return getattr(self.client, name)
 
@@ -115,9 +139,10 @@ class OpenStackClient:
         self.nova = NovaClient(*args, **kwds)
         self.quantum = QuantumClient(*args, **kwds)
 
+    @require_enabled
     def connect(self, *args, **kwds):
         self.__init__(*args, **kwds)
-        
 
+    @require_enabled
     def authenticate(self):
         return self.keystone.authenticate()