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"`
}
// 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)

View File

@ -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)
}

View File

@ -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
}