Add options object for PortainerClient.StackList()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-26 01:40:15 -04:00
parent f38481b008
commit 26a3efdd3a
3 changed files with 28 additions and 12 deletions

View File

@ -19,6 +19,11 @@ type StackListFilter struct {
EndpointID portainer.EndpointID `json:"EndpointId,omitempty"` EndpointID portainer.EndpointID `json:"EndpointId,omitempty"`
} }
// StackListOptions represents options passed to PortainerClient.StackList()
type StackListOptions struct {
Filter StackListFilter
}
// Config represents a Portainer client configuration // Config represents a Portainer client configuration
type Config struct { type Config struct {
URL *url.URL URL *url.URL
@ -41,7 +46,7 @@ type PortainerClient interface {
EndpointGroupList() ([]portainer.EndpointGroup, error) EndpointGroupList() ([]portainer.EndpointGroup, error)
// Get stacks, optionally filtered by swarmId and endpointId // 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 // Create swarm stack
StackCreateSwarm(stackName string, environmentVariables []portainer.Pair, stackFileContent string, swarmClusterID string, endpointID portainer.EndpointID) (stack portainer.Stack, err error) 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 return
} }
func (n *portainerClientImp) StackList(swarmID string, endpointID portainer.EndpointID) (stacks []portainer.Stack, err error) { func (n *portainerClientImp) StackList(options StackListOptions) (stacks []portainer.Stack, err error) {
filter := StackListFilter{ filterJSONBytes, _ := json.Marshal(options.Filter)
SwarmID: swarmID,
EndpointID: endpointID,
}
filterJSONBytes, _ := json.Marshal(filter)
filterJSONString := string(filterJSONBytes) filterJSONString := string(filterJSONBytes)
err = n.doJSONWithToken(fmt.Sprintf("stacks?filters=%s", filterJSONString), http.MethodGet, http.Header{}, nil, &stacks) err = n.doJSONWithToken(fmt.Sprintf("stacks?filters=%s", filterJSONString), http.MethodGet, http.Header{}, nil, &stacks)

View File

@ -53,14 +53,23 @@ var stackListCmd = &cobra.Command{
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"endpoint": endpoint.Name, "endpoint": endpoint.Name,
}).Debug("Getting stacks") }).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) common.CheckError(err)
} else if selectionErr == common.ErrStackClusterNotFound { } else if selectionErr == common.ErrStackClusterNotFound {
// It's not a swarm cluster // It's not a swarm cluster
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"endpoint": endpoint.Name, "endpoint": endpoint.Name,
}).Debug("Getting stacks") }).Debug("Getting stacks")
stacks, err = portainerClient.StackList("", endpoint.ID) stacks, err = portainerClient.StackList(client.StackListOptions{
Filter: client.StackListFilter{
EndpointID: endpoint.ID,
},
})
common.CheckError(err) common.CheckError(err)
} else { } else {
// Something else happened // Something else happened
@ -68,7 +77,7 @@ var stackListCmd = &cobra.Command{
} }
} else { } else {
logrus.Debug("Getting stacks") logrus.Debug("Getting stacks")
stacks, err = portainerClient.StackList("", 0) stacks, err = portainerClient.StackList(client.StackListOptions{})
common.CheckError(err) common.CheckError(err)
} }

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"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"
) )
@ -63,7 +65,12 @@ func GetStackByName(name string, swarmID string, endpointID portainer.EndpointID
return return
} }
stacks, err := portainerClient.StackList(swarmID, endpointID) stacks, err := portainerClient.StackList(client.StackListOptions{
Filter: client.StackListFilter{
SwarmID: swarmID,
EndpointID: endpointID,
},
})
if err != nil { if err != nil {
return return
} }