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