merge master again (2.0-10 changelog only)
[sfa.git] / sfa / managers / registry_manager.py
index d957c57..79ac91c 100644 (file)
@@ -1,5 +1,4 @@
 import types
-import time 
 # for get_key_from_incoming_ip
 import tempfile
 import os
@@ -19,7 +18,7 @@ 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.persistentobjs import RegRecord
+from sfa.storage.model import make_record,RegRecord
 from sfa.storage.alchemy import dbsession
 
 class RegistryManager:
@@ -53,7 +52,7 @@ class RegistryManager:
         # get record info
         record=dbsession.query(RegRecord).filter_by(type=type,hrn=hrn).first()
         if not record:
-            raise RecordNotFound(hrn)
+            raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
     
         # verify_cancreate_credential requires that the member lists
         # (researchers, pis, etc) be filled in
@@ -99,13 +98,13 @@ class RegistryManager:
         return new_cred.save_to_string(save_parents=True)
     
     
-    def Resolve(self, api, xrns, intype=None, full=True):
+    def Resolve(self, api, xrns, type=None, full=True):
     
         if not isinstance(xrns, types.ListType):
             xrns = [xrns]
             # try to infer type if not set and we get a single input
-            if not intype:
-                intype = Xrn(xrns).get_type()
+            if not type:
+                type = Xrn(xrns).get_type()
         hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] 
 
         # load all known registry names into a prefix tree and attempt to find
@@ -137,26 +136,29 @@ class RegistryManager:
                 credential = api.getCredential()
                 interface = api.registries[registry_hrn]
                 server_proxy = api.server_proxy(interface, credential)
-                peer_records = server_proxy.Resolve(xrns, credential,intype)
+                peer_records = server_proxy.Resolve(xrns, credential,type)
                 # pass foreign records as-is
+                # previous code used to read
+                # records.extend([SfaRecord(dict=record).as_dict() for record in peer_records])
+                # not sure why the records coming through xmlrpc had to be processed at all
                 records.extend(peer_records)
     
         # try resolving the remaining unfound records at the local registry
         local_hrns = list ( set(hrns).difference([record['hrn'] for record in records]) )
         # 
         local_records = dbsession.query(RegRecord).filter(RegRecord.hrn.in_(local_hrns))
-        if intype:
-            local_records = local_records.filter_by(type=intype)
+        if type:
+            local_records = local_records.filter_by(type=type)
         local_records=local_records.all()
+        logger.info("Resolve: local_records=%s (type=%s)"%(local_records,type))
         local_dicts = [ record.__dict__ for record in local_records ]
         
         if full:
             # in full mode we get as much info as we can, which involves contacting the 
             # testbed for getting implementation details about the record
-            for record in local_dicts: logger.info("resolve augment %s"%record)
             self.driver.augment_records_with_testbed_info(local_dicts)
-#            # also we fill the 'url' field for known authorities
-#            # used to be in the driver code, sounds like a poorman thing though
+            # also we fill the 'url' field for known authorities
+            # used to be in the driver code, sounds like a poorman thing though
             def solve_neighbour_url (record):
                 if not record.type.startswith('authority'): return 
                 hrn=record.hrn
@@ -169,7 +171,7 @@ class RegistryManager:
         # convert local record objects to dicts for xmlrpc
         # xxx somehow here calling dict(record) issues a weird error
         # however record.todict() seems to work fine
-#        records.extend( [ dict(record) for record in local_records ] )
+        # records.extend( [ dict(record) for record in local_records ] )
         records.extend( [ record.todict() for record in local_records ] )    
         if not records:
             raise RecordNotFound(str(hrns))
@@ -197,6 +199,7 @@ class RegistryManager:
             interface = api.registries[registry_hrn]
             server_proxy = api.server_proxy(interface, credential)
             record_list = server_proxy.List(xrn, credential)
+            # same as above, no need to process what comes from through xmlrpc
             # pass foreign records as-is
             record_dicts = record_list
         
@@ -205,7 +208,7 @@ class RegistryManager:
             if not api.auth.hierarchy.auth_exists(hrn):
                 raise MissingAuthority(hrn)
             records = dbsession.query(RegRecord).filter_by(authority=hrn)
-            record_dicts=[ record.__dict__ for record in records ]
+            record_dicts=[ record.todict() for record in records ]
     
         return record_dicts
     
@@ -262,8 +265,9 @@ class RegistryManager:
             raise ExistingRecord(hrn)
            
         assert ('type' in record_dict)
-        record = RegRecord("undefined")
-        record.set_from_dict(record_dict)
+        # returns the right type of RegRecord according to type in record
+        record = make_record(dict=record_dict)
+        record.just_created()
         record.authority = get_authority(record.hrn)
         auth_info = api.auth.get_auth_info(record.authority)
         pub_key = None
@@ -296,6 +300,7 @@ class RegistryManager:
 
         record.pointer=pointer
         dbsession.add(record)
+        dbsession.commit()
     
         # update membership for researchers, pis, owners, operators
         self.update_relations (record, record)
@@ -303,17 +308,16 @@ class RegistryManager:
         return record.get_gid_object().save_to_string(save_parents=True)
     
     def Update(self, api, record_dict):
-        new_record = SfaRecord(dict = record_dict)
-        type = new_record['type']
-        hrn = new_record['hrn']
-        urn = hrn_to_urn(hrn,type)
-        table = SfaTable()
+        assert ('type' in record_dict)
+        new_record=RegRecord(dict=record_dict)
+        type = new_record.type
+        hrn = new_record.hrn
+        
         # make sure the record exists
-        records = table.findObjects({'type': type, 'hrn': hrn})
-        if not records:
-            raise RecordNotFound(hrn)
-        record = records[0]
-        record['last_updated'] = time.gmtime()
+        record = dbsession.query(RegRecord).filter_by(type=type,hrn=hrn).first()
+        if not record:
+            raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
+        record.just_updated()
     
         # validate the type
         if type not in ['authority', 'slice', 'node', 'user']:
@@ -321,18 +325,18 @@ class RegistryManager:
 
         # Use the pointer from the existing record, not the one that the user
         # gave us. This prevents the user from inserting a forged pointer
-        pointer = record['pointer']
+        pointer = record.pointer
     
         # is the a change in keys ?
         new_key=None
         if type=='user':
-            if 'keys' in new_record and new_record['keys']:
-                new_key=new_record['keys']
+            if getattr(new_key,'keys',None):
+                new_key=new_record.keys
                 if isinstance (new_key,types.ListType):
                     new_key=new_key[0]
 
         # update the PLC information that was specified with the record
-        if not self.driver.update (record, new_record, hrn, new_key):
+        if not self.driver.update (record.__dict__, new_record.__dict__, hrn, new_key):
             logger.warning("driver.update failed")
     
         # take new_key into account
@@ -340,11 +344,11 @@ class RegistryManager:
             # update the openssl key and gid
             pkey = convert_public_key(new_key)
             uuid = create_uuid()
+            urn = hrn_to_urn(hrn,type)
             gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
             gid = gid_object.save_to_string(save_parents=True)
-            record['gid'] = gid
-            record = SfaRecord(dict=record)
-            table.update(record)
+            record.gid = gid
+            dsession.commit()
         
         # update membership for researchers, pis, owners, operators
         self.update_relations (record, new_record)
@@ -353,19 +357,19 @@ class RegistryManager:
     
     # expecting an Xrn instance
     def Remove(self, api, xrn, origin_hrn=None):
-    
-        table = SfaTable()
-        filter = {'hrn': xrn.get_hrn()}
         hrn=xrn.get_hrn()
         type=xrn.get_type()
+        request=dbsession.query(RegRecord).filter_by(hrn=hrn)
         if type and type not in ['all', '*']:
-            filter['type'] = type
+            request=request.filter_by(type=type)
     
-        records = table.find(filter)
-        if not records: raise RecordNotFound(hrn)
-        record = records[0]
-        type = record['type']
-        
+        record = request.first()
+        if not record:
+            msg="Could not find hrn %s"%hrn
+            if type: msg += " type=%s"%type
+            raise RecordNotFound(msg)
+
+        type = record.type
         if type not in ['slice', 'user', 'node', 'authority'] :
             raise UnknownSfaType(type)
 
@@ -384,15 +388,16 @@ class RegistryManager:
 
         # call testbed callback first
         # IIUC this is done on the local testbed TOO because of the refreshpeer link
-        if not self.driver.remove(record):
+        if not self.driver.remove(record.__dict__):
             logger.warning("driver.remove failed")
 
         # delete from sfa db
-        table.remove(record)
+        dbsession.delete(record)
+        dbsession.commit()
     
         return 1
 
-    # This is a PLC-specific thing...
+    # This is a PLC-specific thing, won't work with other platforms
     def get_key_from_incoming_ip (self, api):
         # verify that the callers's ip address exist in the db and is an interface
         # for a node in the db
@@ -406,23 +411,20 @@ class RegistryManager:
         node = nodes[0]
        
         # look up the sfa record
-        table = SfaTable()
-        records = table.findObjects({'type': 'node', 'pointer': node['node_id']})
-        if not records:
-            raise RecordNotFound("pointer:" + str(node['node_id']))  
-        record = records[0]
+        record=dbsession.query(RegRecord).filter_by(type='node',pointer=node['node_id']).first()
+        if not record:
+            raise RecordNotFound("node with pointer %s"%node['node_id'])
         
         # generate a new keypair and gid
         uuid = create_uuid()
         pkey = Keypair(create=True)
-        urn = hrn_to_urn(record['hrn'], record['type'])
+        urn = hrn_to_urn(record.hrn, record.type)
         gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
         gid = gid_object.save_to_string(save_parents=True)
-        record['gid'] = gid
-        record.set_gid(gid)
+        record.gid = gid
 
         # update the record
-        table.update(record)
+        dbsession.commit()
   
         # attempt the scp the key
         # and gid onto the node