emissary/internal/server/api/delete_agent_spec.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-02-27 09:56:15 +01:00
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"
)
type deleteSpecRequest struct {
Name string `json:"name" validate:"required"`
Version string `json:"version" validate:"required"`
2024-02-27 09:56:15 +01:00
}
func (m *Mount) deleteAgentSpec(w http.ResponseWriter, r *http.Request) {
2024-02-27 09:56:15 +01:00
agentID, ok := getAgentID(w, r)
if !ok {
return
}
deleteSpecReq := &deleteSpecRequest{}
if ok := api.Bind(w, r, deleteSpecReq); !ok {
return
}
ctx := r.Context()
err := m.agentRepo.DeleteSpec(
ctx,
agentID,
deleteSpecReq.Name,
deleteSpecReq.Version,
2024-02-27 09:56:15 +01:00
)
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 delete spec", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
return
}
api.DataResponse(w, http.StatusOK, struct {
Name string `json:"name"`
Version string `json:"version"`
2024-02-27 09:56:15 +01:00
}{
Name: deleteSpecReq.Name,
Version: deleteSpecReq.Version,
2024-02-27 09:56:15 +01:00
})
}