2to3 -f has_key
[sfa.git] / sfa / methods / ListResources.py
1 import zlib
2
3 from sfa.util.xrn import urn_to_hrn
4 from sfa.util.method import Method
5 from sfa.util.sfatablesRuntime import run_sfatables
6 from sfa.util.faults import SfaInvalidArgument
7 from sfa.trust.credential import Credential
8
9 from sfa.storage.parameter import Parameter, Mixed
10
11 class ListResources(Method):
12     """
13     Returns information about available resources
14     @param credential list
15     @param options dictionary
16     @return string
17     """
18     interfaces = ['aggregate', 'slicemgr']
19     accepts = [
20         Mixed(Parameter(str, "Credential string"), 
21               Parameter(type([str]), "List of credentials")),
22         Parameter(dict, "Options")
23         ]
24     returns = Parameter(str, "List of resources")
25
26     def call(self, creds, options):
27         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
28        
29         # client must specify a version
30         if not options.get('geni_rspec_version'):
31             if options.get('rspec_version'):
32                 options['geni_rspec_version'] = options['rspec_version']
33             else:
34                 raise SfaInvalidArgument('Must specify an rspec version option. geni_rspec_version cannot be null')
35
36         # Find the valid credentials
37         valid_creds = self.api.auth.checkCredentialsSpeaksFor(creds, 'listnodes', options=options)
38
39         # get hrn of the original caller 
40         origin_hrn = options.get('origin_hrn', None)
41         if not origin_hrn:
42             origin_hrn = Credential(cred=valid_creds[0]).get_gid_caller().get_hrn()
43         rspec = self.api.manager.ListResources(self.api, creds, options)
44
45         # filter rspec through sfatables 
46         if self.api.interface in ['aggregate']:
47             chain_name = 'OUTGOING'
48         elif self.api.interface in ['slicemgr']: 
49             chain_name = 'FORWARD-OUTGOING'
50         self.api.logger.debug("ListResources: sfatables on chain %s"%chain_name)
51         filtered_rspec = run_sfatables(chain_name, '', origin_hrn, rspec) 
52  
53         if 'geni_compressed' in options and options['geni_compressed'] == True:
54             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
55
56         return filtered_rspec  
57     
58