Commit ea21932b authored by cmart's avatar cmart

Handle elapsed_time params in climate client

Adding elapsed_time_to_seconds function on utils module in order to handle the
elapsed time params used by climate client in a more safer way.

Change-Id: I30a211106b87bbb09559bb1b889a275707cd4637
Closes-Bug: #1312851
parent 24cc2cc2
......@@ -19,13 +19,15 @@ import os
import re
import six
from climate.openstack.common import timeutils
from climateclient import exception
from climateclient.openstack.common.gettextutils import _ # noqa
HEX_ELEM = '[0-9A-Fa-f]'
UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}',
HEX_ELEM + '{4}', HEX_ELEM + '{4}',
HEX_ELEM + '{12}'])
ELAPSED_TIME_REGEX = '^(\d+)([s|m|h|d])$'
def env(*args, **kwargs):
......@@ -135,3 +137,28 @@ def _find_resource_id_by_name(client, resource, name):
message = "Unable to find %s with name '%s'" % (resource, name)
raise exception.ClimateClientException(message=message,
status_code=404)
def from_elapsed_time_to_seconds(elapsed_time):
"""Return the amount of seconds based on the time_option parameter
:param: time_option: a string that matches ELAPSED_TIME_REGEX
"""
is_elapsed_time = re.match(ELAPSED_TIME_REGEX, elapsed_time)
if is_elapsed_time is None:
raise exception.ClimateClientException(_("Invalid time "
"format for option."))
elapsed_time_value = int(is_elapsed_time.group(1))
elapsed_time_option = is_elapsed_time.group(2)
seconds = {
's': lambda x:
timeutils.total_seconds(datetime.timedelta(seconds=x)),
'm': lambda x:
timeutils.total_seconds(datetime.timedelta(minutes=x)),
'h': lambda x:
timeutils.total_seconds(datetime.timedelta(hours=x)),
'd': lambda x:
timeutils.total_seconds(datetime.timedelta(days=x)),
}[elapsed_time_option](elapsed_time_value)
# the above code returns a "float"
return int(seconds)
......@@ -16,8 +16,8 @@
import datetime
from climateclient import base
from climateclient import exception
from climateclient.openstack.common.gettextutils import _ # noqa
from climateclient import utils
class LeaseClientManager(base.BaseClientManager):
......@@ -45,24 +45,13 @@ class LeaseClientManager(base.BaseClientManager):
lease_length_option = prolong_for or reduce_by
if lease_length_option:
if lease_length_option.endswith('s'):
coefficient = 1
elif lease_length_option.endswith('m'):
coefficient = 60
elif lease_length_option.endswith('h'):
coefficient = 60 * 60
elif lease_length_option.endswith('d'):
coefficient = 24 * 60 * 60
else:
raise exception.ClimateClientException(_("Unsupportable date "
"format for lease "
"prolonging."))
seconds = utils.from_elapsed_time_to_seconds(lease_length_option)
seconds = seconds * (1 if prolong_for else -1)
delta_sec = datetime.timedelta(seconds=seconds)
lease = self.get(lease_id)
cur_end_date = datetime.datetime.strptime(lease['end_date'],
'%Y-%m-%dT%H:%M:%S.%f')
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(
new_end_date, '%Y-%m-%d %H:%M'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment