#2732 feat(stacks): adding saveComposeStackFromFileContent method

pull/12341/head
Cesar Munoz 2024-10-20 14:49:12 +02:00
parent 35834c65ea
commit 7268697633
No known key found for this signature in database
GPG Key ID: 1211B71005DDB995
1 changed files with 42 additions and 0 deletions

View File

@ -130,6 +130,48 @@ func (handler *Handler) createComposeStackFromFileContent(w http.ResponseWriter,
return handler.decorateStackResponse(w, stack, userID)
}
// @id StackSaveDockerStandaloneString
// @summary Save a new compose stack from a text
// @description Deploy a new stack into a Docker environment specified via the environment identifier.
// @description **Access policy**: authenticated
// @tags stacks
// @security ApiKeyAuth
// @security jwt
// @accept json
// @produce json
// @param body body composeStackFromFileContentPayload true "stack config"
// @param endpointId query int true "Identifier of the environment that will be used to deploy the stack"
// @success 200 {object} portainer.Stack
// @failure 400 "Invalid request"
// @failure 500 "Server error"
// @router /stacks/create/standalone/string [post]
func (handler *Handler) saveComposeStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
payload, handlerError := handler.getValidatedPayload(w, r, endpoint, userID)
if handlerError != nil {
return handlerError
}
securityContext, err := security.RetrieveRestrictedRequestContext(r)
if err != nil {
return httperror.InternalServerError("Unable to retrieve info from request context", err)
}
stackPayload := createStackPayloadFromComposeFileContentPayload(payload.Name, payload.StackFileContent, payload.Env, payload.FromAppTemplate)
composeStackBuilder := stackbuilders.CreateComposeStackFileContentBuilder(securityContext,
handler.DataStore,
handler.FileService,
handler.StackDeployer)
stackBuilderDirector := stackbuilders.NewStackBuilderDirector(composeStackBuilder)
stack, httpErr := stackBuilderDirector.Save(&stackPayload, endpoint)
if httpErr != nil {
return httpErr
}
return handler.decorateStackResponse(w, stack, userID)
}
func (handler *Handler) getValidatedPayload(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) (*composeStackFromFileContentPayload, *httperror.HandlerError) {
var payload composeStackFromFileContentPayload
err := request.DecodeAndValidateJSONPayload(r, &payload)