add request_hash argument to more method calls
[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.misc import *
9 from sfa.util.method import Method
10 from sfa.util.parameter import Parameter, Mixed
11 from sfa.util.record import GeniRecord
12 from sfa.util.genitable import GeniTable
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
17
18 class register_peer_object(Method):
19     """
20     Register a peer object with the registry. In addition to being stored in the
21     Geni database, the appropriate records will also be created in the
22     PLC databases
23     
24     @param cred credential string
25     @param record_dict dictionary containing record fields
26     
27     @return gid string representation
28     """
29
30     interfaces = ['registry']
31     
32     accepts = [
33         Parameter(str, "Credential string"),
34         Parameter(dict, "Record dictionary containing record fields")
35         Parameter(str, "Request hash")
36         ]
37
38     returns = Parameter(int, "1 if successful")
39     
40     def call(self, cred, record_dict, request_hash, caller_cred=None):
41         self.api.auth.authenticateCred(cred, [cred], request_hash)
42         self.api.auth.check(cred, "register")
43         if caller_cred==None:
44                 caller_cred=cred
45                 
46         #log the call
47         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, Credential(string=caller_cred).get_gid_caller().get_hrn(), None, self.name))
48
49         # make sure this is a peer record
50         if 'peer_authority' not in record_dict or \
51            not record_dict['peer_authority']: 
52             raise GeniInvalidArgument, "peer_authority must be specified" 
53
54         record = GeniRecord(dict = record_dict)
55         type, hrn, peer_authority = record['type'], record['hrn'], record['peer_authority']
56         record['authority'] = get_authority(record['hrn'])
57         # verify permissions
58         self.api.auth.verify_cred_is_me(cred)
59
60         # check if record already exists
61         table = GeniTable()
62         existing_records = table.find({'type': type, 'hrn': hrn, 'peer_authority': peer_authority})
63         if existing_records:
64             for existing_record in existing_records:
65                 if existing_record['pointer'] != record['pointer']:
66                     record['record_id'] = existing_record['record_id']
67                     table.update(record)
68             return 1
69         record_id = table.insert(record)
70  
71         return 1