fleet/server/activity/internal/service/handler.go
Victor Lyuboslavsky 7deade8057
Activity bounded context: /api/latest/fleet/activities (2 of 2) (#38478)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #37806 

Removed `ds.ListActivities` from the legacy datastore and updated
code/tests to use the new activity bounded context instead.

The changes to `cron.go` and most changes to `mysql/activities_test.go`
will eventually be migrated to the activity bounded context. The current
changes are an intermediate step.

The issues tracked by https://github.com/fleetdm/fleet/issues/38234 will
be addressed in additional/parallel PRs shortly.

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Done in the previous PR

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Migrated activity retrieval from direct datastore calls to a
service-based architecture for improved maintainability and consistency.
* Enhanced system context handling for background automation tasks to
ensure proper authorization during scheduled operations.
* Streamlined activity recording for automated processes with dedicated
system identity tracking.

* **Tests**
* Updated test infrastructure with new helpers for activity service
integration across test suites.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Ian Littman <iansltx@gmail.com>
2026-01-23 07:42:09 -06:00

69 lines
2.2 KiB
Go

package service
import (
"context"
"github.com/fleetdm/fleet/v4/server/activity/api"
api_http "github.com/fleetdm/fleet/v4/server/activity/api/http"
eu "github.com/fleetdm/fleet/v4/server/platform/endpointer"
platform_http "github.com/fleetdm/fleet/v4/server/platform/http"
"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
)
// GetRoutes returns a function that registers activity routes on the router.
func GetRoutes(svc api.Service, authMiddleware endpoint.Middleware) eu.HandlerRoutesFunc {
return func(r *mux.Router, opts []kithttp.ServerOption) {
attachFleetAPIRoutes(r, svc, authMiddleware, opts)
}
}
func attachFleetAPIRoutes(r *mux.Router, svc api.Service, authMiddleware endpoint.Middleware, opts []kithttp.ServerOption) {
// User-authenticated endpoints
ue := newUserAuthenticatedEndpointer(svc, authMiddleware, opts, r, apiVersions()...)
ue.GET("/api/_version_/fleet/activities", listActivitiesEndpoint, api_http.ListActivitiesRequest{})
}
func apiVersions() []string {
return []string{"v1", "latest"}
}
// listActivitiesEndpoint handles GET /api/_version_/fleet/activities
func listActivitiesEndpoint(ctx context.Context, request any, svc api.Service) platform_http.Errorer {
req := request.(*api_http.ListActivitiesRequest)
opt := req.ListOptions // Access the embedded api.ListOptions
fillListOptions(&opt)
activities, meta, err := svc.ListActivities(ctx, opt)
if err != nil {
return api_http.ListActivitiesResponse{Err: err}
}
return api_http.ListActivitiesResponse{
Meta: meta,
Activities: activities,
}
}
// fillListOptions sets default values for list options.
// Note: IncludeMetadata is set internally by the service layer.
func fillListOptions(opt *api.ListOptions) {
// Default ordering by created_at descending (newest first) if not specified
if opt.OrderKey == "" {
opt.OrderKey = "created_at"
opt.OrderDirection = api.OrderDescending
}
// Default PerPage based on whether pagination was requested
if opt.PerPage == 0 {
if opt.Page == 0 {
// No pagination requested - return all results (legacy behavior)
opt.PerPage = unlimitedPerPage
} else {
// Page specified without per_page - use sensible default
opt.PerPage = defaultPerPage
}
}
}