fleet/server/fleet/hostresponse.go
Lucas Manuel Rodriguez 1603ee0ea8
/api/_version_/fleet/hosts to return bad request instead of server error when passing invalid mdm_enrollment_status (#11242)
#10880

I was not able to reproduce other 500s in `/api/_version_/fleet/hosts`
other than the one fixed in the PR.

- [X] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- ~[ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)~
- ~[ ] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
  - ~For Orbit and Fleet Desktop changes:~
- ~[ ] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.~
- ~[ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
2023-04-19 18:43:15 -03:00

46 lines
1.5 KiB
Go

package fleet
import (
"context"
"time"
)
// HostResponse is the response struct that contains the full host information
// along with the host online status and the "display text" to be used when
// rendering in the UI.
type HostResponse struct {
*Host
Status HostStatus `json:"status" csv:"status"`
DisplayText string `json:"display_text" csv:"display_text"`
DisplayName string `json:"display_name" csv:"display_name"`
Labels []Label `json:"labels,omitempty" csv:"-"`
Geolocation *GeoLocation `json:"geolocation,omitempty" csv:"-"`
CSVDeviceMapping string `json:"-" db:"-" csv:"device_mapping"`
}
// HostResponseForHost returns a HostResponse from Host with Geolocation.
func HostResponseForHost(ctx context.Context, svc Service, host *Host) *HostResponse {
hr := HostResponseForHostCheap(host)
hr.Geolocation = svc.LookupGeoIP(ctx, host.PublicIP)
return hr
}
// HostResponseForHostCheap returns a new HostResponse from a Host without computing Geolocation.
func HostResponseForHostCheap(host *Host) *HostResponse {
return &HostResponse{
Host: host,
Status: host.Status(time.Now()),
DisplayText: host.Hostname,
DisplayName: host.DisplayName(),
}
}
// HostResponsesForHostsCheap returns a HostResponses from Hosts without computing Geolocation.
func HostResponsesForHostsCheap(hosts []Host) []HostResponse {
hrs := make([]HostResponse, len(hosts))
for i, h := range hosts {
h := h
hrs[i] = *HostResponseForHostCheap(&h)
}
return hrs
}