organized
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Sun, 1 Jul 2012 04:28:18 +0000 (00:28 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Sun, 1 Jul 2012 04:28:18 +0000 (00:28 -0400)
sfa/importer/openstackimporter.py

index 00d0a01..62f29ef 100644 (file)
@@ -30,51 +30,39 @@ class OpenstackImporter:
     def __init__ (self, auth_hierarchy, logger):
         self.auth_hierarchy = auth_hierarchy
         self.logger=logger
+        self.config = Config ()
+        self.interface_hrn = self.config.SFA_INTERFACE_HRN
+        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
+        self.shell = Shell (self.config)
 
     def add_options (self, parser):
         self.logger.debug ("OpenstackImporter: no options yet")
         pass
 
-    def run (self, options):
-        # we don't have any options for now
-        self.logger.info ("OpenstackImporter.run : to do")
-
-        config = Config ()
-        interface_hrn = config.SFA_INTERFACE_HRN
-        root_auth = config.SFA_REGISTRY_ROOT_AUTH
-        shell = Shell (config)
-
-        # create dict of all existing sfa records
-        existing_records = {}
-        existing_hrns = []
-        key_ids = []
-        for record in dbsession.query(RegRecord):
-            existing_records[ (record.hrn, record.type,) ] = record
-            existing_hrns.append(record.hrn) 
-            
+    def import_users(self, existing_hrns, existing_records):
         # Get all users
-        users = shell.auth_manager.users.list()
+        users = self.shell.auth_manager.users.list()
         users_dict = {}
-        keys_filename = config.config_path + os.sep + 'person_keys.py' 
+        keys_filename = self.config.config_path + os.sep + 'person_keys.py'
         old_user_keys = load_keys(keys_filename)
-        user_keys = {} 
+        user_keys = {}
         for user in users:
-            auth_hrn = config.SFA_INTERFACE_HRN 
+            auth_hrn = self.config.SFA_INTERFACE_HRN
             if user.tenantId is not None:
-                tenant = shell.auth_manager.tenants.find(id=user.tenantId)
-                auth_hrn = OSXrn(name=tenant.name, auth=config.SFA_INTERFACE_HRN, type='authority').get_hrn()
-            hrn = OSXrn(name=user.name, auth=auth_hrn, type='user').get_hrn() 
+                tenant = self.shell.auth_manager.tenants.find(id=user.tenantId)
+                auth_hrn = OSXrn(name=tenant.name, auth=self.config.SFA_INTERFACE_HRN, type='authority').get_hrn()
+            hrn = OSXrn(name=user.name, auth=auth_hrn, type='user').get_hrn()
             users_dict[hrn] = user
             old_keys = old_user_keys.get(hrn, [])
-            keys = [k.public_key for k in shell.nova_manager.keypairs.findall(name=hrn)]
+            keys = [k.public_key for k in self.shell.nova_manager.keypairs.findall(name=hrn)]
             user_keys[hrn] = 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:    
+                   (hrn, 'user') not in existing_records or update_record:
                 urn = OSXrn(xrn=hrn, type='user').get_urn()
-            
+
                 if keys:
                     try:
                         pkey = convert_public_key(keys[0])
@@ -83,7 +71,7 @@ class OpenstackImporter:
                         pkey = Keypair(create=True)
                 else:
                     self.logger.warn("OpenstackImporter: person %s does not have a PL public key"%hrn)
-                    pkey = Keypair(create=True) 
+                    pkey = Keypair(create=True)
                 user_gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
                 user_record = RegUser ()
                 user_record.type='user'
@@ -92,23 +80,26 @@ class OpenstackImporter:
                 user_record.authority=get_authority(hrn)
                 dbsession.add(user_record)
                 dbsession.commit()
-                self.logger.info("OpenstackImporter: imported person %s" % user_record)
+                self.logger.info("OpenstackImporter: imported person %s" % user_record)   
+
+        return users_dict, user_keys
 
-        # Get all tenants 
-        # A tenant can represent an organizational group (site) or a 
-        # slice. If a tenant's authorty/parent matches the root authority it is 
-        # considered a group/site. All other tenants are considered slices.         
-        tenants = shell.auth_manager.tenants.list()
+    def import_tenants(self, existing_hrns, existing_records):
+        # Get all tenants
+        # A tenant can represent an organizational group (site) or a
+        # slice. If a tenant's authorty/parent matches the root authority it is
+        # considered a group/site. All other tenants are considered slices.
+        tenants = self.shell.auth_manager.tenants.list()
         tenants_dict = {}
         for tenant in tenants:
-            hrn = config.SFA_INTERFACE_HRN + '.' + tenant.name
+            hrn = self.config.SFA_INTERFACE_HRN + '.' + tenant.name
             tenants_dict[hrn] = tenant
             authority_hrn = OSXrn(xrn=hrn, type='authority').get_authority_hrn()
 
             if hrn in existing_hrns:
                 continue
-        
-            if authority_hrn == config.SFA_INTERFACE_HRN:
+
+            if authority_hrn == self.config.SFA_INTERFACE_HRN:
                 # import group/site
                 record = RegAuthority()
                 urn = OSXrn(xrn=hrn, type='authority').get_urn()
@@ -123,7 +114,7 @@ class OpenstackImporter:
                 dbsession.add(record)
                 dbsession.commit()
                 self.logger.info("OpenstackImporter: imported authority: %s" % record)
-                
+
             else:
                 record = RegSlice ()
                 urn = OSXrn(xrn=hrn, type='slice').get_urn()
@@ -135,10 +126,28 @@ class OpenstackImporter:
                 record.authority=get_authority(hrn)
                 dbsession.add(record)
                 dbsession.commit()
-                self.logger.info("OpenstackImporter: imported slice: %s" % record)
+                self.logger.info("OpenstackImporter: imported slice: %s" % record) 
+
+        return tenants_dict
+
+    def run (self, options):
+        # we don't have any options for now
+        self.logger.info ("OpenstackImporter.run : to do")
+
+        # create dict of all existing sfa records
+        existing_records = {}
+        existing_hrns = []
+        key_ids = []
+        for record in dbsession.query(RegRecord):
+            existing_records[ (record.hrn, record.type,) ] = record
+            existing_hrns.append(record.hrn) 
+            
+
+        tenants_dict = self.import_tenants(existing_hrns, existing_records)
+        users_dict, user_keys = self.import_users(existing_hrns, existing_records)
                 
         # remove stale records    
-        system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
+        system_records = [self.interface_hrn, self.root_auth, self.interface_hrn + '.slicemanager']
         for (record_hrn, type) in existing_records.keys():
             if record_hrn in system_records:
                 continue
@@ -150,7 +159,7 @@ class OpenstackImporter:
             if type == 'user':
                 if record_hrn in users_dict:
                     continue  
-            elif type == 'slice':
+            elif type in['slice', 'authority']:
                 if record_hrn in tenants_dict:
                     continue
             else:
@@ -163,5 +172,6 @@ class OpenstackImporter:
                                    
         # save pub keys
         self.logger.info('OpenstackImporter: saving current pub keys')
+        keys_filename = self.config.config_path + os.sep + 'person_keys.py'
         save_keys(keys_filename, user_keys)