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