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