diff --git a/api/http/handler/edgestacks/edgestack_create.go b/api/http/handler/edgestacks/edgestack_create.go index c2f338ac2..bc5beca5a 100644 --- a/api/http/handler/edgestacks/edgestack_create.go +++ b/api/http/handler/edgestacks/edgestack_create.go @@ -340,11 +340,9 @@ func (handler *Handler) storeManifestFromGitRepository(stackFolder string, relat projectPath = handler.FileService.GetEdgeStackProjectPath(stackFolder) repositoryUsername := "" repositoryPassword := "" - if repositoryConfig.Authentication != nil { - if repositoryConfig.Authentication.Password != "" { - repositoryUsername = repositoryConfig.Authentication.Username - repositoryPassword = repositoryConfig.Authentication.Password - } + if repositoryConfig.Authentication != nil && repositoryConfig.Authentication.Password != "" { + repositoryUsername = repositoryConfig.Authentication.Username + repositoryPassword = repositoryConfig.Authentication.Password } err = handler.GitService.CloneRepository(projectPath, repositoryConfig.URL, repositoryConfig.ReferenceName, repositoryUsername, repositoryPassword) diff --git a/api/http/handler/edgestacks/edgestack_file.go b/api/http/handler/edgestacks/edgestack_file.go index 62ed9e4c1..ee5f96f08 100644 --- a/api/http/handler/edgestacks/edgestack_file.go +++ b/api/http/handler/edgestacks/edgestack_file.go @@ -33,10 +33,8 @@ func (handler *Handler) edgeStackFile(w http.ResponseWriter, r *http.Request) *h } stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) - if handler.DataStore.IsErrObjectNotFound(err) { - return httperror.NotFound("Unable to find an edge stack with the specified identifier inside the database", err) - } else if err != nil { - return httperror.InternalServerError("Unable to find an edge stack with the specified identifier inside the database", err) + if err != nil { + return handler.handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") } fileName := stack.EntryPoint diff --git a/api/http/handler/edgestacks/edgestack_inspect.go b/api/http/handler/edgestacks/edgestack_inspect.go index e74e02cd0..2be90a3a7 100644 --- a/api/http/handler/edgestacks/edgestack_inspect.go +++ b/api/http/handler/edgestacks/edgestack_inspect.go @@ -29,10 +29,8 @@ func (handler *Handler) edgeStackInspect(w http.ResponseWriter, r *http.Request) } edgeStack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(edgeStackID)) - if handler.DataStore.IsErrObjectNotFound(err) { - return httperror.NotFound("Unable to find an edge stack with the specified identifier inside the database", err) - } else if err != nil { - return httperror.InternalServerError("Unable to find an edge stack with the specified identifier inside the database", err) + if err != nil { + return handler.handlerDBErr(err, "Unable to find an edge stack with the specified identifier inside the database") } return response.JSON(w, edgeStack) diff --git a/api/http/handler/edgestacks/edgestack_status_delete.go b/api/http/handler/edgestacks/edgestack_status_delete.go index f89f738dc..1ffdabe83 100644 --- a/api/http/handler/edgestacks/edgestack_status_delete.go +++ b/api/http/handler/edgestacks/edgestack_status_delete.go @@ -10,16 +10,6 @@ import ( "github.com/portainer/portainer/api/http/middlewares" ) -func (handler *Handler) handlerDBErr(err error, msg string) *httperror.HandlerError { - httpErr := httperror.InternalServerError(msg, err) - - if handler.DataStore.IsErrObjectNotFound(err) { - httpErr.StatusCode = http.StatusNotFound - } - - return httpErr -} - // @id EdgeStackStatusDelete // @summary Delete an EdgeStack status // @description Authorized only if the request is done by an Edge Environment(Endpoint) diff --git a/api/http/handler/edgestacks/edgestack_status_update.go b/api/http/handler/edgestacks/edgestack_status_update.go index 14f4bc957..fc2aea0dc 100644 --- a/api/http/handler/edgestacks/edgestack_status_update.go +++ b/api/http/handler/edgestacks/edgestack_status_update.go @@ -60,10 +60,8 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req } endpoint, err := handler.DataStore.Endpoint().Endpoint(payload.EndpointID) - if handler.DataStore.IsErrObjectNotFound(err) { - return httperror.NotFound("Unable to find an environment with the specified identifier inside the database", err) - } else if err != nil { - return httperror.InternalServerError("Unable to find an environment with the specified identifier inside the database", err) + if err != nil { + return handler.handlerDBErr(err, "Unable to find an environment with the specified identifier inside the database") } err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint) @@ -98,10 +96,8 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req stack = *edgeStack }) - if handler.DataStore.IsErrObjectNotFound(err) { - return httperror.NotFound("Unable to find a stack with the specified identifier inside the database", err) - } else if err != nil { - return httperror.InternalServerError("Unable to persist the stack changes inside the database", err) + if err != nil { + return handler.handlerDBErr(err, "Unable to persist the stack changes inside the database") } return response.JSON(w, stack) diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 168de1395..c748685d6 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -55,10 +55,8 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) - if handler.DataStore.IsErrObjectNotFound(err) { - return httperror.NotFound("Unable to find a stack with the specified identifier inside the database", err) - } else if err != nil { - return httperror.InternalServerError("Unable to find a stack with the specified identifier inside the database", err) + if err != nil { + return handler.handlerDBErr(err, "Unable to find a stack with the specified identifier inside the database") } var payload updateEdgeStackPayload diff --git a/api/http/handler/edgestacks/handler.go b/api/http/handler/edgestacks/handler.go index 1a9e39617..b7eb8cf23 100644 --- a/api/http/handler/edgestacks/handler.go +++ b/api/http/handler/edgestacks/handler.go @@ -85,3 +85,13 @@ func (handler *Handler) convertAndStoreKubeManifestIfNeeded(stackFolder string, return komposeFileName, nil } + +func (handler *Handler) handlerDBErr(err error, msg string) *httperror.HandlerError { + httpErr := httperror.InternalServerError(msg, err) + + if handler.DataStore.IsErrObjectNotFound(err) { + httpErr.StatusCode = http.StatusNotFound + } + + return httpErr +}