Add options object for PortainerClient.StackCreateSwarm()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-26 01:46:28 -04:00
parent 26a3efdd3a
commit 768410ce20
2 changed files with 25 additions and 8 deletions

View File

@ -24,6 +24,15 @@ type StackListOptions struct {
Filter StackListFilter
}
// StackCreateSwarmOptions represents options passed to PortainerClient.StackCreateSwarm()
type StackCreateSwarmOptions struct {
StackName string
EnvironmentVariables []portainer.Pair
StackFileContent string
SwarmClusterID string
EndpointID portainer.EndpointID
}
// Config represents a Portainer client configuration
type Config struct {
URL *url.URL
@ -49,7 +58,7 @@ type PortainerClient interface {
StackList(options StackListOptions) ([]portainer.Stack, error)
// Create swarm stack
StackCreateSwarm(stackName string, environmentVariables []portainer.Pair, stackFileContent string, swarmClusterID string, endpointID portainer.EndpointID) (stack portainer.Stack, err error)
StackCreateSwarm(options StackCreateSwarmOptions) (stack portainer.Stack, err error)
// Create compose stack
StackCreateCompose(stackName string, environmentVariables []portainer.Pair, stackFileContent string, endpointID portainer.EndpointID) (stack portainer.Stack, err error)
@ -244,15 +253,15 @@ func (n *portainerClientImp) StackList(options StackListOptions) (stacks []porta
return
}
func (n *portainerClientImp) StackCreateSwarm(stackName string, environmentVariables []portainer.Pair, stackFileContent string, swarmClusterID string, endpointID portainer.EndpointID) (stack portainer.Stack, err error) {
func (n *portainerClientImp) StackCreateSwarm(options StackCreateSwarmOptions) (stack portainer.Stack, err error) {
reqBody := StackCreateRequest{
Name: stackName,
Env: environmentVariables,
SwarmID: swarmClusterID,
StackFileContent: stackFileContent,
Name: options.StackName,
Env: options.EnvironmentVariables,
SwarmID: options.SwarmClusterID,
StackFileContent: options.StackFileContent,
}
err = n.doJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 1, "string", endpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
err = n.doJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 1, "string", options.EndpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
return
}

View File

@ -3,6 +3,8 @@ package cmd
import (
"io/ioutil"
"github.com/greenled/portainer-stack-utils/client"
portainer "github.com/portainer/portainer/api"
"github.com/sirupsen/logrus"
@ -135,7 +137,13 @@ var stackDeployCmd = &cobra.Command{
"stack": stackName,
"endpoint": endpoint.Name,
}).Info("Creating stack")
stack, deploymentErr := portainerClient.StackCreateSwarm(stackName, loadedEnvironmentVariables, stackFileContent, endpointSwarmClusterID, endpoint.ID)
stack, deploymentErr := portainerClient.StackCreateSwarm(client.StackCreateSwarmOptions{
StackName: stackName,
EnvironmentVariables: loadedEnvironmentVariables,
StackFileContent: stackFileContent,
SwarmClusterID: endpointSwarmClusterID,
EndpointID: endpoint.ID,
})
common.CheckError(deploymentErr)
logrus.WithFields(logrus.Fields{
"stack": stack.Name,