Scaffold a new Fleet API endpoint with request/response structs, endpoint function, service method, datastore interface, handler registration, and test stubs.
Create a new API endpoint for: $ARGUMENTS
/api/_version_/fleet/resource)StartingAtVersion("2022-04") for new endpoints)Read server/service/vulnerabilities.go for the canonical request/response/endpoint pattern:
Err error field and Error() method(ctx, request, svc) signatureRead server/service/handler.go to find where to register the new endpoint.
type myResourceRequest struct {
ID uint `url:"id"`
Name string `json:"name"`
}
type myResourceResponse struct {
Resource *fleet.Resource `json:"resource,omitempty"`
Err error `json:"error,omitempty"`
}
func (r myResourceResponse) Error() error { return r.Err }
func myResourceEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (fleet.Errorer, error) {
req := request.(*myResourceRequest)
result, err := svc.MyResource(ctx, req.ID)
if err != nil {
return myResourceResponse{Err: err}, nil
}
return myResourceResponse{Resource: result}, nil
}
In server/fleet/service.go, add the method to the Service interface.
In the appropriate server/service/*.go file:
svc.authz.Authorize(ctx, &fleet.Entity{}, fleet.ActionRead)ctxerr.WrapIn server/fleet/datastore.go, add the method to the Datastore interface.
ue.StartingAtVersion("2022-04").GET("/api/_version_/fleet/resource", myResourceEndpoint, myResourceRequest{})
server/service/*_test.gogo build ./... to check compilationgo test ./server/service/ to check mocks are satisfied