changed the sfa db schema. All records are now stored in 1 table instead of createing...
[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
14 class resolve(Method):
15     """
16     Resolve a record.
17
18     @param cred credential string authorizing the caller
19     @param hrn human readable name to resolve
20     @return a list of record dictionaries or empty list     
21     """
22
23     interfaces = ['registry']
24     
25     accepts = [
26         Parameter(str, "Credential string"),
27         Parameter(str, "Human readable name (hrn)")
28         ]
29
30     returns = [GeniRecord]
31     
32     def call(self, cred, hrn):
33         
34         self.api.auth.check(cred, 'resolve')
35         good_records = [] 
36
37         # load all know registry names into a prefix tree and attempt to find
38         # the longest matching prefix
39         registries = Registries(self.api)
40         hrns = registries.keys()
41         tree = prefixTree()
42         tree.load(hrns)
43         registry_hrn = tree.best_match(hrn)
44
45         #if there was no match then this record belongs to an unknow registry
46         if not registry_hrn:
47             raise MissingAuthority(hrn)
48
49         # if the best match (longest matching hrn) is not the local registry,
50         # forward the request
51         if registry_hrn != self.api.hrn:
52             credential = self.api.getCredential()
53             try:
54                 records = registries[registry_hrn].resolve(credential, hrn)
55                 good_records = [record.as_dict() for record in records]
56                 if good_records:
57                     return good_records
58             except:
59                 traceback.print_exc()
60
61         # if we still havnt found the record yet, try the local registry
62         table = GeniTable()
63         records = table.find(hrn)
64         if not records:
65             raise RecordNotFound(hrn) 
66         for record in records:
67             try:
68                 self.api.fill_record_info(record)
69                 good_records.append(dict(record))
70             except PlanetLabRecordDoesNotExist:
71                 # silently drop the ones that are missing in PL
72                 print >> log, "ignoring geni record ", record.get_name(), \
73                               " because pl record does not exist"
74                 table.remove(record)
75
76
77         return good_records    
78