From 464dc67b0bf861f552f36dbe1ff9217ce3fc9909 Mon Sep 17 00:00:00 2001 From: Cristian A Sanchez Date: Tue, 29 Apr 2014 16:38:45 -0300 Subject: [PATCH] Add the option defer-by to postpone start_date Climate client adds an argument --defer-by to the command 'update' to change the start date of a lease by postponing it. This works similar to prolong-for and reduce-by, as it receives an amount of time like in seconds, minutes, hours and days Closes-Bug: #1311266 Change-Id: Ia8843b9baf9773e6e019f5ed5f1e4f13887f4537 --- climateclient/utils.py | 27 ++++++++++++--- climateclient/v1/leases.py | 42 +++++++++++++++-------- climateclient/v1/shell_commands/leases.py | 11 ++++++ 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/climateclient/utils.py b/climateclient/utils.py index 45d7880..9371470 100644 --- a/climateclient/utils.py +++ b/climateclient/utils.py @@ -29,6 +29,9 @@ UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}', HEX_ELEM + '{12}']) ELAPSED_TIME_REGEX = '^(\d+)([s|m|h|d])$' +LEASE_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%f' +API_DATE_FORMAT = '%Y-%m-%d %H:%M' + def env(*args, **kwargs): """Returns the first environment variable set. @@ -139,9 +142,12 @@ def _find_resource_id_by_name(client, resource, name): 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 +def from_elapsed_time_to_seconds(elapsed_time, pos_sign=True): + """Return the positive or negative amount of seconds based on the + elapsed_time parameter with a sign depending on the sign parameter. + :param: elapsed_time: a string that matches ELAPSED_TIME_REGEX + :param: sign: if pos_sign is True, the returned value will be positive. + Otherwise it will be positive. """ is_elapsed_time = re.match(ELAPSED_TIME_REGEX, elapsed_time) if is_elapsed_time is None: @@ -161,4 +167,17 @@ def from_elapsed_time_to_seconds(elapsed_time): }[elapsed_time_option](elapsed_time_value) # the above code returns a "float" - return int(seconds) + if pos_sign: + return int(seconds) + return int(seconds) * -1 + + +def from_elapsed_time_to_delta(elapsed_time, pos_sign=True): + """Return the positive or negative delta time based on the + elapsed_time parameter. + :param: elapsed_time: a string that matches ELAPSED_TIME_REGEX + :param: sign: if sign is True, the returned value will be negative. + Otherwise it will be positive. + """ + seconds = from_elapsed_time_to_seconds(elapsed_time, pos_sign=pos_sign) + return datetime.timedelta(seconds=seconds) diff --git a/climateclient/v1/leases.py b/climateclient/v1/leases.py index 6e77f03..4ed417b 100644 --- a/climateclient/v1/leases.py +++ b/climateclient/v1/leases.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime - from climateclient import base from climateclient.openstack.common.gettextutils import _ # noqa +from climateclient.openstack.common import timeutils from climateclient import utils @@ -36,26 +35,30 @@ class LeaseClientManager(base.BaseClientManager): """ return self._get('/leases/%s' % lease_id, 'lease') - def update(self, lease_id, name=None, prolong_for=None, reduce_by=None): + def update(self, lease_id, name=None, prolong_for=None, reduce_by=None, + defer_by=None): """Update attributes of the lease.""" values = {} if name: values['name'] = name - lease_length_option = prolong_for or reduce_by - - if lease_length_option: - 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_end_date_change = prolong_for or reduce_by + lease_start_date_change = defer_by + lease = None + if lease_end_date_change: lease = self.get(lease_id) - cur_end_date = datetime.datetime.strptime(lease['end_date'], - '%Y-%m-%dT%H:%M:%S.%f') - new_end_date = cur_end_date + delta_sec - values['end_date'] = datetime.datetime.strftime( - new_end_date, '%Y-%m-%d %H:%M' - ) + self._add_lease_date(values, lease, 'end_date', + lease_end_date_change, + prolong_for is not None) + + if lease_start_date_change: + if lease is None: + lease = self.get(lease_id) + self._add_lease_date(values, lease, 'start_date', + lease_start_date_change, + True) + if not values: return _('No values to update passed.') return self._update('/leases/%s' % lease_id, values, @@ -71,3 +74,12 @@ class LeaseClientManager(base.BaseClientManager): if sort_by: leases = sorted(leases, key=lambda l: l[sort_by]) return leases + + def _add_lease_date(self, values, lease, key, delta_date, positive_delta): + delta_sec = utils.from_elapsed_time_to_delta( + delta_date, + pos_sign=positive_delta) + date = timeutils.parse_strtime(lease[key], + utils.LEASE_DATE_FORMAT) + values[key] = timeutils.strtime(date + delta_sec, + utils.API_DATE_FORMAT) diff --git a/climateclient/v1/shell_commands/leases.py b/climateclient/v1/shell_commands/leases.py index 2e8ec00..61fc6b5 100644 --- a/climateclient/v1/shell_commands/leases.py +++ b/climateclient/v1/shell_commands/leases.py @@ -250,6 +250,15 @@ class UpdateLease(command.UpdateCommand): help='Time to reduce lease by', default=None ) + + #defer-by and a 'future' advance-by are mutually exclusive + group = parser.add_mutually_exclusive_group() + group.add_argument( + '--defer-by', + help='Time to defer the lease start', + default=None + ) + return parser def args2body(self, parsed_args): @@ -260,6 +269,8 @@ class UpdateLease(command.UpdateCommand): params['prolong_for'] = parsed_args.prolong_for if parsed_args.reduce_by: params['reduce_by'] = parsed_args.reduce_by + if parsed_args.defer_by: + params['defer_by'] = parsed_args.defer_by return params