Merge branch 'master' into sqlalchemy
[sfa.git] / sfa / managers / registry_manager_openstack.py
index fc01fd1..aee536f 100644 (file)
@@ -1,5 +1,4 @@
 import types
-import time 
 # for get_key_from_incoming_ip
 import tempfile
 import os
@@ -13,15 +12,18 @@ from sfa.util.xrn import Xrn, get_authority, hrn_to_urn, urn_to_hrn
 from sfa.util.plxrn import hrn_to_pl_login_base
 from sfa.util.version import version_core
 from sfa.util.sfalogging import logger
+
 from sfa.trust.gid import GID 
 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.record import SfaRecord
-from sfa.storage.table import SfaTable
-from sfa.managers import registry_manager
 
-class RegistryManager(registry_manager.RegistryManager):
+from sfa.storage.persistentobjs import make_record,RegRecord
+from sfa.storage.alchemy import dbsession
+
+from sfa.managers.registry_manager import RegistryManager
+
+class RegistryManager(RegistryManager):
 
     def GetCredential(self, api, xrn, type, is_self=False):
         # convert xrn to hrn     
@@ -34,19 +36,19 @@ class RegistryManager(registry_manager.RegistryManager):
         auth_hrn = api.auth.get_authority(hrn)
         if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
             auth_hrn = hrn
-        # get record info
         auth_info = api.auth.get_auth_info(auth_hrn)
-        table = SfaTable()
-        records = table.findObjects({'type': type, 'hrn': hrn})
-        if not records:
-            raise RecordNotFound(hrn)
-        record = records[0]
+        # get record info
+        record=dbsession.query(RegRecord).filter_by(type=type,hrn=hrn).first()
+        if not record:
+            raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
     
         # verify_cancreate_credential requires that the member lists
         # (researchers, pis, etc) be filled in
-        self.driver.augment_records_with_testbed_info (record)
-        if not self.driver.is_enabled (record):
-              raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
+        logger.debug("get credential before augment dict, keys=%s"%record.__dict__.keys())
+        self.driver.augment_records_with_testbed_info (record.__dict__)
+        logger.debug("get credential after augment dict, keys=%s"%record.__dict__.keys())
+        if not self.driver.is_enabled (record.__dict__):
+              raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record.email))
     
         # get the callers gid
         # if this is a self cred the record's gid is the caller's gid
@@ -58,12 +60,12 @@ class RegistryManager(registry_manager.RegistryManager):
             caller_hrn = caller_gid.get_hrn()
         
         object_hrn = record.get_gid_object().get_hrn()
-        rights = api.auth.determine_user_rights(caller_hrn, record)
+        rights = api.auth.determine_user_rights(caller_hrn, record.__dict__)
         # make sure caller has rights to this object
         if rights.is_empty():
-            raise PermissionError(caller_hrn + " has no rights to " + record['name'])
+            raise PermissionError(caller_hrn + " has no rights to " + record.hrn)
     
-        object_gid = GID(string=record['gid'])
+        object_gid = GID(string=record.gid)
         new_cred = Credential(subject = object_gid.get_subject())
         new_cred.set_gid_caller(caller_gid)
         new_cred.set_gid_object(object_gid)
@@ -71,8 +73,8 @@ class RegistryManager(registry_manager.RegistryManager):
         #new_cred.set_pubkey(object_gid.get_pubkey())
         new_cred.set_privileges(rights)
         new_cred.get_privileges().delegate_all_privileges(True)
-        if 'expires' in record:
-            date = utcparse(record['expires'])
+        if hasattr(record,'expires'):
+            date = utcparse(record.expires)
             expires = datetime_to_epoch(date)
             new_cred.set_expiration(int(expires))
         auth_kind = "authority,ma,sa"
@@ -87,21 +89,21 @@ class RegistryManager(registry_manager.RegistryManager):
     # subject_record describes the subject of the relationships
     # ref_record contains the target values for the various relationships we need to manage
     # (to begin with, this is just the slice x person relationship)
-    def update_relations (self, subject_record, ref_record):
-        type=subject_record['type']
+    def update_relations (self, subject_obj, ref_obj):
+        type=subject_obj.type
         if type=='slice':
-            self.update_relation(subject_record, 'researcher', ref_record.get('researcher'), 'user')
+            self.update_relation(subject_obj, 'researcher', ref_obj.researcher, 'user')
         
     # field_key is the name of one field in the record, typically 'researcher' for a 'slice' record
     # hrns is the list of hrns that should be linked to the subject from now on
     # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
-    def update_relation (self, sfa_record, field_key, hrns, target_type):
+    def update_relation (self, record_obj, field_key, hrns, target_type):
         # locate the linked objects in our db
-        subject_type=sfa_record['type']
-        subject_id=sfa_record['pointer']
-        table = SfaTable()
-        link_sfa_records = table.find ({'type':target_type, 'hrn': hrns})
-        link_ids = [ rec.get('pointer') for rec in link_sfa_records ]
+        subject_type=record_obj.type
+        subject_id=record_obj.pointer
+        # get the 'pointer' field of all matching records
+        link_id_tuples = dbsession.query(RegRecord.pointer).filter_by(type=target_type).filter(RegRecord.hrn.in_(hrns)).all()
+        # sqlalchemy returns named tuples for columns
+        link_ids = [ tuple.pointer for tuple in link_id_tuples ]
         self.driver.update_relation (subject_type, target_type, subject_id, link_ids)
-