Fix usage of isotime

The commit I34b12b96de3ea21beaf935ed8a9f6bae2fe0d0bc and
Ib384ae8130dcc6cbd47a837d11ca171ce02ef29e introduced the
deprecated oslo_utils.timeutils.isotime() is deprecated
as of 1.6.

The deprecation message says to use the datetime.datetime.isoformat()
instead, but the format of the string generated by isoformat isn't
the same as the format of the string generated by isotime. The string
is used in tokens and other public APIs and we can't change it
without potentially breaking clients.

So the workaround is to copy the current implementation from
oslo_utils.timeutils.isotime() to ec2api.api.ec2utils.

For more informations:
https://docs.openstack.org/oslo.utils/latest/reference/timeutils.html

Change-Id: Id62fb53264b04a7ea6ae3035a129353c5cfa040a
Closes-Bug: #1461251
This commit is contained in:
chenghuiyu 2017-09-11 10:48:48 +08:00 committed by Andrey Pavlov
parent faa6d7fdc1
commit c65cb663de
7 changed files with 46 additions and 19 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ etc/ec2api/ec2api.conf.sample
.pydevproject
ec2_api.egg-info
.tox
.stestr
*.log
*.egg
*.swp

View File

@ -529,3 +529,32 @@ def block_device_properties_root_device_name(properties):
if bdm['virtual'] == 'root'), None)
else:
return None
_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
def isotime(at=None, subsecond=False):
"""Stringify time in ISO 8601 format."""
# Python provides a similar instance method for datetime.datetime objects
# called isoformat(). The format of the strings generated by isoformat()
# have a couple of problems:
# 1) The strings generated by isotime are used in tokens and other public
# APIs that we can't change without a deprecation period. The strings
# generated by isoformat are not the same format, so we can't just
# change to it.
# 2) The strings generated by isoformat do not include the microseconds if
# the value happens to be 0. This will likely show up as random failures
# as parsers may be written to always expect microseconds, and it will
# parse correctly most of the time.
if not at:
at = timeutils.utcnow()
st = at.strftime(_ISO8601_TIME_FORMAT
if not subsecond
else _ISO8601_TIME_FORMAT_SUBSECOND)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
st += ('Z' if tz == 'UTC' else tz)
return st

View File

@ -33,7 +33,6 @@ from lxml import etree
from oslo_concurrency import processutils
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
import six
from ec2api.api import common
@ -131,7 +130,7 @@ def create_image(context, instance_id, name=None, description=None,
restart_instance = True
# meaningful image name
name_map = dict(instance=instance['os_id'], now=timeutils.isotime())
name_map = dict(instance=instance['os_id'], now=ec2utils.isotime())
name = name or _('image of %(instance)s at %(now)s') % name_map
def delayed_create(context, image, name, os_instance):

View File

@ -19,7 +19,6 @@ import netaddr
from neutronclient.common import exceptions as neutron_exception
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
from ec2api.api import address as address_api
from ec2api.api import common
@ -554,7 +553,7 @@ def _attach_network_interface_item(context, network_interface, instance_id,
device_index, attach_time=None,
delete_on_termination=False):
if not attach_time:
attach_time = timeutils.isotime(None, True)
attach_time = ec2utils.isotime(None, True)
network_interface.update({
'instance_id': instance_id,
'device_index': device_index,

View File

@ -180,7 +180,7 @@ class ApiTestCase(BaseTestCase):
super(ApiTestCase, self).setUp()
self.mock_all_os()
self.db_api = self.mock_db()
self.isotime = self.mock('oslo_utils.timeutils.isotime')
self.isotime = self.mock('ec2api.api.ec2utils.isotime')
def execute(self, action, args):
status_code, response = self._execute(action, args)

View File

@ -20,8 +20,7 @@ import uuid
from lxml import etree
from oslo_utils import timeutils
from ec2api.api import ec2utils
from ec2api.tests.unit import tools
@ -49,7 +48,7 @@ def random_ec2_id(kind):
# common constants
ID_OS_USER = random_os_id()
ID_OS_PROJECT = random_os_id()
TIME_ATTACH_NETWORK_INTERFACE = timeutils.isotime(None, True)
TIME_ATTACH_NETWORK_INTERFACE = ec2utils.isotime(None, True)
MAC_ADDRESS = 'fb:10:2e:b2:ba:b7'
# vpc constants
@ -688,8 +687,8 @@ OS_PORT_2 = {'id': ID_OS_PORT_2,
# instance objects
TIME_CREATE_INSTANCE_1 = timeutils.isotime(None, True)
TIME_CREATE_INSTANCE_2 = timeutils.isotime(None, True)
TIME_CREATE_INSTANCE_1 = ec2utils.isotime(None, True)
TIME_CREATE_INSTANCE_2 = ec2utils.isotime(None, True)
DB_INSTANCE_DEFAULT = {
'id': ID_EC2_INSTANCE_DEFAULT,
@ -1595,7 +1594,7 @@ class OSImage(object):
def __getitem__(self, key):
return self._image_dict.get(key)
TIME_CREATE_IMAGE = timeutils.isotime(None, True)
TIME_CREATE_IMAGE = ec2utils.isotime(None, True)
EC2_IMAGE_1 = {
'imageId': ID_EC2_IMAGE_1,
@ -1765,8 +1764,8 @@ class OSSnapshot(object):
def update(self, *args, **kwargs):
pass
TIME_CREATE_SNAPSHOT_1 = timeutils.isotime(None, True)
TIME_CREATE_SNAPSHOT_2 = timeutils.isotime(None, True)
TIME_CREATE_SNAPSHOT_1 = ec2utils.isotime(None, True)
TIME_CREATE_SNAPSHOT_2 = ec2utils.isotime(None, True)
EC2_SNAPSHOT_1 = {
'description': None,
@ -1844,9 +1843,9 @@ class OSVolume(object):
pass
TIME_CREATE_VOLUME_1 = timeutils.isotime(None, True)
TIME_CREATE_VOLUME_2 = timeutils.isotime(None, True)
TIME_CREATE_VOLUME_3 = timeutils.isotime(None, True)
TIME_CREATE_VOLUME_1 = ec2utils.isotime(None, True)
TIME_CREATE_VOLUME_2 = ec2utils.isotime(None, True)
TIME_CREATE_VOLUME_3 = ec2utils.isotime(None, True)
EC2_VOLUME_1 = {
'volumeId': ID_EC2_VOLUME_1,

View File

@ -234,7 +234,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
self.assertTrue(expired)
def test_validate_ec2_req_timestamp_not_expired(self):
params = {'Timestamp': timeutils.isotime()}
params = {'Timestamp': ec2utils.isotime()}
expired = ec2utils.is_ec2_timestamp_expired(params, expires=15)
self.assertFalse(expired)
@ -246,7 +246,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
@tools.screen_all_logs
def test_validate_ec2_req_expired(self):
params = {'Expires': timeutils.isotime()}
params = {'Expires': ec2utils.isotime()}
expired = ec2utils.is_ec2_timestamp_expired(params)
self.assertTrue(expired)
@ -269,7 +269,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
# EC2 request with both Timestamp and Expires
params = {'Timestamp': '2011-04-22T11:29:49Z',
'Expires': timeutils.isotime()}
'Expires': ec2utils.isotime()}
self.assertRaises(exception.InvalidRequest,
ec2utils.is_ec2_timestamp_expired,
params)