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 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 // Config represents a Portainer client configuration
type Config struct { type Config struct {
URL *url.URL URL *url.URL
@ -49,7 +58,7 @@ type PortainerClient interface {
StackList(options StackListOptions) ([]portainer.Stack, error) StackList(options StackListOptions) ([]portainer.Stack, error)
// Create swarm stack // 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 // Create compose stack
StackCreateCompose(stackName string, environmentVariables []portainer.Pair, stackFileContent string, endpointID portainer.EndpointID) (stack portainer.Stack, err error) 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 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{ reqBody := StackCreateRequest{
Name: stackName, Name: options.StackName,
Env: environmentVariables, Env: options.EnvironmentVariables,
SwarmID: swarmClusterID, SwarmID: options.SwarmClusterID,
StackFileContent: stackFileContent, 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 return
} }

View File

@ -3,6 +3,8 @@ package cmd
import ( import (
"io/ioutil" "io/ioutil"
"github.com/greenled/portainer-stack-utils/client"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -135,7 +137,13 @@ var stackDeployCmd = &cobra.Command{
"stack": stackName, "stack": stackName,
"endpoint": endpoint.Name, "endpoint": endpoint.Name,
}).Info("Creating stack") }).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) common.CheckError(deploymentErr)
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"stack": stack.Name, "stack": stack.Name,