rename NovaShell to Shell. cleanup
[sfa.git] / sfa / openstack / nova_driver.py
index 9ad1ce1..c65ca48 100644 (file)
@@ -9,6 +9,8 @@ from sfa.util.sfalogging import logger
 from sfa.util.defaultdict import defaultdict
 from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
 from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf, urn_to_sliver_id
+from sfa.planetlab.plxrn import PlXrn
+from sfa.openstack.osxrn import OSXrn, hrn_to_os_slicename
 from sfa.util.cache import Cache
 from sfa.trust.credential import Credential
 # used to be used in get_ticket
@@ -22,7 +24,7 @@ from sfa.managers.driver import Driver
 from sfa.openstack.nova_shell import NovaShell
 from sfa.openstack.euca_shell import EucaShell
 from sfa.openstack.osaggregate import OSAggregate
-from sfa.plc.plslices import PlSlices
+from sfa.planetlab.plslices import PlSlices
 from sfa.util.osxrn import OSXrn
 
 
@@ -45,8 +47,7 @@ class NovaDriver (Driver):
 
     def __init__ (self, config):
         Driver.__init__ (self, config)
-        self.shell = NovaShell (config)
-        self.euca_shell = EucaShell(config)
+        self.shell = Shell (config)
         self.cache=None
         if config.SFA_AGGREGATE_CACHING:
             if NovaDriver.cache is None:
@@ -73,7 +74,7 @@ class NovaDriver (Driver):
            
         if type == 'slice':
             # add slice description, name, researchers, PI 
-            name = Xrn(hrn).get_leaf()
+            name = hrn_to_os_slicename(hrn)
             researchers = sfa_record.get('researchers', [])
             pis = sfa_record.get('pis', [])
             project_manager = None
@@ -114,18 +115,28 @@ class NovaDriver (Driver):
     ##########
     # xxx actually old_sfa_record comes filled with plc stuff as well in the original code
     def update (self, old_sfa_record, new_sfa_record, hrn, new_key):
-        pointer = old_sfa_record['pointer']
-        type = old_sfa_record['type']
-
+        type = new_sfa_record['type'] 
+        
         # new_key implemented for users only
         if new_key and type not in [ 'user' ]:
             raise UnknownSfaType(type)
 
         elif type == "slice":
-            # can update description, researchers and PI
-            pass 
+            # can update project manager and description
+            name = hrn_to_os_slicename(hrn)
+            researchers = sfa_record.get('researchers', [])
+            pis = sfa_record.get('pis', [])
+            project_manager = None
+            description = sfa_record.get('description', None)
+            if pis:
+                project_manager = Xrn(pis[0], 'user').get_leaf()
+            elif researchers:
+                project_manager = Xrn(researchers[0], 'user').get_leaf()
+            self.shell.auth_manager.modify_project(name, project_manager, description)
+
         elif type == "user":
-            # can update  slices, keys and roles
+            # can techinally update access_key and secret_key,
+            # but that is not in our scope, so we do nothing.  
             pass
         return True
         
@@ -133,11 +144,12 @@ class NovaDriver (Driver):
     ##########
     def remove (self, sfa_record):
         type=sfa_record['type']
-        name = Xrn(sfa_record['hrn']).get_leaf()     
         if type == 'user':
+            name = Xrn(sfa_record['hrn']).get_leaf()     
             if self.shell.auth_manager.get_user(name):
                 self.shell.auth_manager.delete_user(name)
         elif type == 'slice':
+            name = hrn_to_os_slicename(sfa_record['hrn'])     
             if self.shell.auth_manager.get_project(name):
                 self.shell.auth_manager.delete_project(name)
         return True
@@ -153,9 +165,9 @@ class NovaDriver (Driver):
             records = [records]
 
         for record in records:
-            name = Xrn(record['hrn']).get_leaf()
             os_record = None
             if record['type'] == 'user':
+                name = Xrn(record['hrn']).get_leaf()
                 os_record = self.shell.auth_manager.get_user(name)
                 projects = self.shell.db.project_get_by_user(name)
                 record['slices'] = [self.hrn + "." + proj.name for \
@@ -163,7 +175,8 @@ class NovaDriver (Driver):
                 record['roles'] = self.shell.db.user_get_roles(name)
                 keys = self.shell.db.key_pair_get_all_by_user(name)
                 record['keys'] = [key.public_key for key in keys]     
-            elif record['type'] == 'slice': 
+            elif record['type'] == 'slice':
+                name = hrn_to_os_slicename(record['hrn']) 
                 os_record = self.shell.auth_manager.get_project(name)
                 record['description'] = os_record.description
                 record['PI'] = [self.hrn + "." + os_record.project_manager.name]
@@ -279,7 +292,7 @@ class NovaDriver (Driver):
     
     def sliver_status (self, slice_urn, slice_hrn):
         # find out where this slice is currently running
-        project_name = Xrn(slice_urn).get_leaf()
+        project_name = hrn_to_os_slicename(slice_hrn)
         project = self.shell.auth_manager.get_project(project_name)
         instances = self.shell.db.instance_get_all_by_project(project_name)
         if len(instances) == 0:
@@ -318,44 +331,33 @@ class NovaDriver (Driver):
 
     def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
 
-        project_name = get_leaf(slice_hrn)
         aggregate = OSAggregate(self)
-        # parse rspec
         rspec = RSpec(rspec_string)
+        instance_name = hrn_to_os_slicename(slice_hrn)
        
-        # ensure project and users exist in local db
-        aggregate.create_project(project_name, users, options=options)
-     
-        # collect publick keys
+        # assume first user is the caller and use their context
+        # for the ec2/euca api connection. Also, use the first users
+        # key as the project key.
+        key_name = None
+        if len(users) > 1:
+            key_name = aggregate.create_instance_key(slice_hrn, users[0])
+
+        # collect public keys
         pubkeys = []
-        project_key = None
         for user in users:
             pubkeys.extend(user['keys'])
-            # assume first user is the caller and use their context
-            # for the ec2/euca api connection. Also, use the first users
-            # key as the project key.   
-            if not project_key:
-                username = Xrn(user['urn']).get_leaf()
-                user_keys = self.shell.db.key_pair_get_all_by_user(username)
-                if user_keys:
-                    project_key = user_keys[0].name
-                     
-        # ensure person records exists
-        self.euca_shell.init_context(project_name)  
-        aggregate.run_instances(project_name, rspec_string, project_key, pubkeys)    
+           
+        aggregate.run_instances(instance_name, rspec_string, key_name, pubkeys)    
    
         return aggregate.get_rspec(slice_xrn=slice_urn, version=rspec.version)
 
     def delete_sliver (self, slice_urn, slice_hrn, creds, options):
-        # we need to do this using the context of one of the slice users
-        project_name = Xrn(slice_urn).get_leaf()
-        self.euca_shell.init_context(project_name) 
-        name = OSXrn(xrn=slice_urn).name
         aggregate = OSAggregate(self)
-        return aggregate.delete_instances(name)   
+        project_name = hrn_to_os_slicename(slice_hrn)
+        return aggregate.delete_instances(project_name)   
 
     def update_sliver(self, slice_urn, slice_hrn, rspec, creds, options):
-        name = OSXrn(xrn=slice_urn).name
+        name = hrn_to_os_slicename(slice_hrn)
         aggregate = OSAggregate(self)
         return aggregate.update_instances(name)