implement import
[sfa.git] / sfa / server / sfa-ca.py
index 477399a..8f4fafc 100755 (executable)
@@ -48,16 +48,20 @@ def main():
 
 
 def display(options):
+    """
+    Display the sepcified GID
+    """
     gidfile = os.path.abspath(options.display)
-    print gidfile
     if not gidfile or not os.path.isfile(gidfile):
         print "No such gid: %s" % gidfile
-        sys.exit(1) 
+        sys.exit(1)
     gid = GID(filename=gidfile)
     gid.dump(dump_parents=True)
 
 def sign(options):
-    from sfa.util.table import SfaTable
+    """
+    Sign the specified gid
+    """
     hierarchy = Hierarchy()
     config = Config()
     parent_hrn = config.SFA_INTERFACE_HRN
@@ -87,20 +91,81 @@ def sign(options):
     outfile = options.outfile
     if not outfile:
         outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
-    
+   
+    # check if gid already has a parent
     # sign the gid
     gid.set_issuer(parent_key, parent_hrn)
     gid.set_parent(parent_gid)
+    gid.sign()
     gid.save_to_file(outfile, save_parents=True)            
     
 
-def export(options):
+def export_gid(options):
     from sfa.util.table import SfaTable
-    pass
+    # lookup the record for the specified hrn 
+    hrn = options.export
+
+    # check sfa table first    
+    table = SfaTable()
+    records = table.find({'hrn': hrn, type: 'authority'})
+    if not records:
+        # check the authorities hierarchy 
+        hierarchy = Hierarchy()
+        try:
+            auth_info = hierarchy.get_auth_info()
+            gid = auth_info.gid_object 
+        except:
+            print "Record: %s not found" % hrn
+            sys.exit(1)
+    else:
+        record = records[0]
+        gid = GID(string=record['gid'])
+        
+    # get the outfile
+    outfile = options.outfile
+    if not outfile:
+        outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
+
+    # save it
+    gid.save_to_file(outfile, save_parents=True)
 
 def import_gid(options):
+    """
+    Import the specified gid into the registry (db and authorities 
+    hierarchy) overwriting any previous gid.
+    """
     from sfa.util.table import SfaTable
-    pass
+    from sfa.util.record import SfaRecord
+    # load the gid
+    gidfile = os.path.abspath(options.importgid)
+    if not gidfile or not os.path.isfile(gidfile):
+        print "No such gid: %s" % gidfile
+        sys.exit(1)
+    gid = GID(filename=gidfile)
+    
+    # check if it exists within the hierarchy
+    hierarchy = Hierarchy()
+    if not hierarchy.auth_exists(gid.get_hrn()):
+        print "%s not found in hierarchy" % gid.get_hrn()
+        sys.exit(1)
+
+    # check if record exists in db
+    table = SfaTable()
+    records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
+    if not records:
+        print "%s not found in record database" % get.get_hrn()  
+        sys.exit(1)
 
+    # update the database record
+    record = records[0]
+    record['gid'] = gid.save_to_string(save_parents=True)
+    table.update(record)
+
+    # update the hierarchy
+    auth_info = hierarchy.get_auth_info(gid.get_hrn())  
+    filename = auth_info.gid_filename
+    gid.save_to_file(filename, save_parents=True) 
+    
 if __name__ == '__main__':
     main()