2022-04-28 19:50:23 +00:00
|
|
|
package edgestacks
|
|
|
|
|
|
|
|
import (
|
2023-05-05 23:39:22 +00:00
|
|
|
"errors"
|
2022-04-28 19:50:23 +00:00
|
|
|
"net/http"
|
2023-07-13 20:55:52 +00:00
|
|
|
"time"
|
2022-04-28 19:50:23 +00:00
|
|
|
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-05-05 23:39:22 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2022-04-28 19:50:23 +00:00
|
|
|
"github.com/portainer/portainer/api/http/middlewares"
|
2023-05-05 23:39:22 +00:00
|
|
|
"github.com/portainer/portainer/pkg/featureflags"
|
2022-04-28 19:50:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// @id EdgeStackStatusDelete
|
|
|
|
// @summary Delete an EdgeStack status
|
|
|
|
// @description Authorized only if the request is done by an Edge Environment(Endpoint)
|
|
|
|
// @tags edge_stacks
|
|
|
|
// @produce json
|
2023-04-04 04:59:34 +00:00
|
|
|
// @param id path int true "EdgeStack Id"
|
2023-04-27 04:03:55 +00:00
|
|
|
// @param environmentId path int true "Environment identifier"
|
2022-04-28 19:50:23 +00:00
|
|
|
// @success 200 {object} portainer.EdgeStack
|
|
|
|
// @failure 500
|
|
|
|
// @failure 400
|
|
|
|
// @failure 404
|
|
|
|
// @failure 403
|
2023-07-13 20:55:52 +00:00
|
|
|
// @deprecated
|
2023-04-27 04:03:55 +00:00
|
|
|
// @router /edge_stacks/{id}/status/{environmentId} [delete]
|
2022-04-28 19:50:23 +00:00
|
|
|
func (handler *Handler) edgeStackStatusDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid stack identifier route variable", err)
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := middlewares.FetchEndpoint(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve a valid endpoint from the handler context", err)
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.Forbidden("Permission denied to access environment", err)
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 23:39:22 +00:00
|
|
|
var stack *portainer.EdgeStack
|
|
|
|
if featureflags.IsEnabled(portainer.FeatureNoTx) {
|
|
|
|
stack, err = handler.deleteEdgeStackStatus(handler.DataStore, portainer.EdgeStackID(stackID), endpoint)
|
|
|
|
} else {
|
|
|
|
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
|
|
stack, err = handler.deleteEdgeStackStatus(tx, portainer.EdgeStackID(stackID), endpoint)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-04-28 19:50:23 +00:00
|
|
|
if err != nil {
|
2023-05-05 23:39:22 +00:00
|
|
|
var httpErr *httperror.HandlerError
|
|
|
|
if errors.As(err, &httpErr) {
|
|
|
|
return httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) deleteEdgeStackStatus(tx dataservices.DataStoreTx, stackID portainer.EdgeStackID, endpoint *portainer.Endpoint) (*portainer.EdgeStack, error) {
|
|
|
|
stack, err := tx.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, handler.handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database")
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:55:52 +00:00
|
|
|
environmentStatus, ok := stack.Status[endpoint.ID]
|
|
|
|
if !ok {
|
|
|
|
environmentStatus = portainer.EdgeStackStatus{}
|
|
|
|
}
|
|
|
|
|
|
|
|
environmentStatus.Status = append(environmentStatus.Status, portainer.EdgeStackDeploymentStatus{
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
Type: portainer.EdgeStackStatusRemoved,
|
|
|
|
})
|
|
|
|
|
|
|
|
stack.Status[endpoint.ID] = environmentStatus
|
2022-04-28 19:50:23 +00:00
|
|
|
|
2023-05-05 23:39:22 +00:00
|
|
|
err = tx.EdgeStack().UpdateEdgeStack(stack.ID, stack)
|
2022-04-28 19:50:23 +00:00
|
|
|
if err != nil {
|
2023-05-05 23:39:22 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to persist the stack changes inside the database", err)
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 23:39:22 +00:00
|
|
|
return stack, nil
|
2022-04-28 19:50:23 +00:00
|
|
|
}
|