1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-05 05:45:22 +02:00

refactor(api): use a standard stack identifier (#1980)

This commit is contained in:
Anthony Lapenna 2018-06-18 12:07:56 +02:00 committed by GitHub
parent da5a430b8c
commit b4c2820ad7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 150 additions and 38 deletions

View file

@ -46,9 +46,9 @@ func (handler *Handler) createComposeStackFromFileContent(w http.ResponseWriter,
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerComposeStack,
EndpointID: endpoint.ID,
@ -126,9 +126,9 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerComposeStack,
EndpointID: endpoint.ID,
@ -211,9 +211,9 @@ func (handler *Handler) createComposeStackFromFileUpload(w http.ResponseWriter,
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerComposeStack,
EndpointID: endpoint.ID,

View file

@ -51,9 +51,9 @@ func (handler *Handler) createSwarmStackFromFileContent(w http.ResponseWriter, r
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerSwarmStack,
SwarmID: payload.SwarmID,
@ -138,9 +138,9 @@ func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter,
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerSwarmStack,
SwarmID: payload.SwarmID,
@ -239,9 +239,9 @@ func (handler *Handler) createSwarmStackFromFileUpload(w http.ResponseWriter, r
}
}
stackIdentifier := buildStackIdentifier(payload.Name, endpoint.ID)
stackID := handler.StackService.GetNextIdentifier()
stack := &portainer.Stack{
ID: portainer.StackID(stackIdentifier),
ID: portainer.StackID(stackID),
Name: payload.Name,
Type: portainer.DockerSwarmStack,
SwarmID: payload.SwarmID,

View file

@ -1,8 +1,8 @@
package stacks
import (
"log"
"net/http"
"strconv"
"github.com/portainer/portainer"
httperror "github.com/portainer/portainer/http/error"
@ -14,14 +14,13 @@ func (handler *Handler) cleanUp(stack *portainer.Stack, doCleanUp *bool) error {
return nil
}
handler.FileService.RemoveDirectory(stack.ProjectPath)
err := handler.FileService.RemoveDirectory(stack.ProjectPath)
if err != nil {
log.Printf("http error: Unable to cleanup stack creation (err=%s)\n", err)
}
return nil
}
func buildStackIdentifier(stackName string, endpointID portainer.EndpointID) string {
return stackName + "_" + strconv.Itoa(int(endpointID))
}
// POST request on /api/stacks?type=<type>&method=<method>&endpointId=<endpointId>
func (handler *Handler) stackCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackType, err := request.RetrieveNumericQueryParameter(r, "type", false)

View file

@ -2,6 +2,7 @@ package stacks
import (
"net/http"
"strconv"
"github.com/portainer/portainer"
httperror "github.com/portainer/portainer/http/error"
@ -12,6 +13,8 @@ import (
)
// DELETE request on /api/stacks/:id?external=<external>&endpointId=<endpointId>
// If the external query parameter is set to true, the id route variable is expected to be
// the name of an external stack as a string.
func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveRouteVariableValue(r, "id")
if err != nil {
@ -23,7 +26,12 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt
return handler.deleteExternalStack(r, w, stackID)
}
stack, err := handler.StackService.Stack(portainer.StackID(stackID))
id, err := strconv.Atoi(stackID)
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
}
stack, err := handler.StackService.Stack(portainer.StackID(id))
if err == portainer.ErrStackNotFound {
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err}
} else if err != nil {
@ -71,7 +79,7 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt
return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err}
}
err = handler.StackService.DeleteStack(portainer.StackID(stackID))
err = handler.StackService.DeleteStack(portainer.StackID(id))
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the stack from the database", err}
}

View file

@ -18,7 +18,7 @@ type stackFileResponse struct {
// GET request on /api/stacks/:id/file
func (handler *Handler) stackFile(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveRouteVariableValue(r, "id")
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
}

View file

@ -13,7 +13,7 @@ import (
// GET request on /api/stacks/:id
func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveRouteVariableValue(r, "id")
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
}

View file

@ -38,7 +38,7 @@ func (payload *updateSwarmStackPayload) Validate(r *http.Request) error {
// PUT request on /api/stacks/:id?endpointId=<endpointId>
func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
stackID, err := request.RetrieveRouteVariableValue(r, "id")
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
}