From ba8dc73cd2964fe81553e76df7bdf7d88d9a70ed Mon Sep 17 00:00:00 2001 From: Luis Pigueiras Date: Tue, 3 Oct 2017 15:01:16 +0200 Subject: [PATCH] Generate create_at date in usage in isoformat for backups/snapshots Change the format of the dates when generating notifications for snapshots and backups. This was generating a format problem when inserting events into Elasticsearch via Panko, due to an invalid format (YYYY-MM-DD HH:mm:ssZ to YYYY-MM-DDTHH:mm:ssZ) Change-Id: I651699e42c28c05fc8fd71cfeef54d98839c1a29 --- cinder/tests/unit/api/v2/test_snapshots.py | 6 +++++- cinder/tests/unit/test_volume_utils.py | 5 ++--- cinder/volume/utils.py | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cinder/tests/unit/api/v2/test_snapshots.py b/cinder/tests/unit/api/v2/test_snapshots.py index be35d396481..6e8d777c4d7 100644 --- a/cinder/tests/unit/api/v2/test_snapshots.py +++ b/cinder/tests/unit/api/v2/test_snapshots.py @@ -13,9 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import ddt import mock from oslo_config import cfg +import pytz from six.moves import http_client from six.moves.urllib import parse as urllib import webob @@ -203,6 +205,7 @@ class SnapshotApiTest(test.TestCase): 'id': UUID, 'volume_id': fake.VOLUME_ID, 'status': fields.SnapshotStatus.AVAILABLE, + 'created_at': "2014-01-01 00:00:00", 'volume_size': 100, 'display_name': 'Default name', 'display_description': 'Default description', @@ -226,7 +229,8 @@ class SnapshotApiTest(test.TestCase): 'volume_id': fake.VOLUME_ID, 'status': fields.SnapshotStatus.AVAILABLE, 'size': 100, - 'created_at': None, + 'created_at': datetime.datetime(2014, 1, 1, 0, 0, 0, + tzinfo=pytz.utc), 'updated_at': None, 'name': u'Updated Test Name', 'description': u'Default description', diff --git a/cinder/tests/unit/test_volume_utils.py b/cinder/tests/unit/test_volume_utils.py index 47810508c2c..265655d7c3b 100644 --- a/cinder/tests/unit/test_volume_utils.py +++ b/cinder/tests/unit/test_volume_utils.py @@ -162,7 +162,7 @@ class NotifyUsageTestCase(test.TestCase): 'volume_size': 1, 'snapshot_id': fake.SNAPSHOT_ID, 'display_name': '11', - 'created_at': mock.ANY, + 'created_at': '2014-12-11T10:10:00+00:00', 'status': fields.SnapshotStatus.ERROR, 'deleted': '', 'metadata': six.text_type({'fake_snap_meta_key': @@ -371,8 +371,7 @@ class NotifyUsageTestCase(test.TestCase): expected_backup = raw_backup.copy() expected_backup['tenant_id'] = expected_backup.pop('project_id') expected_backup['backup_id'] = expected_backup.pop('id') - expected_backup['created_at'] = ( - six.text_type(expected_backup['created_at']) + '+00:00') + expected_backup['created_at'] = '2015-01-01T01:01:01+00:00' usage_info = volume_utils._usage_from_backup(backup_obj) self.assertDictEqual(expected_backup, usage_info) diff --git a/cinder/volume/utils.py b/cinder/volume/utils.py index 3f556a5c1b3..b45a9c43a8a 100644 --- a/cinder/volume/utils.py +++ b/cinder/volume/utils.py @@ -109,7 +109,7 @@ def _usage_from_backup(backup, **kw): backup_id=backup.id, host=backup.host, display_name=backup.display_name, - created_at=str(backup.created_at), + created_at=backup.created_at.isoformat(), status=backup.status, volume_id=backup.volume_id, size=backup.size, @@ -171,7 +171,7 @@ def _usage_from_snapshot(snapshot, context, **extra_usage_info): 'volume_size': snapshot.volume_size, 'snapshot_id': snapshot.id, 'display_name': snapshot.display_name, - 'created_at': str(snapshot.created_at), + 'created_at': snapshot.created_at.isoformat(), 'status': snapshot.status, 'deleted': null_safe_str(snapshot.deleted), 'metadata': null_safe_str(snapshot.metadata),