github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/cluster/filters.go (about)

     1  package cluster // import "github.com/demonoid81/moby/daemon/cluster"
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/demonoid81/moby/api/types/filters"
     8  	runconfigopts "github.com/demonoid81/moby/runconfig/opts"
     9  	swarmapi "github.com/docker/swarmkit/api"
    10  )
    11  
    12  func newListNodesFilters(filter filters.Args) (*swarmapi.ListNodesRequest_Filters, error) {
    13  	accepted := map[string]bool{
    14  		"name":       true,
    15  		"id":         true,
    16  		"label":      true,
    17  		"role":       true,
    18  		"membership": true,
    19  		"node.label": true,
    20  	}
    21  	if err := filter.Validate(accepted); err != nil {
    22  		return nil, err
    23  	}
    24  	f := &swarmapi.ListNodesRequest_Filters{
    25  		NamePrefixes: filter.Get("name"),
    26  		IDPrefixes:   filter.Get("id"),
    27  		Labels:       runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
    28  		NodeLabels:   runconfigopts.ConvertKVStringsToMap(filter.Get("node.label")),
    29  	}
    30  
    31  	for _, r := range filter.Get("role") {
    32  		if role, ok := swarmapi.NodeRole_value[strings.ToUpper(r)]; ok {
    33  			f.Roles = append(f.Roles, swarmapi.NodeRole(role))
    34  		} else if r != "" {
    35  			return nil, fmt.Errorf("Invalid role filter: '%s'", r)
    36  		}
    37  	}
    38  
    39  	for _, a := range filter.Get("membership") {
    40  		if membership, ok := swarmapi.NodeSpec_Membership_value[strings.ToUpper(a)]; ok {
    41  			f.Memberships = append(f.Memberships, swarmapi.NodeSpec_Membership(membership))
    42  		} else if a != "" {
    43  			return nil, fmt.Errorf("Invalid membership filter: '%s'", a)
    44  		}
    45  	}
    46  
    47  	return f, nil
    48  }
    49  
    50  func newListTasksFilters(filter filters.Args, transformFunc func(filters.Args) error) (*swarmapi.ListTasksRequest_Filters, error) {
    51  	accepted := map[string]bool{
    52  		"name":          true,
    53  		"id":            true,
    54  		"label":         true,
    55  		"service":       true,
    56  		"node":          true,
    57  		"desired-state": true,
    58  		// UpToDate is not meant to be exposed to users. It's for
    59  		// internal use in checking create/update progress. Therefore,
    60  		// we prefix it with a '_'.
    61  		"_up-to-date": true,
    62  		"runtime":     true,
    63  	}
    64  	if err := filter.Validate(accepted); err != nil {
    65  		return nil, err
    66  	}
    67  	if transformFunc != nil {
    68  		if err := transformFunc(filter); err != nil {
    69  			return nil, err
    70  		}
    71  	}
    72  	f := &swarmapi.ListTasksRequest_Filters{
    73  		NamePrefixes: filter.Get("name"),
    74  		IDPrefixes:   filter.Get("id"),
    75  		Labels:       runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
    76  		ServiceIDs:   filter.Get("service"),
    77  		NodeIDs:      filter.Get("node"),
    78  		UpToDate:     len(filter.Get("_up-to-date")) != 0,
    79  		Runtimes:     filter.Get("runtime"),
    80  	}
    81  
    82  	for _, s := range filter.Get("desired-state") {
    83  		if state, ok := swarmapi.TaskState_value[strings.ToUpper(s)]; ok {
    84  			f.DesiredStates = append(f.DesiredStates, swarmapi.TaskState(state))
    85  		} else if s != "" {
    86  			return nil, fmt.Errorf("Invalid desired-state filter: '%s'", s)
    87  		}
    88  	}
    89  
    90  	return f, nil
    91  }
    92  
    93  func newListSecretsFilters(filter filters.Args) (*swarmapi.ListSecretsRequest_Filters, error) {
    94  	accepted := map[string]bool{
    95  		"names": true,
    96  		"name":  true,
    97  		"id":    true,
    98  		"label": true,
    99  	}
   100  	if err := filter.Validate(accepted); err != nil {
   101  		return nil, err
   102  	}
   103  	return &swarmapi.ListSecretsRequest_Filters{
   104  		Names:        filter.Get("names"),
   105  		NamePrefixes: filter.Get("name"),
   106  		IDPrefixes:   filter.Get("id"),
   107  		Labels:       runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
   108  	}, nil
   109  }
   110  
   111  func newListConfigsFilters(filter filters.Args) (*swarmapi.ListConfigsRequest_Filters, error) {
   112  	accepted := map[string]bool{
   113  		"name":  true,
   114  		"id":    true,
   115  		"label": true,
   116  	}
   117  	if err := filter.Validate(accepted); err != nil {
   118  		return nil, err
   119  	}
   120  	return &swarmapi.ListConfigsRequest_Filters{
   121  		NamePrefixes: filter.Get("name"),
   122  		IDPrefixes:   filter.Get("id"),
   123  		Labels:       runconfigopts.ConvertKVStringsToMap(filter.Get("label")),
   124  	}, nil
   125  }