getting closer - far from perfect but tests should pass after-alchemy-1
Thierry Parmentelat [Thu, 19 Jan 2012 09:59:33 +0000 (10:59 +0100)]
sfa/client/sfi.py
sfa/managers/registry_manager.py
sfa/plc/pldriver.py
sfa/storage/persistentobjs.py

index 6f2d5b7..06733d5 100644 (file)
@@ -760,14 +760,14 @@ or version information about sfi itself
             sys.exit(1)
         rec_file = self.get_record_file(args[0])
         record = load_record_from_file(rec_file)
-        if record['type'] == "user":
-            if record.get_name() == self.user:
+        if record.type == "user":
+            if record.hrn == self.user:
                 cred = self.my_credential_string
             else:
                 cred = self.my_authority_credential_string()
-        elif record['type'] in ["slice"]:
+        elif record.type in ["slice"]:
             try:
-                cred = self.slice_credential_string(record.get_name())
+                cred = self.slice_credential_string(record.hrn)
             except ServerException, e:
                # XXX smbaker -- once we have better error return codes, update this
                # to do something better than a string compare
@@ -775,14 +775,14 @@ or version information about sfi itself
                    cred = self.my_authority_credential_string()
                else:
                    raise
-        elif record.get_type() in ["authority"]:
+        elif record.type in ["authority"]:
             cred = self.my_authority_credential_string()
-        elif record.get_type() == 'node':
+        elif record.type == 'node':
             cred = self.my_authority_credential_string()
         else:
-            raise "unknown record type" + record.get_type()
-        record = record.as_dict()
-        return self.registry().Update(record, cred)
+            raise "unknown record type" + record.type
+        record_dict = record.todict()
+        return self.registry().Update(record_dict, cred)
   
     def remove(self, options, args):
         "remove registry record by name (Remove)"
index ef00d9c..218d62b 100644 (file)
@@ -152,7 +152,7 @@ class RegistryManager:
         if full:
             # in full mode we get as much info as we can, which involves contacting the 
             # testbed for getting implementation details about the record
-            for record in local_dicts: logger.info("resolve augment %s"%record)
+            for record in local_dicts: logger.debug("resolve augment %s"%record)
             self.driver.augment_records_with_testbed_info(local_dicts)
 #            # also we fill the 'url' field for known authorities
 #            # used to be in the driver code, sounds like a poorman thing though
index 9a7db2d..78395b4 100644 (file)
@@ -469,6 +469,7 @@ class PlDriver (Driver):
             #    continue 
             sfa_info = {}
             type = record['type']
+            logger.info("fill_record_sfa_info - incoming record typed %s"%type)
             if (type == "slice"):
                 # all slice users are researchers
                 record['geni_urn'] = hrn_to_urn(record['hrn'], 'slice')
@@ -489,6 +490,7 @@ class PlDriver (Driver):
                 
             elif (type.startswith("authority")):
                 record['url'] = None
+                logger.info("fill_record_sfa_info - authority xherex")
                 if record['pointer'] != -1:
                     record['PI'] = []
                     record['operator'] = []
@@ -511,6 +513,7 @@ class PlDriver (Driver):
                 # xxx TODO: URI, LatLong, IP, DNS
     
             elif (type == "user"):
+                logger.info('setting user.email')
                 sfa_info['email'] = record.get("email", "")
                 sfa_info['geni_urn'] = hrn_to_urn(record['hrn'], 'user')
                 sfa_info['geni_certificate'] = record['gid'] 
index db62608..349d70d 100644 (file)
@@ -6,6 +6,7 @@ from sqlalchemy import Table, Column, MetaData, join, ForeignKey
 from sqlalchemy.orm import relationship, backref
 from sqlalchemy.orm import column_property
 from sqlalchemy.orm import object_mapper
+from sqlalchemy.orm import validates
 from sqlalchemy.ext.declarative import declarative_base
 
 from sfa.util.sfalogging import logger
@@ -58,9 +59,8 @@ class AlchemyObj:
     def load_from_dict (self, d):
         for (k,v) in d.iteritems():
             # experimental
-            if isinstance(v, StringTypes):
-                if v.lower() in ['true']: v=True
-                if v.lower() in ['false']: v=False
+            if isinstance(v, StringTypes) and v.lower() in ['true']: v=True
+            if isinstance(v, StringTypes) and v.lower() in ['false']: v=False
             setattr(self,k,v)
         assert self.type in BUILTIN_TYPES
     
@@ -74,21 +74,43 @@ class AlchemyObj:
     def load_from_xml (self, xml):
         xml_record = XML(xml)
         xml_dict = xml_record.todict()
-        for k in self.xml_fields():
-            if k in xml_dict:
-                setattr(self,k,xml_dict[k])
+        logger.info("load from xml, keys=%s"%xml_dict.keys())
+#        for k in self.xml_fields():
+        for (k,v) in xml_dict.iteritems():
+            setattr(self,k,v)
 
     def save_as_xml (self):
-        # xxx unset fields don't get exposed, is that right ?
+        # xxx not sure about the scope here
         input_dict = dict( [ (key, getattr(self.key), ) for key in self.xml_fields() if getattr(self,key,None) ] )
         xml_record=XML("<record />")
         xml_record.parse_dict (input_dict)
         return xml_record.toxml()
 
+    def dump(self, dump_parents=False):
+        for key in self.fields:
+            if key == 'gid' and self.gid:
+                gid = GID(string=self.gid)
+                print "    %s:" % key
+                gid.dump(8, dump_parents)
+            elif getattr(self,key,None):    
+                print "    %s: %s" % (key, getattr(self,key))
+    
+#    # only intended for debugging 
+#    def inspect (self, logger, message=""):
+#        logger.info("%s -- Inspecting AlchemyObj -- attrs"%message)
+#        for k in dir(self):
+#            if not k.startswith('_'):
+#                logger.info ("  %s: %s"%(k,getattr(self,k)))
+#        logger.info("%s -- Inspecting AlchemyObj -- __dict__"%message)
+#        d=self.__dict__
+#        for (k,v) in d.iteritems():
+#            logger.info("[%s]=%s"%(k,v))
+
+
 ##############################
 class Type (Base):
     __table__ = Table ('types', Base.metadata,
-                       Column ('type',String, primary_key=True)
+                       Column ('type',String, primary_key=True),
                        )
     def __init__ (self, type): self.type=type
     def __repr__ (self): return "<Type %s>"%self.type
@@ -121,7 +143,7 @@ class RegRecord (Base,AlchemyObj):
                        )
     fields = [ 'type', 'hrn', 'gid', 'authority', 'peer_authority' ]
     def __init__ (self, type='unknown', hrn=None, gid=None, authority=None, peer_authority=None, 
-                  pointer=-1, dict=None):
+                  pointer=None, dict=None):
         self.type=type
         if hrn: self.hrn=hrn
         if gid: 
@@ -129,7 +151,7 @@ class RegRecord (Base,AlchemyObj):
             else: self.gid=gid.save_to_string(save_parents=True)
         if authority: self.authority=authority
         if peer_authority: self.peer_authority=peer_authority
-        if not hasattr(self,'pointer'): self.pointer=pointer
+        if pointer: self.pointer=pointer
         if dict:
             self.load_from_dict (dict)
 
@@ -156,31 +178,43 @@ class RegRecord (Base,AlchemyObj):
         self.last_updated=now
 
 ##############################
+
 class User (Base):
     __table__ = Table ('users', Base.metadata,
-                       Column ('user_id', Integer, primary_key=True),
-                       Column ('record_id',Integer, ForeignKey('records.record_id')),
+                       Column ('record_id', Integer, ForeignKey ("records.record_id"), primary_key=True),
                        Column ('email', String),
                        )
     def __init__ (self, email):
         self.email=email
-    def __repr__ (self): return "<User(%d) %s, record_id=%d>"%(self.user_id,self.email,self.record_id,)
+    def __repr__ (self): return "[User(%d) email=%s>"%(self.record_id,self.email,)
+    
+    @validates('email') 
+    def validate_email(self, key, address):
+        assert '@' in address
+        return address
                            
-record_table = RegRecord.__table__
-user_table = User.__table__
-record_user_join = join (record_table, user_table)
-
-class UserRecord (Base):
-    __table__ = record_user_join
-    record_id = column_property (record_table.c.record_id, user_table.c.record_id)
-    user_id = user_table.c.user_id
-    def __init__ (self, gid, email):
-        self.type='user'
-        self.gid=gid
-        self.email=email
-    def __repr__ (self): return "<UserRecord %s %s>"%(self.email,self.gid)
+class Key (Base):
+    __table__ = Table ('keys', Base.metadata,
+                       Column ('key_id', Integer, primary_key=True),
+                       Column ('key',String),
+                       )
 
 ##############################
+#record_table = RegRecord.__table__
+#user_table = User.__table__
+#record_user_join = join (record_table, user_table)
+#
+#class UserRecord (Base):
+#    __table__ = record_user_join
+#    record_id = column_property (record_table.c.record_id, user_table.c.record_id)
+#    user_id = user_table.c.user_id
+#    def __init__ (self, gid, email):
+#        self.type='user'
+#        self.gid=gid
+#        self.email=email
+#    def __repr__ (self): return "<UserRecord %s %s>"%(self.email,self.gid)
+#
+##############################
 def init_tables(dbsession):
     logger.info("Initializing db schema and builtin types")
     # the doc states we could retrieve the engine this way