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