emissary/internal/server/api/release_agent.go

59 lines
1.3 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 releaseAgentRequest struct {
AgentID int64 `json:"agentId" validate:"required"`
}
func (m *Mount) releaseAgent(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
releaseAgentReq := &releaseAgentRequest{}
if ok := api.Bind(w, r, releaseAgentReq); !ok {
return
}
agentID := datastore.AgentID(releaseAgentReq.AgentID)
if ok := m.assertTenantOwns(w, r, agentID); !ok {
return
}
agent, err := m.agentRepo.Get(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 retrieve agent", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
return
}
agent, err = m.agentRepo.Detach(ctx, agent.ID)
if err != nil {
err = errors.WithStack(err)
logger.Error(ctx, "could not detach agent", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
return
}
api.DataResponse(w, http.StatusOK, struct {
Agent *datastore.Agent `json:"agent"`
}{
Agent: agent,
})
}