554926df2252d77cbdae4d6d3c81495322efb23a
[sfa.git] / sfa / methods / RenewSliver.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
5 from sfa.trust.credential import Credential
6 from dateutil.parser import parse
7
8 class RenewSliver(Method):
9     """
10     Renews the resources in a sliver, extending the lifetime of the slice.    
11     @param slice_urn (string) URN of slice to renew
12     @param credentials ([string]) of credentials
13     @param expiration_time (string) requested time of expiration
14     
15     """
16     interfaces = ['geni_am']
17     accepts = [
18         Parameter(str, "Slice URN"),
19         Parameter(type([str]), "List of credentials"),
20         Parameter(str, "Expiration time in RFC 3339 format")
21         ]
22     returns = Parameter(bool, "Success or Failure")
23
24     def call(self, slice_xrn, creds, expiration_time):
25         hrn, type = urn_to_hrn(slice_xrn)
26
27         self.api.logger.info("interface: %s\ttarget-hrn: %s\tcaller-creds: %s\tmethod-name: %s"%(self.api.interface, hrn, creds, self.name))
28
29         # Validate that at least one of the credentials is good enough
30         found = False
31         validCred = None
32         for cred in creds:
33             try:
34                 self.api.auth.check(cred, 'renewsliver')
35                 validCred = cred
36                 found = True
37                 break
38             except:
39                 continue
40             
41         if not found:
42             raise InsufficientRights('SliverStatus: Credentials either did not verify, were no longer valid, or did not have appropriate privileges')
43             
44         # Validate that the time does not go beyond the credential's expiration time
45         requested_time = parse(expiration_time)
46         if requested_time > Credential(string=validCred).get_lifetime():
47             raise InsufficientRights('SliverStatus: Credential expires before requested expiration time')
48         
49         manager_base = 'sfa.managers'
50
51         if self.api.interface in ['geni_am']:
52             mgr_type = self.api.config.SFA_GENI_AGGREGATE_TYPE
53             manager_module = manager_base + ".geni_am_%s" % mgr_type
54             manager = __import__(manager_module, fromlist=[manager_base])
55             return manager.RenewSliver(self.api, slice_xrn, creds, expiration_time)
56
57         return ''
58