summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenghuiyu <yuchenghui@unionpay.com>2017-09-11 10:48:48 +0800
committerAndrey Pavlov <andrey.mp@gmail.com>2018-06-29 16:00:40 +0300
commitc65cb663de5d1cfb239bbf32610f0f7a71c7e170 (patch)
tree997538bbff30f159d1c11b3e46a7784c385560ea
parentfaa6d7fdc1023a2a623bbfbec1e1de2e0652b074 (diff)
Fix usage of isotimeHEADmaster
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
Notes
Notes (review): Code-Review+2: Andrey Pavlov <andrey.mp@gmail.com> Workflow+1: Andrey Pavlov <andrey.mp@gmail.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Sat, 30 Jun 2018 23:27:53 +0000 Reviewed-on: https://review.openstack.org/579145 Project: openstack/ec2-api Branch: refs/heads/master
-rw-r--r--.gitignore1
-rw-r--r--ec2api/api/ec2utils.py29
-rw-r--r--ec2api/api/image.py3
-rw-r--r--ec2api/api/network_interface.py3
-rw-r--r--ec2api/tests/unit/base.py2
-rw-r--r--ec2api/tests/unit/fakes.py21
-rw-r--r--ec2api/tests/unit/test_ec2_validate.py6
7 files changed, 46 insertions, 19 deletions
diff --git a/.gitignore b/.gitignore
index bc85a77..96bc43e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@ etc/ec2api/ec2api.conf.sample
5.pydevproject 5.pydevproject
6ec2_api.egg-info 6ec2_api.egg-info
7.tox 7.tox
8.stestr
8*.log 9*.log
9*.egg 10*.egg
10*.swp 11*.swp
diff --git a/ec2api/api/ec2utils.py b/ec2api/api/ec2utils.py
index 1f0abb6..467889e 100644
--- a/ec2api/api/ec2utils.py
+++ b/ec2api/api/ec2utils.py
@@ -529,3 +529,32 @@ def block_device_properties_root_device_name(properties):
529 if bdm['virtual'] == 'root'), None) 529 if bdm['virtual'] == 'root'), None)
530 else: 530 else:
531 return None 531 return None
532
533
534_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
535_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
536
537
538def isotime(at=None, subsecond=False):
539 """Stringify time in ISO 8601 format."""
540
541 # Python provides a similar instance method for datetime.datetime objects
542 # called isoformat(). The format of the strings generated by isoformat()
543 # have a couple of problems:
544 # 1) The strings generated by isotime are used in tokens and other public
545 # APIs that we can't change without a deprecation period. The strings
546 # generated by isoformat are not the same format, so we can't just
547 # change to it.
548 # 2) The strings generated by isoformat do not include the microseconds if
549 # the value happens to be 0. This will likely show up as random failures
550 # as parsers may be written to always expect microseconds, and it will
551 # parse correctly most of the time.
552
553 if not at:
554 at = timeutils.utcnow()
555 st = at.strftime(_ISO8601_TIME_FORMAT
556 if not subsecond
557 else _ISO8601_TIME_FORMAT_SUBSECOND)
558 tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
559 st += ('Z' if tz == 'UTC' else tz)
560 return st
diff --git a/ec2api/api/image.py b/ec2api/api/image.py
index 61d2c52..efc77f2 100644
--- a/ec2api/api/image.py
+++ b/ec2api/api/image.py
@@ -33,7 +33,6 @@ from lxml import etree
33from oslo_concurrency import processutils 33from oslo_concurrency import processutils
34from oslo_config import cfg 34from oslo_config import cfg
35from oslo_log import log as logging 35from oslo_log import log as logging
36from oslo_utils import timeutils
37import six 36import six
38 37
39from ec2api.api import common 38from ec2api.api import common
@@ -131,7 +130,7 @@ def create_image(context, instance_id, name=None, description=None,
131 restart_instance = True 130 restart_instance = True
132 131
133 # meaningful image name 132 # meaningful image name
134 name_map = dict(instance=instance['os_id'], now=timeutils.isotime()) 133 name_map = dict(instance=instance['os_id'], now=ec2utils.isotime())
135 name = name or _('image of %(instance)s at %(now)s') % name_map 134 name = name or _('image of %(instance)s at %(now)s') % name_map
136 135
137 def delayed_create(context, image, name, os_instance): 136 def delayed_create(context, image, name, os_instance):
diff --git a/ec2api/api/network_interface.py b/ec2api/api/network_interface.py
index 08595f7..1b03a09 100644
--- a/ec2api/api/network_interface.py
+++ b/ec2api/api/network_interface.py
@@ -19,7 +19,6 @@ import netaddr
19from neutronclient.common import exceptions as neutron_exception 19from neutronclient.common import exceptions as neutron_exception
20from oslo_config import cfg 20from oslo_config import cfg
21from oslo_log import log as logging 21from oslo_log import log as logging
22from oslo_utils import timeutils
23 22
24from ec2api.api import address as address_api 23from ec2api.api import address as address_api
25from ec2api.api import common 24from ec2api.api import common
@@ -554,7 +553,7 @@ def _attach_network_interface_item(context, network_interface, instance_id,
554 device_index, attach_time=None, 553 device_index, attach_time=None,
555 delete_on_termination=False): 554 delete_on_termination=False):
556 if not attach_time: 555 if not attach_time:
557 attach_time = timeutils.isotime(None, True) 556 attach_time = ec2utils.isotime(None, True)
558 network_interface.update({ 557 network_interface.update({
559 'instance_id': instance_id, 558 'instance_id': instance_id,
560 'device_index': device_index, 559 'device_index': device_index,
diff --git a/ec2api/tests/unit/base.py b/ec2api/tests/unit/base.py
index f593581..e841ba1 100644
--- a/ec2api/tests/unit/base.py
+++ b/ec2api/tests/unit/base.py
@@ -180,7 +180,7 @@ class ApiTestCase(BaseTestCase):
180 super(ApiTestCase, self).setUp() 180 super(ApiTestCase, self).setUp()
181 self.mock_all_os() 181 self.mock_all_os()
182 self.db_api = self.mock_db() 182 self.db_api = self.mock_db()
183 self.isotime = self.mock('oslo_utils.timeutils.isotime') 183 self.isotime = self.mock('ec2api.api.ec2utils.isotime')
184 184
185 def execute(self, action, args): 185 def execute(self, action, args):
186 status_code, response = self._execute(action, args) 186 status_code, response = self._execute(action, args)
diff --git a/ec2api/tests/unit/fakes.py b/ec2api/tests/unit/fakes.py
index 8dd2731..3202aff 100644
--- a/ec2api/tests/unit/fakes.py
+++ b/ec2api/tests/unit/fakes.py
@@ -20,8 +20,7 @@ import uuid
20 20
21from lxml import etree 21from lxml import etree
22 22
23from oslo_utils import timeutils 23from ec2api.api import ec2utils
24
25from ec2api.tests.unit import tools 24from ec2api.tests.unit import tools
26 25
27 26
@@ -49,7 +48,7 @@ def random_ec2_id(kind):
49# common constants 48# common constants
50ID_OS_USER = random_os_id() 49ID_OS_USER = random_os_id()
51ID_OS_PROJECT = random_os_id() 50ID_OS_PROJECT = random_os_id()
52TIME_ATTACH_NETWORK_INTERFACE = timeutils.isotime(None, True) 51TIME_ATTACH_NETWORK_INTERFACE = ec2utils.isotime(None, True)
53MAC_ADDRESS = 'fb:10:2e:b2:ba:b7' 52MAC_ADDRESS = 'fb:10:2e:b2:ba:b7'
54 53
55# vpc constants 54# vpc constants
@@ -688,8 +687,8 @@ OS_PORT_2 = {'id': ID_OS_PORT_2,
688 687
689 688
690# instance objects 689# instance objects
691TIME_CREATE_INSTANCE_1 = timeutils.isotime(None, True) 690TIME_CREATE_INSTANCE_1 = ec2utils.isotime(None, True)
692TIME_CREATE_INSTANCE_2 = timeutils.isotime(None, True) 691TIME_CREATE_INSTANCE_2 = ec2utils.isotime(None, True)
693 692
694DB_INSTANCE_DEFAULT = { 693DB_INSTANCE_DEFAULT = {
695 'id': ID_EC2_INSTANCE_DEFAULT, 694 'id': ID_EC2_INSTANCE_DEFAULT,
@@ -1595,7 +1594,7 @@ class OSImage(object):
1595 def __getitem__(self, key): 1594 def __getitem__(self, key):
1596 return self._image_dict.get(key) 1595 return self._image_dict.get(key)
1597 1596
1598TIME_CREATE_IMAGE = timeutils.isotime(None, True) 1597TIME_CREATE_IMAGE = ec2utils.isotime(None, True)
1599 1598
1600EC2_IMAGE_1 = { 1599EC2_IMAGE_1 = {
1601 'imageId': ID_EC2_IMAGE_1, 1600 'imageId': ID_EC2_IMAGE_1,
@@ -1765,8 +1764,8 @@ class OSSnapshot(object):
1765 def update(self, *args, **kwargs): 1764 def update(self, *args, **kwargs):
1766 pass 1765 pass
1767 1766
1768TIME_CREATE_SNAPSHOT_1 = timeutils.isotime(None, True) 1767TIME_CREATE_SNAPSHOT_1 = ec2utils.isotime(None, True)
1769TIME_CREATE_SNAPSHOT_2 = timeutils.isotime(None, True) 1768TIME_CREATE_SNAPSHOT_2 = ec2utils.isotime(None, True)
1770 1769
1771EC2_SNAPSHOT_1 = { 1770EC2_SNAPSHOT_1 = {
1772 'description': None, 1771 'description': None,
@@ -1844,9 +1843,9 @@ class OSVolume(object):
1844 pass 1843 pass
1845 1844
1846 1845
1847TIME_CREATE_VOLUME_1 = timeutils.isotime(None, True) 1846TIME_CREATE_VOLUME_1 = ec2utils.isotime(None, True)
1848TIME_CREATE_VOLUME_2 = timeutils.isotime(None, True) 1847TIME_CREATE_VOLUME_2 = ec2utils.isotime(None, True)
1849TIME_CREATE_VOLUME_3 = timeutils.isotime(None, True) 1848TIME_CREATE_VOLUME_3 = ec2utils.isotime(None, True)
1850 1849
1851EC2_VOLUME_1 = { 1850EC2_VOLUME_1 = {
1852 'volumeId': ID_EC2_VOLUME_1, 1851 'volumeId': ID_EC2_VOLUME_1,
diff --git a/ec2api/tests/unit/test_ec2_validate.py b/ec2api/tests/unit/test_ec2_validate.py
index 0a25951..a550b18 100644
--- a/ec2api/tests/unit/test_ec2_validate.py
+++ b/ec2api/tests/unit/test_ec2_validate.py
@@ -234,7 +234,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
234 self.assertTrue(expired) 234 self.assertTrue(expired)
235 235
236 def test_validate_ec2_req_timestamp_not_expired(self): 236 def test_validate_ec2_req_timestamp_not_expired(self):
237 params = {'Timestamp': timeutils.isotime()} 237 params = {'Timestamp': ec2utils.isotime()}
238 expired = ec2utils.is_ec2_timestamp_expired(params, expires=15) 238 expired = ec2utils.is_ec2_timestamp_expired(params, expires=15)
239 self.assertFalse(expired) 239 self.assertFalse(expired)
240 240
@@ -246,7 +246,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
246 246
247 @tools.screen_all_logs 247 @tools.screen_all_logs
248 def test_validate_ec2_req_expired(self): 248 def test_validate_ec2_req_expired(self):
249 params = {'Expires': timeutils.isotime()} 249 params = {'Expires': ec2utils.isotime()}
250 expired = ec2utils.is_ec2_timestamp_expired(params) 250 expired = ec2utils.is_ec2_timestamp_expired(params)
251 self.assertTrue(expired) 251 self.assertTrue(expired)
252 252
@@ -269,7 +269,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
269 269
270 # EC2 request with both Timestamp and Expires 270 # EC2 request with both Timestamp and Expires
271 params = {'Timestamp': '2011-04-22T11:29:49Z', 271 params = {'Timestamp': '2011-04-22T11:29:49Z',
272 'Expires': timeutils.isotime()} 272 'Expires': ec2utils.isotime()}
273 self.assertRaises(exception.InvalidRequest, 273 self.assertRaises(exception.InvalidRequest,
274 ec2utils.is_ec2_timestamp_expired, 274 ec2utils.is_ec2_timestamp_expired,
275 params) 275 params)