fix bug in get_aggregate_nodes()
[sfa.git] / sfa / managers / registry_manager.py
index da0b127..9fd2659 100644 (file)
@@ -7,9 +7,8 @@ import commands
 
 from sfa.util.faults import RecordNotFound, AccountNotEnabled, PermissionError, MissingAuthority, \
     UnknownSfaType, ExistingRecord, NonExistingRecord
+from sfa.util.sfatime import utcparse, datetime_to_epoch
 from sfa.util.prefixTree import prefixTree
-from sfa.util.record import SfaRecord
-from sfa.util.table import SfaTable
 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn, urn_to_hrn
 from sfa.util.plxrn import hrn_to_pl_login_base
 from sfa.util.version import version_core
@@ -20,16 +19,21 @@ from sfa.trust.credential import Credential
 from sfa.trust.certificate import Certificate, Keypair, convert_public_key
 from sfa.trust.gid import create_uuid
 
+from sfa.storage.record import SfaRecord
+from sfa.storage.table import SfaTable
+
 class RegistryManager:
 
-    def __init__ (self): pass
+    def __init__ (self, config): pass
 
     # The GENI GetVersion call
-    def GetVersion(self, api):
-        peers = dict ( [ (hrn,interface._ServerProxy__host) for (hrn,interface) in api.registries.iteritems() 
+    def GetVersion(self, api, options):
+        peers = dict ( [ (hrn,interface.get_url()) for (hrn,interface) in api.registries.iteritems() 
                        if hrn != api.hrn])
         xrn=Xrn(api.hrn)
         return version_core({'interface':'registry',
+                             'sfa': 2,
+                             'geni_api': 2,
                              'hrn':xrn.get_hrn(),
                              'urn':xrn.get_urn(),
                              'peers':peers})
@@ -55,6 +59,7 @@ class RegistryManager:
     
         # verify_cancreate_credential requires that the member lists
         # (researchers, pis, etc) be filled in
+        self.driver.augment_records_with_testbed_info (record)
         if not self.driver.is_enabled (record):
               raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
     
@@ -71,8 +76,8 @@ class RegistryManager:
         rights = api.auth.determine_user_rights(caller_hrn, record)
         # make sure caller has rights to this object
         if rights.is_empty():
-            raise PermissionError(caller_hrn + " has no rights to " + record['name'])
-    
+            raise PermissionError("%s has no rights to %s (%s)" % \
+                                  (caller_hrn, object_hrn, xrn))    
         object_gid = GID(string=record['gid'])
         new_cred = Credential(subject = object_gid.get_subject())
         new_cred.set_gid_caller(caller_gid)
@@ -82,7 +87,9 @@ class RegistryManager:
         new_cred.set_privileges(rights)
         new_cred.get_privileges().delegate_all_privileges(True)
         if 'expires' in record:
-            new_cred.set_expiration(int(record['expires']))
+            date = utcparse(record['expires'])
+            expires = datetime_to_epoch(date)
+            new_cred.set_expiration(int(expires))
         auth_kind = "authority,ma,sa"
         # Parent not necessary, verify with certs
         #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
@@ -211,10 +218,20 @@ class RegistryManager:
             pkey = certificate.get_pubkey()    
         gid = api.auth.hierarchy.create_gid(xrn, create_uuid(), pkey) 
         return gid.save_to_string(save_parents=True)
-        
+    
+    ####################
     # utility for handling relationships among the SFA objects 
     # given that the SFA db does not handle this sort of relationsships
     # it will rely on side-effects in the testbed to keep this persistent
+    
+    # subject_record describes the subject of the relationships
+    # ref_record contains the target values for the various relationships we need to manage
+    # (to begin with, this is just the slice x person relationship)
+    def update_relations (self, subject_record, ref_record):
+        type=subject_record['type']
+        if type=='slice':
+            self.update_relation(subject_record, 'researcher', ref_record.get('researcher'), 'user')
+        
     # field_key is the name of one field in the record, typically 'researcher' for a 'slice' record
     # hrns is the list of hrns that should be linked to the subject from now on
     # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
@@ -271,8 +288,7 @@ class RegistryManager:
             gid = auth_info.get_gid_object()
             record.set_gid(gid.save_to_string(save_parents=True))
 
-        # update testbed-specific data f needed
-        logger.info("Getting driver from manager=%s"%self)
+        # update testbed-specific data if needed
         pointer = self.driver.register (record, hrn, pub_key)
 
         record.set_pointer(pointer)
@@ -280,8 +296,8 @@ class RegistryManager:
         record['record_id'] = record_id
     
         # update membership for researchers, pis, owners, operators
-        self.update_relation(record, 'researcher', record.get('researcher'), 'user')
-    
+        self.update_relations (record, record)
+        
         return record.get_gid_object().save_to_string(save_parents=True)
     
     def Update(self, api, record_dict):
@@ -329,7 +345,7 @@ class RegistryManager:
             table.update(record)
         
         # update membership for researchers, pis, owners, operators
-        self.update_relation(record, 'researcher', new_record.get('researcher'), 'user')
+        self.update_relations (record, new_record)
         
         return 1 
     
@@ -379,10 +395,10 @@ class RegistryManager:
         # verify that the callers's ip address exist in the db and is an interface
         # for a node in the db
         (ip, port) = api.remote_addr
-        interfaces = self.driver.GetInterfaces({'ip': ip}, ['node_id'])
+        interfaces = self.driver.shell.GetInterfaces({'ip': ip}, ['node_id'])
         if not interfaces:
             raise NonExistingRecord("no such ip %(ip)s" % locals())
-        nodes = self.driver.GetNodes([interfaces[0]['node_id']], ['node_id', 'hostname'])
+        nodes = self.driver.shell.GetNodes([interfaces[0]['node_id']], ['node_id', 'hostname'])
         if not nodes:
             raise NonExistingRecord("no such node using ip %(ip)s" % locals())
         node = nodes[0]