2023-02-02 10:55:24 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
2023-02-28 15:50:35 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/spec"
|
2023-02-02 10:55:24 +01:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/api"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrCodeUnexpectedRevision api.ErrorCode = "unexpected-revision"
|
|
|
|
)
|
|
|
|
|
|
|
|
type updateSpecRequest struct {
|
2023-02-28 15:50:35 +01:00
|
|
|
spec.RawSpec
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateSpec(w http.ResponseWriter, r *http.Request) {
|
|
|
|
agentID, ok := getAgentID(w, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
updateSpecReq := &updateSpecRequest{}
|
|
|
|
if ok := api.Bind(w, r, updateSpecReq); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
if err := spec.Validate(ctx, updateSpecReq); err != nil {
|
2023-02-28 15:50:35 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
spec, err := s.agentRepo.UpdateSpec(
|
|
|
|
ctx,
|
|
|
|
datastore.AgentID(agentID),
|
2023-02-28 15:50:35 +01:00
|
|
|
string(updateSpecReq.SpecName()),
|
|
|
|
updateSpecReq.SpecRevision(),
|
|
|
|
updateSpecReq.SpecData(),
|
2023-02-02 10:55:24 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, datastore.ErrUnexpectedRevision) {
|
|
|
|
api.ErrorResponse(w, http.StatusConflict, ErrCodeUnexpectedRevision, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Error(ctx, "could not update spec", logger.E(errors.WithStack(err)))
|
|
|
|
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.DataResponse(w, http.StatusOK, struct {
|
|
|
|
Spec *datastore.Spec `json:"spec"`
|
|
|
|
}{
|
|
|
|
Spec: spec,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getAgentSpecs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
agentID, ok := getAgentID(w, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
specs, err := s.agentRepo.GetSpecs(ctx, agentID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, "could not list specs", logger.E(errors.WithStack(err)))
|
|
|
|
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.DataResponse(w, http.StatusOK, struct {
|
|
|
|
Specs []*datastore.Spec `json:"specs"`
|
|
|
|
}{
|
|
|
|
Specs: specs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteSpecRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) deleteSpec(w http.ResponseWriter, r *http.Request) {
|
|
|
|
agentID, ok := getAgentID(w, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteSpecReq := &deleteSpecRequest{}
|
|
|
|
if ok := api.Bind(w, r, deleteSpecReq); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
err := s.agentRepo.DeleteSpec(
|
|
|
|
ctx,
|
|
|
|
agentID,
|
|
|
|
deleteSpecReq.Name,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, datastore.ErrNotFound) {
|
|
|
|
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Error(ctx, "could not delete spec", logger.E(errors.WithStack(err)))
|
|
|
|
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.DataResponse(w, http.StatusOK, struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}{
|
|
|
|
Name: deleteSpecReq.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSpecID(w http.ResponseWriter, r *http.Request) (datastore.SpecID, bool) {
|
|
|
|
rawSpecID := chi.URLParam(r, "")
|
|
|
|
|
|
|
|
specID, err := strconv.ParseInt(rawSpecID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(r.Context(), "could not parse spec id", logger.E(errors.WithStack(err)))
|
|
|
|
api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil)
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return datastore.SpecID(specID), true
|
|
|
|
}
|