1669517abcc0306c74204061abaa9d896e41017a
[sfa.git] / sfa / methods / RenewSliver.py
1 from sfa.util.faults import *
2 from sfa.util.xrn import urn_to_hrn
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter
5 from sfa.trust.credential import Credential
6 from sfa.util.sfatime import utcparse
7 import datetime
8
9 class RenewSliver(Method):
10     """
11     Renews the resources in a sliver, extending the lifetime of the slice.    
12     @param slice_urn (string) URN of slice to renew
13     @param credentials ([string]) of credentials
14     @param expiration_time (string) requested time of expiration
15     
16     """
17     interfaces = ['aggregate', 'slicemgr']
18     accepts = [
19         Parameter(str, "Slice URN"),
20         Parameter(type([str]), "List of credentials"),
21         Parameter(str, "Expiration time in RFC 3339 format"),
22         Parameter(str, "call_id"),
23         ]
24     returns = Parameter(bool, "Success or Failure")
25
26     def call(self, slice_xrn, creds, expiration_time, call_id=""):
27
28         (hrn, type) = urn_to_hrn(slice_xrn)
29
30         self.api.logger.info("interface: %s\ttarget-hrn: %s\tcaller-creds: %s\tmethod-name: %s"%(self.api.interface, hrn, creds, self.name))
31
32         # Find the valid credentials
33         valid_creds = self.api.auth.checkCredentials(creds, 'renewsliver', hrn)
34
35         # Validate that the time does not go beyond the credential's expiration time
36         requested_time = utcparse(expiration_time)
37         max_renew_days = int(self.api.config.SFA_MAX_SLICE_RENEW)
38         if requested_time > Credential(string=valid_creds[0]).get_expiration():
39             raise InsufficientRights('Renewsliver: Credential expires before requested expiration time')
40         if requested_time > datetime.datetime.utcnow() + datetime.timedelta(days=max_renew_days):
41             raise Exception('Cannot renew > %s days from now' % max_renew_days)
42         manager = self.api.get_interface_manager()
43         return manager.RenewSliver(self.api, slice_xrn, valid_creds, expiration_time, call_id)    
44