Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of member cli: add -filter expression to flags into release/1.14.x #18274

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/18223.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
cli: `consul members` command uses `-filter` expression to filter members based on bexpr.
```
15 changes: 15 additions & 0 deletions agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,21 @@ func (s *HTTPHandlers) AgentMembers(resp http.ResponseWriter, req *http.Request)
}
}

// filter the members by parsed filter expression
var filterExpression string
s.parseFilter(req, &filterExpression)
if filterExpression != "" {
filter, err := bexpr.CreateFilter(filterExpression, nil, members)
if err != nil {
return nil, err
}
raw, err := filter.Execute(members)
if err != nil {
return nil, err
}
members = raw.([]serf.Member)
}

total := len(members)
if err := s.agent.filterMembers(token, &members); err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ type MembersOpts struct {
// Segment is the LAN segment to show members for. Setting this to the
// AllSegments value above will show members in all segments.
Segment string

Filter string
}

// AgentServiceRegistration is used to register a new service
Expand Down Expand Up @@ -762,6 +764,10 @@ func (a *Agent) MembersOpts(opts MembersOpts) ([]*AgentMember, error) {
r.params.Set("wan", "1")
}

if opts.Filter != "" {
r.params.Set("filter", opts.Filter)
}

_, resp, err := a.c.doRequest(r)
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions api/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ func TestAPI_AgentMembersOpts(t *testing.T) {
if len(members) != 2 {
t.Fatalf("bad: %v", members)
}

members, err = agent.MembersOpts(MembersOpts{
WAN: true,
Filter: `Tags["dc"] == dc2`,
})
if err != nil {
t.Fatalf("err: %v", err)
}

require.Equal(t, 1, len(members))
}

func TestAPI_AgentMembers(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions command/members/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type cmd struct {
wan bool
statusFilter string
segment string
filter string
}

func New(ui cli.Ui) *cmd {
Expand All @@ -51,6 +52,7 @@ func (c *cmd) init() {
c.flags.StringVar(&c.segment, "segment", consulapi.AllSegments,
"(Enterprise-only) If provided, output is filtered to only nodes in"+
"the given segment.")
c.flags.StringVar(&c.filter, "filter", "", "Filter to use with the request")

c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
Expand Down Expand Up @@ -80,6 +82,7 @@ func (c *cmd) Run(args []string) int {
opts := consulapi.MembersOpts{
Segment: c.segment,
WAN: c.wan,
Filter: c.filter,
}
members, err := client.Agent().MembersOpts(opts)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions website/content/commands/members.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Usage: `consul members [options]`
in the WAN gossip pool. These are generally all the server nodes in
each datacenter.

- `-filter=<filter>` - Expression to use for filtering the results,
e.g., `-filter='Tags["dc"] == dc2'`.
See the [`/catalog/nodes` API documentation](/consul/api-docs/catalog#filtering) for a
description of what is filterable.


#### Enterprise Options

@include 'http_api_partition_options.mdx'
Expand Down