From 1d2b71857bf57c5a4b9999321104b9f94e57d894 Mon Sep 17 00:00:00 2001 From: RachelElysia <71795832+RachelElysia@users.noreply.github.com> Date: Tue, 17 Jun 2025 09:20:09 -0400 Subject: [PATCH] Host API: Return empty array instead of 404 for software filter not found (#30045) --- changes/29823-26258-empty-host | 1 + server/service/hosts.go | 4 ++-- server/service/integration_core_test.go | 14 +++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 changes/29823-26258-empty-host diff --git a/changes/29823-26258-empty-host b/changes/29823-26258-empty-host new file mode 100644 index 0000000000..d723f299d5 --- /dev/null +++ b/changes/29823-26258-empty-host @@ -0,0 +1 @@ +- Fixed host API to returns empty array (instead of 404) if software title or version is not found on hosts on that team consistent with other host filters diff --git a/server/service/hosts.go b/server/service/hosts.go index a66fb78d81..f1a47f53a3 100644 --- a/server/service/hosts.go +++ b/server/service/hosts.go @@ -102,7 +102,7 @@ func listHostsEndpoint(ctx context.Context, request interface{}, svc fleet.Servi id = req.Opts.SoftwareIDFilter } software, err = svc.SoftwareByID(ctx, *id, req.Opts.TeamFilter, false) - if err != nil { + if err != nil && !fleet.IsNotFound(err) { // ignore not found, just return nil for the software in that case return listHostsResponse{Err: err}, nil } } @@ -112,7 +112,7 @@ func listHostsEndpoint(ctx context.Context, request interface{}, svc fleet.Servi var err error softwareTitle, err = svc.SoftwareTitleByID(ctx, *req.Opts.SoftwareTitleIDFilter, req.Opts.TeamFilter) - if err != nil { + if err != nil && !fleet.IsNotFound(err) { // ignore not found, just return nil for the software title in that case return listHostsResponse{Err: err}, nil } } diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index 9184d6809b..b42aa2ba8b 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -1788,11 +1788,19 @@ func (s *integrationTestSuite) TestListHosts() { // Filter by inexistent software. resp = listHostsResponse{} - s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusNotFound, &resp, "software_id", fmt.Sprint(9999)) + s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusOK, &resp, "software_id", fmt.Sprint(9999)) + require.Len(t, resp.Hosts, 0) + assert.Nil(t, resp.Software) + resp = listHostsResponse{} - s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusNotFound, &resp, "software_version_id", fmt.Sprint(9999)) + s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusOK, &resp, "software_version_id", fmt.Sprint(9999)) + require.Len(t, resp.Hosts, 0) + assert.Nil(t, resp.Software) + resp = listHostsResponse{} - s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusNotFound, &resp, "software_title_id", fmt.Sprint(9999)) + s.DoJSON("GET", "/api/latest/fleet/hosts", nil, http.StatusOK, &resp, "software_title_id", fmt.Sprint(9999)) + require.Len(t, resp.Hosts, 0) + assert.Nil(t, resp.SoftwareTitle) // Filter by non-existent team. resp = listHostsResponse{}