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
This commit is contained in:
Cristian A Sanchez 2014-04-29 16:38:45 -03:00
parent c4ba4f0489
commit 464dc67b0b
3 changed files with 61 additions and 19 deletions

View File

@ -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"
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)

View File

@ -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)

View File

@ -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