diff --git a/netbox_awx_plugin/synchronization.py b/netbox_awx_plugin/synchronization.py index 7f96e5d87be3c4c7622e1cea7391e217f256c345..16ab5641ac35eb8b76521fa32443febe8023bc27 100644 --- a/netbox_awx_plugin/synchronization.py +++ b/netbox_awx_plugin/synchronization.py @@ -96,7 +96,10 @@ def sync_host(inventory, sender, instance): else: logger.error(f"Invalid data for host_serializer: {host_serializer.errors}") - current_groups = host["summary_fields"]["groups"]["results"] + if len(host["summary_fields"]["groups"]["results"]) > host["summary_fields"]["groups"]["count"]: + current_groups = inventory.get_host_groups(host['id']) + else: + current_groups = host["summary_fields"]["groups"]["results"] if hasattr(instance, 'site') and instance.site: sync_host_group_association(inventory, host, Site, instance.site, current_groups) diff --git a/netbox_awx_plugin/tests/test_synchronization.py b/netbox_awx_plugin/tests/test_synchronization.py index a2f4f828b8400cdd56e4009e36ee1031b2a92b39..7e7a0df422748c2bb97f08653363f912264ab312 100644 --- a/netbox_awx_plugin/tests/test_synchronization.py +++ b/netbox_awx_plugin/tests/test_synchronization.py @@ -84,7 +84,7 @@ class SynchronizationTestCase(TestCase): { 'id': 1, 'name': 'test-device.example.com', - 'summary_fields': {'groups': {'results': []}} + 'summary_fields': {'groups': {'count': 0, 'results': []}} } # Second call returns a mock host ] # Simulate that the group exists @@ -105,7 +105,7 @@ class SynchronizationTestCase(TestCase): mock_get_host.return_value = { 'id': 1, 'name': 'test-device.example.com', - 'summary_fields': {'groups': {'results': []}} + 'summary_fields': {'groups': {'count': 0, 'results': []}} } # Simulate that the group exists mock_get_group.return_value = {'id': 2, 'name': 'site_test_site'} @@ -162,7 +162,7 @@ class SynchronizationTestCase(TestCase): mock_get_host.return_value = { 'id': 1, 'name': 'test-device.example.com', - 'summary_fields': {'groups': {'results': []}} + 'summary_fields': {'groups': {'count': 0, 'results': []}} } sync_host(self.awx_inventory, Device, self.device) # Check that the host is associated with the tag group @@ -223,7 +223,7 @@ class SynchronizationTestCase(TestCase): { 'id': 1, 'name': 'test-vm.example.com', - 'summary_fields': {'groups': {'results': []}} + 'summary_fields': {'groups': {'count': 0, 'results': []}} } # Second call returns a mock host ] # Simulate that the group exists for the VM role @@ -248,7 +248,7 @@ class SynchronizationTestCase(TestCase): mock_get_host.return_value = { 'id': 1, 'name': self.device.name, - 'summary_fields': {'groups': {'results': []}} + 'summary_fields': {'groups': {'count': 0, 'results': []}} } # Mock get_group to prevent network calls mock_get_group.return_value = {'id': 2, 'name': 'site_test_site'}