diff --git a/climateclient/v1/leases.py b/climateclient/v1/leases.py index a509a19..54c6f01 100644 --- a/climateclient/v1/leases.py +++ b/climateclient/v1/leases.py @@ -36,19 +36,22 @@ class LeaseClientManager(base.BaseClientManager): """ return self._get('/leases/%s' % lease_id, 'lease') - def update(self, lease_id, name=None, prolong_for=None): + def update(self, lease_id, name=None, prolong_for=None, reduce_by=None): """Update attributes of the lease.""" values = {} if name: values['name'] = name - if prolong_for: - if prolong_for.endswith('s'): + + lease_length_option = prolong_for or reduce_by + + if lease_length_option: + if lease_length_option.endswith('s'): coefficient = 1 - elif prolong_for.endswith('m'): + elif lease_length_option.endswith('m'): coefficient = 60 - elif prolong_for.endswith('h'): + elif lease_length_option.endswith('h'): coefficient = 60 * 60 - elif prolong_for.endswith('d'): + elif lease_length_option.endswith('d'): coefficient = 24 * 60 * 60 else: raise exception.ClimateClientException(_("Unsupportable date " @@ -57,7 +60,8 @@ class LeaseClientManager(base.BaseClientManager): lease = self.get(lease_id) cur_end_date = datetime.datetime.strptime(lease['end_date'], '%Y-%m-%dT%H:%M:%S.%f') - seconds = int(prolong_for[:-1]) * coefficient + coefficient = coefficient * (1 if prolong_for else -1) + seconds = int(lease_length_option[:-1]) * coefficient delta_sec = datetime.timedelta(seconds=seconds) new_end_date = cur_end_date + delta_sec values['end_date'] = datetime.datetime.strftime( diff --git a/climateclient/v1/shell_commands/leases.py b/climateclient/v1/shell_commands/leases.py index d82a246..81d02e2 100644 --- a/climateclient/v1/shell_commands/leases.py +++ b/climateclient/v1/shell_commands/leases.py @@ -220,16 +220,23 @@ class UpdateLease(command.UpdateCommand): help='New name for the lease', default=None ) - parser.add_argument( + #prolong-for and reduce_by are mutually exclusive + group = parser.add_mutually_exclusive_group() + group.add_argument( '--prolong-for', help='Time to prolong lease for', default=None ) - parser.add_argument( + group.add_argument( '--prolong_for', help=argparse.SUPPRESS, default=None ) + group.add_argument( + '--reduce-by', + help='Time to reduce lease by', + default=None + ) return parser def args2body(self, parsed_args): @@ -238,6 +245,8 @@ class UpdateLease(command.UpdateCommand): params['name'] = parsed_args.name if parsed_args.prolong_for: params['prolong_for'] = parsed_args.prolong_for + if parsed_args.reduce_by: + params['reduce_by'] = parsed_args.reduce_by return params