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