From 768410ce20a7470f1cca6ce30a511aa90650fe89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Mon, 26 Aug 2019 01:46:28 -0400 Subject: [PATCH] Add options object for PortainerClient.StackCreateSwarm() --- client/client.go | 23 ++++++++++++++++------- cmd/stackDeploy.go | 10 +++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index c16a8ff..fdf975a 100644 --- a/client/client.go +++ b/client/client.go @@ -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 } diff --git a/cmd/stackDeploy.go b/cmd/stackDeploy.go index 0f5dc18..4b0b0c5 100644 --- a/cmd/stackDeploy.go +++ b/cmd/stackDeploy.go @@ -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,