RSpecVersion.todict() needs to turn values() into a list()
[sfa.git] / sfa / openstack / euca_shell.py
index cb2817c..e2bdf7f 100644 (file)
@@ -8,6 +8,7 @@ except:
 
 from sfa.util.sfalogging import logger
 from sfa.openstack.nova_shell import NovaShell
+from sfa.util.config import Config
 
 class EucaShell:
     """
@@ -15,21 +16,39 @@ class EucaShell:
     """    
 
     def __init__(self, config):
-        self.config = Config
+        self.config = config
+        self.nova_shell = NovaShell(config)
+        self.access_key = None
+        self.secret_key = None
 
-    def get_euca_connection(self):
+    def init_context(self, project_name=None):
+        
+        # use the context of the specified  project's project
+        # manager. 
+        if project_name:
+            project = self.nova_shell.auth_manager.get_project(project_name)
+            self.access_key = "%s:%s" % (project.project_manager.name, project_name)
+            self.secret_key = project.project_manager.secret
+        else:
+            # use admin user's context
+            admin_user = self.nova_shell.auth_manager.get_user(self.config.SFA_NOVA_USER)
+            #access_key = admin_user.access
+            self.access_key = '%s' % admin_user.name
+            self.secret_key = admin_user.secret
+
+    def get_euca_connection(self, project_name=None):
         if not has_boto:
             logger.info('Unable to access EC2 API - boto library not found.')
             return None
-        nova = NovaShell(self.config)
-        admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER)
-        access_key = admin_user.access
-        secret_key = admin_user.secret
+
+        if not self.access_key or not self.secret_key:
+            self.init_context(project_name)
+        
         url = self.config.SFA_NOVA_API_URL
+        host = None
+        port = None    
         path = "/"
-        euca_port = self.config.SFA_NOVA_API_PORT        
         use_ssl = False
-
         # Split the url into parts 
         if url.find('https://') >= 0:
             use_ssl  = True
@@ -37,22 +56,22 @@ class EucaShell:
         elif url.find('http://') >= 0:
             use_ssl  = False
             url = url.replace('http://', '')
-        (host, parts) = url.split(':')
+        parts = url.split(':')
+        host = parts[0]
         if len(parts) > 1:
-            parts = parts.split('/')
+            parts = parts[1].split('/')
             port = int(parts[0])
             parts = parts[1:]
-            path = '/'.join(parts)
-        
-        return boto.connect_ec2(aws_access_key_id=access_key,
-                                aws_secret_access_key=secret_key,
+            path = '/'+'/'.join(parts)
+        return boto.connect_ec2(aws_access_key_id=self.access_key,
+                                aws_secret_access_key=self.secret_key,
                                 is_secure=use_ssl,
                                 region=RegionInfo(None, 'eucalyptus', host),
+                                host=host,
                                 port=port,
                                 path=path) 
 
     def __getattr__(self, name):
         def func(*args, **kwds):
             conn = self.get_euca_connection()
-            return getattr(conn, name)(*args, **kwds)
-        return func                     
+