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