feat: cli client with spec schema validation
This commit is contained in:
@ -57,7 +57,7 @@ func (s *Server) registerAgent(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
type updateAgentRequest struct {
|
||||
Status *datastore.AgentStatus `json:"status"`
|
||||
Status *datastore.AgentStatus `json:"status" validate:"omitempty,oneof=0 1 2 3"`
|
||||
}
|
||||
|
||||
func (s *Server) updateAgent(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/spec"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
@ -16,9 +17,7 @@ const (
|
||||
)
|
||||
|
||||
type updateSpecRequest struct {
|
||||
Name string `json:"name"`
|
||||
Revision int `json:"revision"`
|
||||
Data map[string]any `json:"data"`
|
||||
spec.RawSpec
|
||||
}
|
||||
|
||||
func (s *Server) updateSpec(w http.ResponseWriter, r *http.Request) {
|
||||
@ -34,12 +33,29 @@ func (s *Server) updateSpec(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if ok, err := spec.Validate(ctx, updateSpecReq); !ok || err != nil {
|
||||
data := struct {
|
||||
Message string `json:"message"`
|
||||
}{}
|
||||
|
||||
var validationErr *spec.ValidationError
|
||||
|
||||
if errors.As(err, &validationErr) {
|
||||
data.Message = validationErr.Error()
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not validate spec", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeInvalidRequest, data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
spec, err := s.agentRepo.UpdateSpec(
|
||||
ctx,
|
||||
datastore.AgentID(agentID),
|
||||
updateSpecReq.Name,
|
||||
updateSpecReq.Revision,
|
||||
updateSpecReq.Data,
|
||||
string(updateSpecReq.SpecName()),
|
||||
updateSpecReq.SpecRevision(),
|
||||
updateSpecReq.SpecData(),
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, datastore.ErrUnexpectedRevision) {
|
||||
|
Reference in New Issue
Block a user