Add new option to allow lease reductions
The current prolong-for option allows to increment/prolong the duration of lease by passing a parameter. It could also allow to reduce the lease duration, but the parameter needs to be passed in a way that is unclear for the user (for example, --prolong-for " -1d") With this fix we added a new option to allow lease duration reductions with a similar option to the "prolong-for". Both options are mutually exclusive Change-Id: Ifa616b8d215ed2825f98bea64ce8419866d252d5 Closes-Bug: #1305129
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user