0007c2550bb3a7a852a7a1af21892d8e183f3370
[sfa.git] / sfa / methods / ListResources.py
1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter, Mixed
5 from sfa.trust.credential import Credential
6 from sfatables.runtime import SFATablesRules
7 import sys
8
9
10 class ListResources(Method):
11     """
12     Returns information about available resources or resources allocated to this    slice
13     @param credential list
14     @param options dictionary
15     @return string
16     """
17     interfaces = ['geni_am']
18     accepts = [
19         Parameter(type([str]), "List of credentials"),
20         Parameter(dict, "Options")
21         ]
22     returns = Parameter(str, "List of resources")
23
24     def call(self, creds, options):
25         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
26             
27         # Validate that at least one of the credentials is good enough
28         found = False
29         for cred in creds:
30             try:
31                 self.api.auth.check(cred, 'listnodes')
32                 found = True
33                 user_cred = Credential(string=cred)
34                 break
35             except:
36                 error = sys.exc_info()[:2]
37                 continue
38             
39         if not found:
40             raise InsufficientRights('ListResources: Access denied: %s -- %s' % (error[0],error[1]))
41         
42         origin_hrn = user_cred.get_gid_caller().get_hrn()
43                     
44         manager_base = 'sfa.managers'
45
46         if self.api.interface in ['geni_am']:
47             mgr_type = self.api.config.SFA_GENI_AGGREGATE_TYPE
48             manager_module = manager_base + ".geni_am_%s" % mgr_type
49             manager = __import__(manager_module, fromlist=[manager_base])
50             rspec = manager.ListResources(self.api, creds, options)
51             outgoing_rules = SFATablesRules('OUTGOING')
52             
53         
54         filtered_rspec = rspec
55         if outgoing_rules.sorted_rule_list:
56             context = {'sfa':{'user':{'hrn':origin_hrn}, 'slice':{'hrn':None}}}
57             outgoing_rules.set_context(context)
58             filtered_rspec = outgoing_rules.apply(rspec)      
59
60         return filtered_rspec  
61     
62