the generic architecture had a serious flaw
[sfa.git] / sfa / methods / Renew.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.sfatime import utcparse
7
8 from sfa.trust.credential import Credential
9
10 from sfa.storage.parameter import Parameter
11
12 class Renew(Method):
13     """
14     Renews the resources in the specified slice or slivers by 
15     extending the lifetime.
16     
17     @param surn ([string]) List of URNs of to renew
18     @param credentials ([string]) of credentials
19     @param expiration_time (string) requested time of expiration
20     @param options (dict) options
21     """
22     interfaces = ['aggregate', 'slicemgr']
23     accepts = [
24         Parameter(type([str]), "Slice URN"),
25         Parameter(type([str]), "List of credentials"),
26         Parameter(str, "Expiration time in RFC 3339 format"),
27         Parameter(dict, "Options"),
28         ]
29     returns = Parameter(bool, "Success or Failure")
30
31     def call(self, urns, creds, expiration_time, options):
32
33         self.api.logger.info("interface: %s\ttarget-hrn: %s\tcaller-creds: %s\tmethod-name: %s"%(self.api.interface, urns, creds, self.name))
34
35         # Find the valid credentials
36         valid_creds = self.api.auth.checkCredentials(creds, 'renewsliver', urns,
37                       check_sliver_callback = self.api.driver.check_sliver_credentials)
38
39         # Validate that the time does not go beyond the credential's expiration time
40         requested_time = utcparse(expiration_time)
41         max_renew_days = int(self.api.config.SFA_MAX_SLICE_RENEW)
42         if requested_time > Credential(cred=valid_creds[0]).get_expiration():
43             raise InsufficientRights('Renewsliver: Credential expires before requested expiration time')
44         if requested_time > datetime.datetime.utcnow() + datetime.timedelta(days=max_renew_days):
45             raise Exception('Cannot renew > %s days from now' % max_renew_days)
46         return self.api.manager.Renew(self.api, urns, creds, expiration_time, options)
47