51fbe25caed145adaebc0a931f907eb0d17e6a04
[sfa.git] / sfa / methods / register_peer_object.py
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 $
3
4 from sfa.trust.certificate import Keypair, convert_public_key
5 from sfa.trust.gid import *
6
7 from sfa.util.faults import *
8 from sfa.util.namespace import get_authority
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.trust.auth import Auth
14 from sfa.trust.gid import create_uuid
15 from sfa.trust.credential import Credential
16
17 class register_peer_object(Method):
18     """
19     Register a peer object with the registry. In addition to being stored in the
20     SFA database, the appropriate records will also be created in the
21     PLC databases
22     
23     @param cred credential string
24     @param record_dict dictionary containing record fields
25     @return gid string representation
26     """
27
28     interfaces = ['registry']
29     
30     accepts = [
31         Parameter(str, "Credential string"),
32         Parameter(dict, "Record dictionary containing record fields"),
33         Mixed(Parameter(str, "Human readable name of the original caller"),
34               Parameter(None, "Origin hrn not specified"))
35         ]
36
37     returns = Parameter(int, "1 if successful")
38     
39     def call(self, cred, record_dict, origin_hrn=None):
40         user_cred = Credential(string=cred)
41
42         #log the call
43         if not origin_hrn:
44             origin_hrn = user_cred.get_gid_caller().get_hrn()    
45         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, None, self.name))
46
47         # validate the cred
48         self.api.auth.check(cred, "register")
49
50         # make sure this is a peer record
51         if 'peer_authority' not in record_dict or \
52            not record_dict['peer_authority']: 
53             raise SfaInvalidArgument, "peer_authority must be specified" 
54
55         record = SfaRecord(dict = record_dict)
56         type, hrn, peer_authority = record['type'], record['hrn'], record['peer_authority']
57         record['authority'] = get_authority(record['hrn'])
58         # verify permissions
59         self.api.auth.verify_cred_is_me(cred)
60
61         # check if record already exists
62         table = SfaTable()
63         existing_records = table.find({'type': type, 'hrn': hrn, 'peer_authority': peer_authority})
64         if existing_records:
65             for existing_record in existing_records:
66                 if existing_record['pointer'] != record['pointer']:
67                     record['record_id'] = existing_record['record_id']
68                     table.update(record)
69         else:
70             record_id = table.insert(record)
71  
72         return 1