Removed hard coded login in GET requests (OARrestapi).
[sfa.git] / sfa / importer / sfa-import-openstack.py
index bf63a53..ec785e8 100755 (executable)
@@ -27,11 +27,7 @@ from sfa.trust.certificate import convert_public_key, Keypair
 from sfa.trust.gid import create_uuid
 from sfa.importer.sfaImport import sfaImport, _cleanup_string
 from sfa.util.sfalogging import logger
-try:
-    from nova.auth.manager import AuthManager, db, context
-except ImportError:
-    AuthManager = None
-    
+from sfa.openstack.openstack_shell import OpenstackShell    
 
 def process_options():
 
@@ -68,11 +64,7 @@ def main():
         sys.exit(0)
     root_auth = config.SFA_REGISTRY_ROOT_AUTH
     interface_hrn = config.SFA_INTERFACE_HRN
-    if  AuthManager:
-        auth_manager = AuthManager()
-    else:    
-        logger.info("Unable to import nova.auth.manager. Doesn't look like openstack-copute is installed. Exiting...")
-        sys.exit(0)    
+    shell = OpenstackShell(config)
     sfaImporter.create_top_level_records()
     
     # create dict of all existing sfa records
@@ -87,42 +79,45 @@ def main():
             
         
     # Get all users
-    persons = auth_manager.get_users()
+    persons = shell.user_get_all()
     persons_dict = {}
     keys_filename = config.config_path + os.sep + 'person_keys.py' 
     old_person_keys = load_keys(keys_filename)    
     person_keys = {} 
     for person in persons:
         hrn = config.SFA_INTERFACE_HRN + "." + person.id
+        persons_dict[hrn] = person
         old_keys = old_person_keys.get(person.id, [])
-        keys = db.key_pair_get_all_by_user(context.get_admin_context(), person.id)
-        person_keys[person.id] = [key.public_key for key in keys]
+        keys = [k.public_key for k in shell.key_pair_get_all_by_user(person.id)]
+        person_keys[person.id] = keys
         update_record = False
         if old_keys != keys:
             update_record = True
         if hrn not in existing_hrns or \
                (hrn, 'user') not in existing_records or update_record:    
             urn = hrn_to_urn(hrn, 'user')
+            
             if keys:
                 try:
-                    pkey = convert_public_key(key)
+                    pkey = convert_public_key(keys[0])
                 except:
-                    logger.warn('unable to convert public key for %s' % hrn)
+                    logger.log_exc('unable to convert public key for %s' % hrn)
                     pkey = Keypair(create=True)
             else:
                 logger.warn("Import: person %s does not have a PL public key"%hrn)
                 pkey = Keypair(create=True) 
-                person_gid = sfaImporter.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
-                person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", \
-                                              authority=get_authority(hrn))
-                persons_dict[person_record['hrn']] = person_record
-                person_record.sync()
+            person_gid = sfaImporter.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
+            person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", \
+                                          authority=get_authority(hrn))
+            logger.info("Import: importing %s " % person_record.summary_string())
+            person_record.sync()
 
     # Get all projects
-    projects = db.project_get_all(context.get_admin_context())
+    projects = shell.project_get_all()
     projects_dict = {}
     for project in projects:
         hrn = config.SFA_INTERFACE_HRN + '.' + project.id
+        projects_dict[hrn] = project
         if hrn not in existing_hrns or \
         (hrn, 'slice') not in existing_records:
             pkey = Keypair(create=True)
@@ -131,7 +126,8 @@ def main():
             project_record = SfaRecord(hrn=hrn, gid=project_gid, type="slice",
                                        authority=get_authority(hrn))
             projects_dict[project_record['hrn']] = project_record
-            project_record.sync(verbose=True) 
+            logger.info("Import: importing %s " % project_record.summary_string())
+            project_record.sync() 
     
     # remove stale records    
     system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
@@ -154,6 +150,7 @@ def main():
         
         record_object = existing_records[(record_hrn, type)]
         record = SfaRecord(dict=record_object)
+        logger.info("Import: removing %s " % record.summary_string())
         record.delete()
                                    
     # save pub keys