Merge pull request #24173 from vdemeester/tasks-filter-service-node-by-name

Allow service and node filter to be name in `tasks` subcommands
This commit is contained in:
Alexander Morozov 2016-07-05 12:16:57 -07:00 committed by GitHub
commit db75aa029d
6 changed files with 53 additions and 7 deletions

View file

@ -35,9 +35,10 @@ func NewNodeCommand(dockerCli *client.DockerCli) *cobra.Command {
return cmd
}
func nodeReference(client apiclient.APIClient, ctx context.Context, ref string) (string, error) {
// The special value "self" for a node reference is mapped to the current
// node, hence the node ID is retrieved using the `/info` endpoint.
// Reference return the reference of a node. The special value "self" for a node
// reference is mapped to the current node, hence the node ID is retrieved using
// the `/info` endpoint.
func Reference(client apiclient.APIClient, ctx context.Context, ref string) (string, error) {
if ref == "self" {
info, err := client.Info(ctx)
if err != nil {

View file

@ -45,7 +45,7 @@ func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()
getRef := func(ref string) (interface{}, []byte, error) {
nodeRef, err := nodeReference(client, ctx, ref)
nodeRef, err := Reference(client, ctx, ref)
if err != nil {
return nil, nil, err
}

View file

@ -44,7 +44,7 @@ func runTasks(dockerCli *client.DockerCli, opts tasksOptions) error {
client := dockerCli.Client()
ctx := context.Background()
nodeRef, err := nodeReference(client, ctx, opts.nodeID)
nodeRef, err := Reference(client, ctx, opts.nodeID)
if err != nil {
return nil
}

View file

@ -5,6 +5,7 @@ import (
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/client/idresolver"
"github.com/docker/docker/api/client/node"
"github.com/docker/docker/api/client/task"
"github.com/docker/docker/cli"
"github.com/docker/docker/opts"
@ -56,6 +57,18 @@ func runTasks(dockerCli *client.DockerCli, opts tasksOptions) error {
filter.Add("desired-state", string(swarm.TaskStateAccepted))
}
if filter.Include("node") {
nodeFilters := filter.Get("node")
for _, nodeFilter := range nodeFilters {
nodeReference, err := node.Reference(client, ctx, nodeFilter)
if err != nil {
return err
}
filter.Del("node", nodeFilter)
filter.Add("node", nodeReference)
}
}
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: filter})
if err != nil {
return err

View file

@ -22,6 +22,7 @@ import (
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/runconfig"
apitypes "github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/filters"
types "github.com/docker/engine-api/types/swarm"
swarmagent "github.com/docker/swarmkit/agent"
swarmapi "github.com/docker/swarmkit/api"
@ -884,7 +885,33 @@ func (c *Cluster) GetTasks(options apitypes.TaskListOptions) ([]types.Task, erro
return nil, c.errNoManager()
}
filters, err := newListTasksFilters(options.Filter)
byName := func(filter filters.Args) error {
if filter.Include("service") {
serviceFilters := filter.Get("service")
for _, serviceFilter := range serviceFilters {
service, err := c.GetService(serviceFilter)
if err != nil {
return err
}
filter.Del("service", serviceFilter)
filter.Add("service", service.ID)
}
}
if filter.Include("node") {
nodeFilters := filter.Get("node")
for _, nodeFilter := range nodeFilters {
node, err := c.GetNode(nodeFilter)
if err != nil {
return err
}
filter.Del("node", nodeFilter)
filter.Add("node", node.ID)
}
}
return nil
}
filters, err := newListTasksFilters(options.Filter, byName)
if err != nil {
return nil, err
}

View file

@ -61,7 +61,7 @@ func newListServicesFilters(filter filters.Args) (*swarmapi.ListServicesRequest_
}, nil
}
func newListTasksFilters(filter filters.Args) (*swarmapi.ListTasksRequest_Filters, error) {
func newListTasksFilters(filter filters.Args, transformFunc func(filters.Args) error) (*swarmapi.ListTasksRequest_Filters, error) {
accepted := map[string]bool{
"name": true,
"id": true,
@ -73,6 +73,11 @@ func newListTasksFilters(filter filters.Args) (*swarmapi.ListTasksRequest_Filter
if err := filter.Validate(accepted); err != nil {
return nil, err
}
if transformFunc != nil {
if err := transformFunc(filter); err != nil {
return nil, err
}
}
f := &swarmapi.ListTasksRequest_Filters{
Names: filter.Get("name"),
IDPrefixes: filter.Get("id"),