Add options object for PortainerClient.StackUpdate()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-26 01:52:41 -04:00
parent 17b941d108
commit d17b9eb9f5
2 changed files with 22 additions and 7 deletions

View File

@ -41,6 +41,15 @@ type StackCreateComposeOptions struct {
EndpointID portainer.EndpointID EndpointID portainer.EndpointID
} }
// StackUpdateOptions represents options passed to PortainerClient.StackUpdate()
type StackUpdateOptions struct {
Stack portainer.Stack
EnvironmentVariables []portainer.Pair
StackFileContent string
Prune bool
EndpointID portainer.EndpointID
}
// Config represents a Portainer client configuration // Config represents a Portainer client configuration
type Config struct { type Config struct {
URL *url.URL URL *url.URL
@ -72,7 +81,7 @@ type PortainerClient interface {
StackCreateCompose(options StackCreateComposeOptions) (stack portainer.Stack, err error) StackCreateCompose(options StackCreateComposeOptions) (stack portainer.Stack, err error)
// Update stack // Update stack
StackUpdate(stack portainer.Stack, environmentVariables []portainer.Pair, stackFileContent string, prune bool, endpointID portainer.EndpointID) error StackUpdate(options StackUpdateOptions) error
// Delete stack // Delete stack
StackDelete(stackID portainer.StackID) error StackDelete(stackID portainer.StackID) error
@ -284,14 +293,14 @@ func (n *portainerClientImp) StackCreateCompose(options StackCreateComposeOption
return return
} }
func (n *portainerClientImp) StackUpdate(stack portainer.Stack, environmentVariables []portainer.Pair, stackFileContent string, prune bool, endpointID portainer.EndpointID) (err error) { func (n *portainerClientImp) StackUpdate(options StackUpdateOptions) (err error) {
reqBody := StackUpdateRequest{ reqBody := StackUpdateRequest{
Env: environmentVariables, Env: options.EnvironmentVariables,
StackFileContent: stackFileContent, StackFileContent: options.StackFileContent,
Prune: prune, Prune: options.Prune,
} }
err = n.doJSONWithToken(fmt.Sprintf("stacks/%v?endpointId=%v", stack.ID, endpointID), http.MethodPut, http.Header{}, &reqBody, nil) err = n.doJSONWithToken(fmt.Sprintf("stacks/%v?endpointId=%v", options.Stack.ID, options.EndpointID), http.MethodPut, http.Header{}, &reqBody, nil)
return return
} }

View File

@ -117,7 +117,13 @@ var stackDeployCmd = &cobra.Command{
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"stack": retrievedStack.Name, "stack": retrievedStack.Name,
}).Info("Updating stack") }).Info("Updating stack")
err := portainerClient.StackUpdate(retrievedStack, newEnvironmentVariables, stackFileContent, viper.GetBool("stack.deploy.prune"), endpoint.ID) err := portainerClient.StackUpdate(client.StackUpdateOptions{
Stack: retrievedStack,
EnvironmentVariables: newEnvironmentVariables,
StackFileContent: stackFileContent,
Prune: viper.GetBool("stack.deploy.prune"),
EndpointID: endpoint.ID,
})
common.CheckError(err) common.CheckError(err)
} else if stackRetrievalErr == common.ErrStackNotFound { } else if stackRetrievalErr == common.ErrStackNotFound {
// We are deploying a new stack // We are deploying a new stack