baefbdd85a38a9ab58f397452f514fb2e8661c4e
[sfa.git] / geni / methods / resolve.py
1 from geni.util.faults import *
2 from geni.util.excep import *
3 from geni.util.method import Method
4 from geni.util.parameter import Parameter, Mixed
5 from geni.util.auth import Auth
6 from geni.util.record import GeniRecord
7 from geni.util.debug import log
8
9 class resolve(Method):
10     """
11     Resolve a record.
12
13     @param cred credential string authorizing the caller
14     @param hrn human readable name to resolve
15     @return a list of record dictionaries or empty list     
16     """
17
18     interfaces = ['registry']
19     
20     accepts = [
21         Parameter(str, "Credential string"),
22         Parameter(str, "Human readable name (hrn)")
23         ]
24
25     returns = [GeniRecord]
26     
27     def call(self, cred, hrn):
28         
29         self.api.auth.check(cred, 'resolve')
30         
31         # is this a foreign record
32         if not hrn.startswith(self.api.hrn):
33             for registry in self.api.registries:
34                 if hrn.startswith(registry):
35                     records = self.api.registries[registry].resolve(self.api.credential, name)
36                     good_records = records   
37         else:
38             auth_hrn = self.api.auth.get_authority(hrn)
39             if not auth_hrn:
40                 auth_hrn = hrn
41             table = self.api.auth.get_auth_table(auth_hrn)
42             records = table.resolve('*', hrn)
43             good_records = []
44             for record in records:
45                 try:
46                     self.api.fill_record_info(record)
47                     good_records.append(record)
48                 except PlanetLabRecordDoesNotExist:
49                     # silently drop the ones that are missing in PL
50                     print >> log, "ignoring geni record ", record.get_name(), \
51                               " because pl record does not exist"
52                 table.remove(record)
53
54         dicts = [record.as_dict() for record in good_records]
55         if not dicts:
56             raise RecordNotFound(hrn)
57
58         return dicts    
59