diff --git a/stacktach/notification.py b/stacktach/notification.py index ac6aad1..c08de04 100644 --- a/stacktach/notification.py +++ b/stacktach/notification.py @@ -95,25 +95,38 @@ class GlanceNotification(Notification): def __init__(self, body, deployment, routing_key, json): super(GlanceNotification, self).__init__(body, deployment, routing_key, json) - self.properties = self.payload.get('properties', {}) - self.image_type = image_type.get_numeric_code(self.payload) - self.status = self.payload.get('status', None) - self.uuid = self.payload.get('id', None) - self.size = self.payload.get('size', None) - created_at = self.payload.get('created_at', None) - self.created_at = created_at and utils.str_time_to_unix(created_at) - audit_period_beginning = self.payload.get( - 'audit_period_beginning', None) - self.audit_period_beginning = audit_period_beginning and\ - utils.str_time_to_unix(audit_period_beginning) - audit_period_ending = self.payload.get( - 'audit_period_ending', None) - self.audit_period_ending = audit_period_ending and \ - utils.str_time_to_unix(audit_period_ending) + if isinstance(self.payload, dict): + self.properties = self.payload.get('properties', {}) + self.image_type = image_type.get_numeric_code(self.payload) + self.status = self.payload.get('status', None) + self.uuid = self.payload.get('id', None) + self.size = self.payload.get('size', None) + created_at = self.payload.get('created_at', None) + self.created_at = created_at and utils.str_time_to_unix(created_at) + audit_period_beginning = self.payload.get( + 'audit_period_beginning', None) + self.audit_period_beginning = audit_period_beginning and\ + utils.str_time_to_unix(audit_period_beginning) + audit_period_ending = self.payload.get( + 'audit_period_ending', None) + self.audit_period_ending = audit_period_ending and \ + utils.str_time_to_unix(audit_period_ending) + else: + self.properties = {} + self.image_type = None + self.status = None + self.uuid = None + self.size = None + self.created_at = None + self.audit_period_beginning = None + self.audit_period_ending = None @property def owner(self): - return self.payload.get('owner', None) + if isinstance(self.payload, dict): + return self.payload.get('owner', None) + else: + return None @property def instance(self): @@ -121,7 +134,10 @@ class GlanceNotification(Notification): @property def deleted_at(self): deleted_at = self.body.get('deleted_at', None) - deleted_at = deleted_at or self.payload.get('deleted_at', None) + + if isinstance(self.payload, dict): + deleted_at = deleted_at or self.payload.get('deleted_at', None) + return deleted_at and utils.str_time_to_unix(deleted_at) def save(self): diff --git a/tests/unit/test_notification.py b/tests/unit/test_notification.py index d86a508..0979d28 100644 --- a/tests/unit/test_notification.py +++ b/tests/unit/test_notification.py @@ -18,6 +18,8 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. +import json + import mox from stacktach import notification @@ -118,13 +120,13 @@ class NovaNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) raw = self.mox.CreateMockAnything() self.mox.StubOutWithMock(db, 'create_nova_rawdata') db.create_nova_rawdata( deployment="1", tenant=TENANT_ID_1, - json=json, + json=json_body, routing_key=routing_key, when=utils.str_time_to_unix(TIMESTAMP_1), publisher="compute.global.preprod-ord.ohthree.com", @@ -144,7 +146,7 @@ class NovaNotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() - notification = NovaNotification(body, deployment, routing_key, json) + notification = NovaNotification(body, deployment, routing_key, json_body) self.assertEquals(notification.save(), raw) self.mox.VerifyAll() @@ -173,13 +175,13 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) raw = self.mox.CreateMockAnything() self.mox.StubOutWithMock(db, 'create_glance_rawdata') db.create_glance_rawdata( deployment="1", owner=TENANT_ID_1, - json=json, + json=json_body, routing_key=routing_key, when=utils.str_time_to_unix("2013-06-20 17:31:57.939614"), publisher="glance-api01-r2961.global.preprod-ord.ohthree.com", @@ -195,7 +197,42 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() notification = GlanceNotification(body, deployment, routing_key, - json) + json_body) + self.assertEquals(notification.save(), raw) + self.mox.VerifyAll() + + def test_save_should_persist_glance_rawdata_erro_payload_to_database(self): + body = { + "event_type": "image.upload", + "timestamp": "2013-06-20 17:31:57.939614", + "publisher_id": "glance-api01-r2961.global.preprod-ord.ohthree.com", + "payload": "error_message" + } + deployment = "1" + routing_key = "glance_monitor.error" + json_body = json.dumps([routing_key, body]) + raw = self.mox.CreateMockAnything() + self.mox.StubOutWithMock(db, 'create_glance_rawdata') + db.create_glance_rawdata( + deployment="1", + owner=None, + json=json_body, + routing_key=routing_key, + when=utils.str_time_to_unix("2013-06-20 17:31:57.939614"), + publisher="glance-api01-r2961.global.preprod-ord.ohthree.com", + event="image.upload", + service="glance-api01-r2961", + host="global.preprod-ord.ohthree.com", + instance=None, + request_id='', + image_type=None, + status=None, + uuid=None).AndReturn(raw) + + self.mox.ReplayAll() + + notification = GlanceNotification(body, deployment, routing_key, + json_body) self.assertEquals(notification.save(), raw) self.mox.VerifyAll() @@ -225,7 +262,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) self.mox.StubOutWithMock(db, 'create_image_exists') self.mox.StubOutWithMock(db, 'get_image_usage') @@ -246,7 +283,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() notification = GlanceNotification(body, deployment, routing_key, - json) + json_body) notification.save_exists(raw) self.mox.VerifyAll() @@ -279,7 +316,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) self.mox.StubOutWithMock(db, 'create_image_exists') self.mox.StubOutWithMock(db, 'get_image_usage') @@ -305,7 +342,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() notification = GlanceNotification(body, deployment, routing_key, - json) + json_body) notification.save_exists(raw) self.mox.VerifyAll() @@ -336,7 +373,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) self.mox.StubOutWithMock(db, 'create_image_exists') self.mox.StubOutWithMock(db, 'get_image_usage') @@ -358,7 +395,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() notification = GlanceNotification(body, deployment, routing_key, - json) + json_body) notification.save_exists(raw) self.mox.VerifyAll() @@ -379,7 +416,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) self.mox.StubOutWithMock(db, 'create_image_usage') db.create_image_usage( @@ -390,7 +427,8 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): uuid=uuid).AndReturn(raw) self.mox.ReplayAll() - notification = GlanceNotification(body, deployment, routing_key, json) + notification = GlanceNotification(body, deployment, routing_key, + json_body) notification.save_usage(raw) self.mox.VerifyAll() @@ -408,7 +446,7 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "glance_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) self.mox.StubOutWithMock(db, 'create_image_delete') db.create_image_delete( @@ -417,7 +455,8 @@ class GlanceNotificationTestCase(StacktachBaseTestCase): deleted_at=utils.str_time_to_unix(deleted_at)).AndReturn(raw) self.mox.ReplayAll() - notification = GlanceNotification(body, deployment, routing_key, json) + notification = GlanceNotification(body, deployment, routing_key, + json_body) notification.save_delete(raw) self.mox.VerifyAll() @@ -446,13 +485,13 @@ class NotificationTestCase(StacktachBaseTestCase): } deployment = "1" routing_key = "generic_monitor.info" - json = '{["routing_key", {%s}]}' % body + json_body = json.dumps([routing_key, body]) raw = self.mox.CreateMockAnything() self.mox.StubOutWithMock(db, 'create_generic_rawdata') db.create_generic_rawdata( deployment="1", tenant=TENANT_ID_1, - json=json, + json=json_body, routing_key=routing_key, when=utils.str_time_to_unix(TIMESTAMP_1), publisher="glance-api01-r2961.global.preprod-ord.ohthree.com", @@ -465,6 +504,6 @@ class NotificationTestCase(StacktachBaseTestCase): self.mox.ReplayAll() - notification = Notification(body, deployment, routing_key, json) + notification = Notification(body, deployment, routing_key, json_body) self.assertEquals(notification.save(), raw) self.mox.VerifyAll()