Files
emissary/internal/server/api/query_agent_specs.go
William Petit 85ccf2e1df
All checks were successful
arcad/emissary/pipeline/head This commit looks good
feat: agent specifications query and get endpoints
2024-03-13 16:07:16 +01:00

56 lines
1.3 KiB
Go

package api
import (
"net/http"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
)
func (m *Mount) queryAgentSpec(w http.ResponseWriter, r *http.Request) {
agentID, ok := getAgentID(w, r)
if !ok {
return
}
ctx := r.Context()
specHeaders, err := m.agentRepo.QuerySpecs(ctx, agentID)
if err != nil {
if errors.Is(err, datastore.ErrNotFound) {
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
return
}
err = errors.WithStack(err)
logger.Error(ctx, "could not query specs", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
return
}
// Retro-compatibility mode: return full specs temporarily
specs := make([]*datastore.Spec, 0, len(specHeaders))
for _, sh := range specHeaders {
spec, err := m.agentRepo.GetSpec(ctx, agentID, sh.DefinitionName, sh.DefinitionVersion)
if err != nil {
err = errors.WithStack(err)
logger.Error(ctx, "could not query specs", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
}
specs = append(specs, spec)
}
api.DataResponse(w, http.StatusOK, struct {
Specs []*datastore.Spec `json:"specs"`
}{
Specs: specs,
})
}