diff --git a/awx/api/views.py b/awx/api/views.py index 1bf21696ef..4531cbe5dc 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1959,6 +1959,13 @@ class InventoryHostsList(SubListCreateAttachDetachAPIView): parent_key = 'inventory' capabilities_prefetch = ['inventory.admin'] + def get_queryset(self): + inventory = self.get_parent_object() + if inventory.kind == 'smart': + filter_qs = SmartFilter.query_from_string(inventory.host_filter) + return filter_qs.distinct() + return super(InventoryHostsList, self).get_queryset() + class HostGroupsList(ControlledByScmMixin, SubListCreateAttachDetachAPIView): ''' the list of groups a host is directly a member of ''' diff --git a/awx/main/tests/unit/api/test_views.py b/awx/main/tests/unit/api/test_views.py index 8a0a22aa48..1d82e4a4ea 100644 --- a/awx/main/tests/unit/api/test_views.py +++ b/awx/main/tests/unit/api/test_views.py @@ -9,6 +9,7 @@ from awx.api.views import ( JobTemplateLabelList, JobTemplateSurveySpec, InventoryInventorySourcesUpdate, + InventoryHostsList, HostInsights, ) @@ -204,3 +205,15 @@ class TestHostInsights(): assert resp.data['error'] == 'The Insights Credential for "inventory_name_here" was not found.' assert resp.status_code == 404 + + +class TestInventoryHostsList(object): + + def test_host_list_smart_inventory(self, mocker): + Inventory = namedtuple('Inventory', ['kind', 'host_filter']) + obj = Inventory(kind='smart', host_filter='localhost') + with mock.patch.object(InventoryHostsList, 'get_parent_object', return_value=obj): + with mock.patch('awx.api.views.SmartFilter.query_from_string') as mock_query: + view = InventoryHostsList() + view.get_queryset() + mock_query.assert_called_once_with('localhost')