get caller's hrn from the credentials gid_origin_caller object
[sfa.git] / sfa / methods / resolve.py
1 ### $Id$
2 ### $URL$
3 import traceback
4 from sfa.util.faults import *
5 from sfa.util.method import Method
6 from sfa.util.parameter import Parameter, Mixed
7 from sfa.trust.auth import Auth
8 from sfa.util.record import GeniRecord
9 from sfa.util.genitable import GeniTable
10 from sfa.util.debug import log
11 from sfa.server.registry import Registries
12 from sfa.util.prefixTree import prefixTree
13 from sfa.trust.credential import Credential
14
15 class resolve(Method):
16     """
17     Resolve a record.
18
19     @param cred credential string authorizing the caller
20     @param hrn human readable name to resolve
21     @return a list of record dictionaries or empty list     
22     """
23
24     interfaces = ['registry']
25     
26     accepts = [
27         Parameter(str, "Credential string"),
28         Parameter(str, "Human readable name (hrn)"),
29         Mixed(Parameter(str, "Request hash"),
30               Parameter(None, "Request hash not specified"))
31         ]
32
33     returns = [GeniRecord]
34     
35     def call(self, cred, hrn, request_hash=None):
36         user_cred = Credential(string=cred)
37
38         #log the call
39         gid_origin_caller = user_cred.get_gid_origin_caller()
40         origin_hrn = gid_origin_caller.get_hrn()
41         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
42  
43         self.api.auth.authenticateCred(cred, [cred, hrn], request_hash) 
44         self.api.auth.check(cred, 'resolve')
45
46         # load all know registry names into a prefix tree and attempt to find
47         # the longest matching prefix
48         good_records = [] 
49         registries = Registries(self.api)
50         hrns = registries.keys()
51         tree = prefixTree()
52         tree.load(hrns)
53         registry_hrn = tree.best_match(hrn)
54
55         #if there was no match then this record belongs to an unknow registry
56         if not registry_hrn:
57             raise MissingAuthority(hrn)
58
59         # if the best match (longest matching hrn) is not the local registry,
60         # forward the request
61         if registry_hrn != self.api.hrn:
62             credential = self.api.getCredential()
63             credential.set_gid_origin_caller(gid_origin_caller)
64             try:
65                 request_hash=None
66                 records = registries[registry_hrn].resolve(credential, hrn, request_hash)
67                 good_records = [GeniRecord(dict=record).as_dict() for record in records]
68             except:
69                 arg_list = [credential, hrn]
70                 request_hash=self.api.key.compute_hash(arg_list)                
71                 records = registries[registry_hrn].resolve(credential, hrn, request_hash)
72                 good_records = [GeniRecord(dict=record).as_dict() for record in records]
73                 
74         if good_records:
75             return good_records
76
77         # if we still havnt found the record yet, try the local registry
78         table = GeniTable()
79         records = table.findObjects(hrn)
80         if not records:
81             raise RecordNotFound(hrn) 
82         for record in records:
83             try:
84                 self.api.fill_record_info(record)
85                 good_records.append(dict(record))
86             except PlanetLabRecordDoesNotExist:
87                 # silently drop the ones that are missing in PL
88                 print >> log, "ignoring geni record ", record['hrn'], \
89                               " because pl record does not exist"
90                 table.remove(record)
91
92
93         return good_records    
94