diff --git a/stacktach/notification.py b/stacktach/notification.py index abcce00..652f458 100644 --- a/stacktach/notification.py +++ b/stacktach/notification.py @@ -237,6 +237,7 @@ class NovaNotification(Notification): self.payload.get('new_instance_type_id', None) self.launched_at = self.payload.get('launched_at', None) self.deleted_at = self.payload.get('deleted_at', None) + self.terminated_at = self.payload.get('terminated_at', None) self.audit_period_beginning = self.payload.get( 'audit_period_beginning', None) self.audit_period_ending = self.payload.get( diff --git a/stacktach/views.py b/stacktach/views.py index f92688d..c496f5b 100644 --- a/stacktach/views.py +++ b/stacktach/views.py @@ -242,7 +242,11 @@ def _process_usage_for_updates(raw, notification): def _process_delete(raw, notification): if notification.launched_at and notification.launched_at != '': instance_id = notification.instance - deleted_at = utils.str_time_to_unix(notification.deleted_at) + deleted_at = None + if notification.deleted_at: + deleted_at = utils.str_time_to_unix(notification.deleted_at) + elif notification.terminated_at: + deleted_at = utils.str_time_to_unix(notification.terminated_at) launched_at = utils.str_time_to_unix(notification.launched_at) values = { 'instance': instance_id, diff --git a/tests/unit/test_stacktach.py b/tests/unit/test_stacktach.py index 41382c6..a2de719 100644 --- a/tests/unit/test_stacktach.py +++ b/tests/unit/test_stacktach.py @@ -665,12 +665,74 @@ class StacktachUsageParsingTestCase(StacktachBaseTestCase): def test_process_delete(self): delete_time = datetime.datetime.utcnow() + terminated_time = delete_time-datetime.timedelta(seconds=1) launch_time = delete_time-datetime.timedelta(days=1) launch_decimal = utils.decimal_utc(launch_time) delete_decimal = utils.decimal_utc(delete_time) notification = self.mox.CreateMockAnything() notification.instance = INSTANCE_ID_1 notification.deleted_at = str(delete_time) + notification.terminated_at = str(terminated_time) + notification.launched_at = str(launch_time) + + raw = self.mox.CreateMockAnything() + delete = self.mox.CreateMockAnything() + delete.instance = INSTANCE_ID_1 + delete.launched_at = launch_decimal + delete.deleted_at = delete_decimal + views.STACKDB.get_or_create_instance_delete( + instance=INSTANCE_ID_1, deleted_at=delete_decimal, + launched_at=launch_decimal)\ + .AndReturn((delete, True)) + views.STACKDB.save(delete) + self.mox.ReplayAll() + + views._process_delete(raw, notification) + + self.assertEqual(delete.instance, INSTANCE_ID_1) + self.assertEqual(delete.launched_at, launch_decimal) + self.assertEqual(delete.deleted_at, delete_decimal) + self.mox.VerifyAll() + + def test_process_delete_with_only_terminated_at(self): + delete_time = datetime.datetime.utcnow() + launch_time = delete_time-datetime.timedelta(days=1) + launch_decimal = utils.decimal_utc(launch_time) + delete_decimal = utils.decimal_utc(delete_time) + notification = self.mox.CreateMockAnything() + notification.instance = INSTANCE_ID_1 + notification.deleted_at = '' + notification.terminated_at = str(delete_time) + notification.launched_at = str(launch_time) + + raw = self.mox.CreateMockAnything() + delete = self.mox.CreateMockAnything() + delete.instance = INSTANCE_ID_1 + delete.launched_at = launch_decimal + delete.deleted_at = delete_decimal + views.STACKDB.get_or_create_instance_delete( + instance=INSTANCE_ID_1, deleted_at=delete_decimal, + launched_at=launch_decimal)\ + .AndReturn((delete, True)) + views.STACKDB.save(delete) + self.mox.ReplayAll() + + views._process_delete(raw, notification) + + self.assertEqual(delete.instance, INSTANCE_ID_1) + self.assertEqual(delete.launched_at, launch_decimal) + self.assertEqual(delete.deleted_at, delete_decimal) + self.mox.VerifyAll() + + def test_process_delete_with_neither(self): + delete_time = datetime.datetime.utcnow() + launch_time = delete_time-datetime.timedelta(days=1) + launch_decimal = utils.decimal_utc(launch_time) + delete_decimal = utils.decimal_utc(delete_time) + notification = self.mox.CreateMockAnything() + notification.instance = INSTANCE_ID_1 + notification.deleted_at = '' + notification.terminated_at = str(delete_time) notification.launched_at = str(launch_time) raw = self.mox.CreateMockAnything()