Bugfix. NT.
[sfa.git] / sfa / senslab / slab-import.py
index 9699f4b..4f20b00 100644 (file)
@@ -1,17 +1,13 @@
-###########################################################################
-#    Copyright (C) 2011 by root                                      
-#    <root@FlabFedora2>                                                             
-#
-# Copyright: See COPYING file that comes with this distribution
-#
-###########################################################################
+
 import sys
 import datetime
 import time
 from sfa.senslab.OARrestapi import OARapi
 from sfa.senslab.LDAPapi import LDAPapi
 from sfa.senslab.slabdriver import SlabDriver
+from sfa.senslab.slabpostgres import SlabDB
 from sfa.util.config import Config
+from sfa.util.plxrn import PlXrn
 from sfa.util.xrn import hrn_to_urn, get_authority,Xrn,get_leaf
 from sfa.util.table import SfaTable
 from sfa.util.record import SfaRecord
@@ -24,6 +20,7 @@ config = Config()
 TrustedR = TrustedRoots(Config.get_trustedroots_dir(config))
 AuthHierarchy = Hierarchy()
 table = SfaTable()
+db = SlabDB()
 if not table.exists():
     table.create()
     
@@ -104,7 +101,8 @@ def import_node(hrn, node):
         hrn = hrn[:64]
 
     node_record = table.find({'type': 'node', 'hrn': hrn})
-    pkey = Keypair(create=True)
+    pkey = Keypair(create=True)        
+    
     urn = hrn_to_urn(hrn, 'node')
     node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
     node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
@@ -113,7 +111,7 @@ def import_node(hrn, node):
     node_record['date_created'] = int(time.mktime(extime.timetuple()))
     existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
     if not existing_records:
-        print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record['hrn'])
+        print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record )
         table.insert(node_record)
     else:
         existing_record = existing_records[0]
@@ -151,17 +149,20 @@ def import_slice(person):
    
     extime = datetime.datetime.utcnow()
     slice_record['date_created'] = int(time.mktime(extime.timetuple()))
-                               
+    #special slice table for Senslab, to store nodes info (OAR)                        
 
     existing_records = table.find({'hrn': slice_record['hrn'], 'type': 'slice'})
     if not existing_records:
         print>>sys.stderr, " \r\n \t slab-import : slice record %s inserted" %(slice_record['hrn'])
         table.insert(slice_record)
+        db.insert_slab_slice(person)
+
     else:
         print>>sys.stderr, " \r\n \t slab-import : slice record %s updated" %(slice_record['hrn'])
         existing_record = existing_records[0]
         slice_record['record_id'] = existing_record['record_id']
-        table.update(slice_record)        
+        table.update(slice_record)
+        db.update_senslab_slice(slice_record)   
         
 def delete_record( hrn, type):
     # delete the record
@@ -170,21 +171,20 @@ def delete_record( hrn, type):
         print>>sys.stderr, " \r\n \t slab-import : record %s deleted" %(record['hrn'])
         table.remove(record)
                 
-def hostname_to_hrn(root_auth,hostname):
-    # keep only the first part of the DNS name
-    #hrn='.'.join( [auth,hostname.split(".")[0] ] )
-    # escape the '.' in the hostname
-    hrn='.'.join( [root_auth,Xrn.escape(hostname)] )
-    return hrn_to_urn(hrn,'node')
+def hostname_to_hrn(root_auth,login_base,hostname):
+    return PlXrn(auth=root_auth,hostname=login_base+'_'+hostname).get_hrn()
+
     
 def main():
 
-    
+    if not db.exists('slice'):
+        db.createtable('slice')
+        
     if not config.SFA_REGISTRY_ENABLED:
         sys.exit(0)
     root_auth = config.SFA_REGISTRY_ROOT_AUTH
     interface_hrn = config.SFA_INTERFACE_HRN
-    print interface_hrn, root_auth
+
     
     #Get all records in the sfa table   
     # create dict of all existing sfa records
@@ -224,12 +224,12 @@ def main():
     #print "\r\n NODES8DICT ",nodes_dict
     
     ldap_person_list = Driver.GetPersons()
-    
 
-   
         # import node records
     for node in nodes_dict:
-        hrn =  hostname_to_hrn( root_auth, node['hostname'])
+        # Sandrine
+        # A changer pour l utilisation du nouveau OAR de prod, le site etant contenu dans le hostname
+        hrn =  hostname_to_hrn( root_auth,node['site_login_base'], node['hostname'])
         if hrn not in existing_hrns or \
         (hrn, 'node') not in existing_records:
             import_node(hrn, node)