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-11-30 16:01:35 +01:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-02-02 10:55:24 +01:00
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(ctx, "could not validate spec", logger.CapturedE(err))
|
2023-10-13 12:30:52 +02:00
|
|
|
|
2023-02-28 15:50:35 +01:00
|
|
|
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 {
|
2023-04-20 12:22:30 +02:00
|
|
|
if errors.Is(err, datastore.ErrNotFound) {
|
|
|
|
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
if errors.Is(err, datastore.ErrUnexpectedRevision) {
|
|
|
|
api.ErrorResponse(w, http.StatusConflict, ErrCodeUnexpectedRevision, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(ctx, "could not update spec", logger.CapturedE(err))
|
2023-10-13 12:30:52 +02:00
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
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 {
|
2023-04-20 12:22:30 +02:00
|
|
|
if errors.Is(err, datastore.ErrNotFound) {
|
|
|
|
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(ctx, "could not list specs", logger.CapturedE(err))
|
2023-10-13 12:30:52 +02:00
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(ctx, "could not delete spec", logger.CapturedE(err))
|
2023-10-13 12:30:52 +02:00
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
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 {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(r.Context(), "could not parse spec id", logger.CapturedE(err))
|
2023-10-13 12:30:52 +02:00
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil)
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return datastore.SpecID(specID), true
|
|
|
|
}
|