c1d87143974e70ccf755c306cf01aa9a6a7160fa
[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, caller_cred=None):
36         
37         self.api.auth.authenticateCred(cred, [cred, hrn], request_hash) 
38         self.api.auth.check(cred, 'resolve')
39         if caller_cred==None:
40             caller_cred=cred
41
42         #log the call
43         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(), hrn, self.name))
44         good_records = [] 
45
46         # load all know registry names into a prefix tree and attempt to find
47         # the longest matching prefix
48         registries = Registries(self.api)
49         hrns = registries.keys()
50         tree = prefixTree()
51         tree.load(hrns)
52         registry_hrn = tree.best_match(hrn)
53
54         #if there was no match then this record belongs to an unknow registry
55         if not registry_hrn:
56             raise MissingAuthority(hrn)
57
58         # if the best match (longest matching hrn) is not the local registry,
59         # forward the request
60         if registry_hrn != self.api.hrn:
61             credential = self.api.getCredential()
62             try:
63                 records = registries[registry_hrn].resolve(credential, hrn, caller_cred=caller_cred)
64                 good_records = [record.as_dict() for record in records]
65                 if good_records:
66                     return good_records
67             except:
68                 traceback.print_exc()
69
70         # if we still havnt found the record yet, try the local registry
71         table = GeniTable()
72         records = table.findObjects(hrn)
73         if not records:
74             raise RecordNotFound(hrn) 
75         for record in records:
76             try:
77                 self.api.fill_record_info(record)
78                 good_records.append(dict(record))
79             except PlanetLabRecordDoesNotExist:
80                 # silently drop the ones that are missing in PL
81                 print >> log, "ignoring geni record ", record['hrn'], \
82                               " because pl record does not exist"
83                 table.remove(record)
84
85
86         return good_records    
87