avoid as much as possible accessing logger through class instances, whenever that...
[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, add_datetime
7 from sfa.util.sfalogging import logger
8
9 from sfa.trust.credential import Credential
10
11 from sfa.storage.parameter import Parameter
12
13
14 class Renew(Method):
15     """
16     Renews the resources in the specified slice or slivers by
17     extending the lifetime.
18
19     @param urns ([string]) List of URNs of to renew
20     @param credentials ([string]) of credentials
21     @param expiration_time (string) requested time of expiration
22     @param options (dict) options
23     """
24     interfaces = ['aggregate', 'slicemgr']
25     accepts = [
26         Parameter(type([str]), "Slice URN"),
27         Parameter(type([str]), "List of credentials"),
28         Parameter(str, "Expiration time in RFC 3339 format"),
29         Parameter(dict, "Options"),
30     ]
31     returns = Parameter(bool, "Success or Failure")
32
33     def call(self, urns, creds, expiration_time, options):
34
35         # Find the valid credentials
36         valid_creds = self.api.auth.checkCredentialsSpeaksFor(
37             creds, 'renewsliver', urns,
38             check_sliver_callback=self.api.driver.check_sliver_credentials,
39             options=options)
40         the_credential = Credential(cred=valid_creds[0])
41         actual_caller_hrn = the_credential.actual_caller_hrn()
42         logger.info("interface: %s\tcaller-hrn: %s\ttarget-urns: %s\texpiration:%s\tmethod-name: %s" %
43                     (self.api.interface, actual_caller_hrn, urns, expiration_time, self.name))
44
45         # extend as long as possible : take the min of requested and
46         # now+SFA_MAX_SLICE_RENEW
47         if options.get('geni_extend_alap'):
48             # ignore requested time and set to max
49             expiration_time = add_datetime(datetime.datetime.utcnow(
50             ), days=int(self.api.config.SFA_MAX_SLICE_RENEW))
51
52         # Validate that the time does not go beyond the credential's expiration
53         # time
54         requested_expire = utcparse(expiration_time)
55         logger.info("requested_expire = %s" % requested_expire)
56         credential_expire = the_credential.get_expiration()
57         logger.info("credential_expire = %s" % credential_expire)
58         max_renew_days = int(self.api.config.SFA_MAX_SLICE_RENEW)
59         max_expire = datetime.datetime.utcnow() + datetime.timedelta(days=max_renew_days)
60         if requested_expire > credential_expire:
61             # used to throw an InsufficientRights exception here, this was not
62             # right
63             logger.warning("Requested expiration %s, after credential expiration (%s) -> trimming to the latter/sooner" %
64                            (requested_expire, credential_expire))
65             requested_expire = credential_expire
66         if requested_expire > max_expire:
67             # likewise
68             logger.warning("Requested expiration %s, after maximal expiration %s days (%s) -> trimming to the latter/sooner" %
69                            (requested_expire, self.api.config.SFA_MAX_SLICE_RENEW, max_expire))
70             requested_expire = max_expire
71
72         return self.api.manager.Renew(self.api, urns, creds, requested_expire, options)