From cb4572044a58f50448de28514802ae4a7c827eef Mon Sep 17 00:00:00 2001 From: xliiv Date: Thu, 22 Sep 2016 12:03:05 +0200 Subject: [PATCH] Fix json encode error during dc host update `_previous_state` includes non serializable fields, like date/datetime Reduce data being send only to hostname. Add also test checking if data is serializable. --- src/ralph/data_center/models/physical.py | 1 + src/ralph/data_center/publishers.py | 5 ++++- src/ralph/data_center/tests/test_signals.py | 13 +++++++++++++ src/ralph/virtual/models.py | 3 +++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ralph/data_center/models/physical.py b/src/ralph/data_center/models/physical.py index b73d85f711..67ccb1375b 100644 --- a/src/ralph/data_center/models/physical.py +++ b/src/ralph/data_center/models/physical.py @@ -356,6 +356,7 @@ class DataCenterAsset( Asset ): _allow_in_dashboard = True + previous_dc_host_update_fields = ['hostname'] rack = models.ForeignKey(Rack, null=True, blank=True) status = TransitionField( diff --git a/src/ralph/data_center/publishers.py b/src/ralph/data_center/publishers.py index c3feeec7b3..de9a1ed557 100644 --- a/src/ralph/data_center/publishers.py +++ b/src/ralph/data_center/publishers.py @@ -10,7 +10,10 @@ def _get_host_data(instance): serializer = DCHostSerializer(instance=instance) if hasattr(serializer.instance, '_previous_state'): data = deepcopy(serializer.data) - data['_previous_state'] = serializer.instance._previous_state + data['_previous_state'] = { + k: v for k, v in serializer.instance._previous_state.items() + if k in serializer.instance.previous_dc_host_update_fields + } else: data = serializer.data return data diff --git a/src/ralph/data_center/tests/test_signals.py b/src/ralph/data_center/tests/test_signals.py index 3638dc418e..abba2d45ec 100644 --- a/src/ralph/data_center/tests/test_signals.py +++ b/src/ralph/data_center/tests/test_signals.py @@ -1,3 +1,5 @@ +import json + from django.test import TestCase from ralph.data_center.publishers import _get_host_data @@ -33,3 +35,14 @@ def test_sending_data_includes_previous_data(self): data = _get_host_data(getattr(self, obj_name)) results.append('_previous_state' in data) self.assertEqual(results, [True] * 3) + + def test_sending_data_doesnt_raise_json_error(self): + for obj_name in ['cloud_host', 'dc_asset', 'virtual_server']: + json.dumps(_get_host_data(self.dc_asset)) + + def test_sending_data_includes_only_selected_fields(self): + data = _get_host_data(self.dc_asset) + self.assertEqual( + list(data['_previous_state'].keys()), + self.dc_asset.previous_dc_host_update_fields, + ) diff --git a/src/ralph/virtual/models.py b/src/ralph/virtual/models.py index 0255b34697..639578801d 100644 --- a/src/ralph/virtual/models.py +++ b/src/ralph/virtual/models.py @@ -153,6 +153,7 @@ def update_service_env_on_cloudproject_save(sender, instance, **kwargs): class CloudHost(PreviousStateMixin, AdminAbsoluteUrlMixin, BaseObject): + previous_dc_host_update_fields = ['hostname'] def save(self, *args, **kwargs): try: @@ -289,6 +290,8 @@ class VirtualServer( # TODO: remove this field cluster = models.ForeignKey(Cluster, blank=True, null=True) + previous_dc_host_update_fields = ['hostname'] + @cached_property def polymorphic_parent(self): return self.parent.last_descendant if self.parent_id else None