diff --git a/client/client.go b/client/client.go index 0566d4c..c16a8ff 100644 --- a/client/client.go +++ b/client/client.go @@ -19,6 +19,11 @@ type StackListFilter struct { EndpointID portainer.EndpointID `json:"EndpointId,omitempty"` } +// StackListOptions represents options passed to PortainerClient.StackList() +type StackListOptions struct { + Filter StackListFilter +} + // Config represents a Portainer client configuration type Config struct { URL *url.URL @@ -41,7 +46,7 @@ type PortainerClient interface { EndpointGroupList() ([]portainer.EndpointGroup, error) // Get stacks, optionally filtered by swarmId and endpointId - StackList(swarmID string, endpointID portainer.EndpointID) ([]portainer.Stack, error) + 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) @@ -231,13 +236,8 @@ func (n *portainerClientImp) EndpointGroupList() (endpointGroups []portainer.End return } -func (n *portainerClientImp) StackList(swarmID string, endpointID portainer.EndpointID) (stacks []portainer.Stack, err error) { - filter := StackListFilter{ - SwarmID: swarmID, - EndpointID: endpointID, - } - - filterJSONBytes, _ := json.Marshal(filter) +func (n *portainerClientImp) StackList(options StackListOptions) (stacks []portainer.Stack, err error) { + filterJSONBytes, _ := json.Marshal(options.Filter) filterJSONString := string(filterJSONBytes) err = n.doJSONWithToken(fmt.Sprintf("stacks?filters=%s", filterJSONString), http.MethodGet, http.Header{}, nil, &stacks) diff --git a/cmd/stackList.go b/cmd/stackList.go index d073427..88cdfa8 100644 --- a/cmd/stackList.go +++ b/cmd/stackList.go @@ -53,14 +53,23 @@ var stackListCmd = &cobra.Command{ logrus.WithFields(logrus.Fields{ "endpoint": endpoint.Name, }).Debug("Getting stacks") - stacks, err = portainerClient.StackList(endpointSwarmClusterID, endpoint.ID) + stacks, err = portainerClient.StackList(client.StackListOptions{ + Filter: client.StackListFilter{ + SwarmID: endpointSwarmClusterID, + EndpointID: endpoint.ID, + }, + }) common.CheckError(err) } else if selectionErr == common.ErrStackClusterNotFound { // It's not a swarm cluster logrus.WithFields(logrus.Fields{ "endpoint": endpoint.Name, }).Debug("Getting stacks") - stacks, err = portainerClient.StackList("", endpoint.ID) + stacks, err = portainerClient.StackList(client.StackListOptions{ + Filter: client.StackListFilter{ + EndpointID: endpoint.ID, + }, + }) common.CheckError(err) } else { // Something else happened @@ -68,7 +77,7 @@ var stackListCmd = &cobra.Command{ } } else { logrus.Debug("Getting stacks") - stacks, err = portainerClient.StackList("", 0) + stacks, err = portainerClient.StackList(client.StackListOptions{}) common.CheckError(err) } diff --git a/common/utils.go b/common/utils.go index 28a1d09..1b34702 100644 --- a/common/utils.go +++ b/common/utils.go @@ -4,6 +4,8 @@ import ( "fmt" "reflect" + "github.com/greenled/portainer-stack-utils/client" + portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" ) @@ -63,7 +65,12 @@ func GetStackByName(name string, swarmID string, endpointID portainer.EndpointID return } - stacks, err := portainerClient.StackList(swarmID, endpointID) + stacks, err := portainerClient.StackList(client.StackListOptions{ + Filter: client.StackListFilter{ + SwarmID: swarmID, + EndpointID: endpointID, + }, + }) if err != nil { return }