Merge branch 'master' into senslab2
[sfa.git] / sfa / planetlab / pldriver.py
index 76f54da..21fa227 100644 (file)
@@ -7,7 +7,7 @@ from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
 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 hrn_to_urn, get_leaf, urn_to_sliver_id
+from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf
 from sfa.util.cache import Cache
 
 # one would think the driver should not need to mess with the SFA db, but..
@@ -27,7 +27,7 @@ from sfa.planetlab.plshell import PlShell
 import sfa.planetlab.peers as peers
 from sfa.planetlab.plaggregate import PlAggregate
 from sfa.planetlab.plslices import PlSlices
-from sfa.planetlab.plxrn import PlXrn, slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename
+from sfa.planetlab.plxrn import PlXrn, slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename, xrn_to_hostname
 
 
 def list_to_dict(recs, key):
@@ -160,6 +160,10 @@ class PlDriver (Driver):
                            'password', 'phone', 'url', 'bio', 'accepted_aup',
                            'enabled']:
                     update_fields[key] = all_fields[key]
+            # when updating a user, we always get a 'email' field at this point
+            # this is because 'email' is a native field in the RegUser object...
+            if 'email' in update_fields and not update_fields['email']:
+                del update_fields['email']
             self.shell.UpdatePerson(pointer, update_fields)
     
             if new_key:
@@ -607,6 +611,14 @@ class PlDriver (Driver):
         #panos adding the info option to the caching key (can be improved)
         if options.get('info'):
             version_string = version_string + "_"+options.get('info', 'default')
+
+        # Adding the list_leases option to the caching key
+        if options.get('list_leases'):
+            version_string = version_string + "_"+options.get('list_leases', 'default')
+
+        # Adding geni_available to caching key
+        if options.get('geni_available'):
+            version_string = version_string + "_" + str(options.get('geni_available'))
     
         # look in cache first
         if cached_requested and self.cache and not slice_hrn:
@@ -650,7 +662,7 @@ class PlDriver (Driver):
             persons = self.shell.GetPersons(slice['person_ids'], ['key_ids'])
             key_ids = [key_id for person in persons for key_id in person['key_ids']]
             person_keys = self.shell.GetKeys(key_ids)
-            keys = [key['key'] for key in keys]
+            keys = [key['key'] for key in person_keys]
 
             user.update({'urn': slice_urn,
                          'login': slice['name'],
@@ -679,7 +691,7 @@ class PlDriver (Driver):
             if node['last_contact'] is not None:
                 
                 res['pl_last_contact'] = datetime_to_string(utcparse(node['last_contact']))
-            sliver_id = urn_to_sliver_id(slice_urn, slice['slice_id'], node['node_id'], authority=self.hrn) 
+            sliver_id = Xrn(slice_urn, type='slice', id=node['node_id'], authority=self.hrn).urn
             res['geni_urn'] = sliver_id
             if node['boot_state'] == 'boot':
                 res['geni_status'] = 'ready'
@@ -724,15 +736,31 @@ class PlDriver (Driver):
         for node in rspec.version.get_nodes_with_slivers():
             hostname = None
             if node.get('component_name'):
-                hostname = node.get('component_name')
+                hostname = node.get('component_name').strip()
             elif node.get('component_id'):
-                hostname = xrn_to_hostname(node.get('component_id'))
+                hostname = xrn_to_hostname(node.get('component_id').strip())
             if hostname:
                 requested_slivers.append(hostname)
         nodes = slices.verify_slice_nodes(slice, requested_slivers, peer) 
    
         # add/remove links links 
         slices.verify_slice_links(slice, rspec.version.get_link_requests(), nodes)
+
+        # add/remove leases
+        requested_leases = []
+        kept_leases = []
+        for lease in rspec.version.get_leases():
+            requested_lease = {}
+            if not lease.get('lease_id'):
+               requested_lease['hostname'] = xrn_to_hostname(lease.get('component_id').strip())
+               requested_lease['start_time'] = lease.get('start_time')
+               requested_lease['duration'] = lease.get('duration')
+            else:
+               kept_leases.append(int(lease['lease_id']))
+            if requested_lease.get('hostname'):
+                requested_leases.append(requested_lease)
+
+        leases = slices.verify_slice_leases(slice, requested_leases, kept_leases, peer)
     
         # handle MyPLC peer association.
         # only used by plc and ple.
@@ -744,7 +772,7 @@ class PlDriver (Driver):
         slicename = hrn_to_pl_slicename(slice_hrn)
         slices = self.shell.GetSlices({'name': slicename})
         if not slices:
-            return 1
+            return True
         slice = slices[0]
     
         # determine if this is a peer slice
@@ -759,7 +787,7 @@ class PlDriver (Driver):
         finally:
             if peer:
                 self.shell.BindObjectToPeer('slice', slice['slice_id'], peer, slice['peer_slice_id'])
-        return 1
+        return True
     
     def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):
         slicename = hrn_to_pl_slicename(slice_hrn)