Skip to content

Commit

Permalink
Fix json encode error during dc host update
Browse files Browse the repository at this point in the history
`_previous_state` includes non serializable fields, like date/datetime
Reduce data being send only to hostname.
Add also test checking if data is serializable.
  • Loading branch information
xliiv committed Sep 22, 2016
1 parent 012e373 commit cb45720
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ralph/data_center/models/physical.py
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/ralph/data_center/publishers.py
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions 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
Expand Down Expand Up @@ -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,
)
3 changes: 3 additions & 0 deletions src/ralph/virtual/models.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb45720

Please sign in to comment.