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