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