Host API: Return empty array instead of 404 for software filter not found (#30045)

This commit is contained in:
RachelElysia
2025-06-17 09:20:09 -04:00
committed by GitHub
parent cef08202b8
commit 1d2b71857b
3 changed files with 14 additions and 5 deletions
+1
View File
@@ -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
+2 -2
View File
@@ -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
}
}
+11 -3
View File
@@ -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{}