1 ### $Id: register.py 15001 2009-09-11 20:18:54Z tmack $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/methods/register.py $
4 from sfa.trust.certificate import Keypair, convert_public_key
5 from sfa.trust.gid import *
7 from sfa.util.faults import *
8 from sfa.util.namespace import *
9 from sfa.util.method import Method
10 from sfa.util.parameter import Parameter, Mixed
11 from sfa.util.record import SfaRecord
12 from sfa.util.table import SfaTable
13 from sfa.util.debug import log
14 from sfa.trust.auth import Auth
15 from sfa.trust.gid import create_uuid
16 from sfa.trust.credential import Credential
18 class register_peer_object(Method):
20 Register a peer object with the registry. In addition to being stored in the
21 SFA database, the appropriate records will also be created in the
24 @param cred credential string
25 @param record_dict dictionary containing record fields
26 @return gid string representation
29 interfaces = ['registry']
32 Parameter(str, "Credential string"),
33 Parameter(dict, "Record dictionary containing record fields"),
34 Mixed(Parameter(str, "Human readable name of the original caller"),
35 Parameter(None, "Origin hrn not specified"))
38 returns = Parameter(int, "1 if successful")
40 def call(self, cred, record_dict, origin_hrn=None):
41 user_cred = Credential(string=cred)
45 origin_hrn = user_cred.get_gid_caller().get_hrn()
46 self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, None, self.name))
49 self.api.auth.check(cred, "register")
51 # make sure this is a peer record
52 if 'peer_authority' not in record_dict or \
53 not record_dict['peer_authority']:
54 raise SfaInvalidArgument, "peer_authority must be specified"
56 record = SfaRecord(dict = record_dict)
57 type, hrn, peer_authority = record['type'], record['hrn'], record['peer_authority']
58 record['authority'] = get_authority(record['hrn'])
60 self.api.auth.verify_cred_is_me(cred)
62 # check if record already exists
64 existing_records = table.find({'type': type, 'hrn': hrn, 'peer_authority': peer_authority})
66 for existing_record in existing_records:
67 if existing_record['pointer'] != record['pointer']:
68 record['record_id'] = existing_record['record_id']
71 record_id = table.insert(record)