mirror of https://github.com/portainer/portainer
92 lines
3.5 KiB
Go
92 lines
3.5 KiB
Go
package stacks
|
|
|
|
import (
|
|
"fmt"
|
|
httperror "github.com/portainer/libhttp/error"
|
|
"github.com/portainer/libhttp/request"
|
|
"github.com/portainer/libhttp/response"
|
|
portainer "github.com/portainer/portainer/api"
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
|
"github.com/portainer/portainer/api/http/security"
|
|
"github.com/portainer/portainer/api/internal/stackutils"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// PUT request on /api/stacks/:id/associate?endpointId=<endpointId>&swarmId=<swarmId>&orphanedRunning=<orphanedRunning>
|
|
func (handler *Handler) stackAssociate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
|
|
}
|
|
|
|
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", false)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
|
|
}
|
|
|
|
swarmId, err := request.RetrieveQueryParameter(r, "swarmId", true)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: swarmId", err}
|
|
}
|
|
|
|
orphanedRunning, err := request.RetrieveBooleanQueryParameter(r, "orphanedRunning", false)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: orphanedRunning", err}
|
|
}
|
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
|
}
|
|
|
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to load user information from the database", err}
|
|
}
|
|
|
|
stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID))
|
|
if err == bolterrors.ErrObjectNotFound {
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err}
|
|
} else if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
|
|
}
|
|
|
|
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stackutils.ResourceControlID(stack.EndpointID, stack.Name), portainer.StackResourceControl)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err}
|
|
}
|
|
|
|
if resourceControl != nil {
|
|
resourceControl.ResourceID = fmt.Sprintf("%d_%s", endpointID, stack.Name)
|
|
|
|
err = handler.DataStore.ResourceControl().UpdateResourceControl(resourceControl.ID, resourceControl)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist resource control changes inside the database", err}
|
|
}
|
|
}
|
|
|
|
stack.EndpointID = portainer.EndpointID(endpointID)
|
|
stack.SwarmID = swarmId
|
|
|
|
if orphanedRunning {
|
|
stack.Status = portainer.StackStatusActive
|
|
} else {
|
|
stack.Status = portainer.StackStatusInactive
|
|
}
|
|
|
|
stack.CreationDate = time.Now().Unix()
|
|
stack.CreatedBy = user.Username
|
|
stack.UpdateDate = 0
|
|
stack.UpdatedBy = ""
|
|
|
|
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err}
|
|
}
|
|
|
|
stack.ResourceControl = resourceControl
|
|
|
|
return response.JSON(w, stack)
|
|
}
|