Skip to content

Commit 7b38095

Browse files
committed
Fix bug in URL builder for NodesInfo API
Close #1078
1 parent 95d4e85 commit 7b38095

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

nodes_info.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ type NodesInfoService struct {
3030
func NewNodesInfoService(client *Client) *NodesInfoService {
3131
return &NodesInfoService{
3232
client: client,
33-
nodeId: []string{"_all"},
34-
metric: []string{"_all"},
3533
}
3634
}
3735

@@ -71,10 +69,24 @@ func (s *NodesInfoService) Pretty(pretty bool) *NodesInfoService {
7169

7270
// buildURL builds the URL for the operation.
7371
func (s *NodesInfoService) buildURL() (string, url.Values, error) {
72+
var nodeId, metric string
73+
74+
if len(s.nodeId) > 0 {
75+
nodeId = strings.Join(s.nodeId, ",")
76+
} else {
77+
nodeId = "_all"
78+
}
79+
80+
if len(s.metric) > 0 {
81+
metric = strings.Join(s.metric, ",")
82+
} else {
83+
metric = "_all"
84+
}
85+
7486
// Build URL
7587
path, err := uritemplates.Expand("/_nodes/{node_id}/{metric}", map[string]string{
76-
"node_id": strings.Join(s.nodeId, ","),
77-
"metric": strings.Join(s.metric, ","),
88+
"node_id": nodeId,
89+
"metric": metric,
7890
})
7991
if err != nil {
8092
return "", url.Values{}, err

nodes_info_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,51 @@ import (
99
"testing"
1010
)
1111

12+
func TestNodesInfoBuildURL(t *testing.T) {
13+
client := setupTestClientAndCreateIndex(t)
14+
15+
tests := []struct {
16+
NodeIDs []string
17+
Metrics []string
18+
Expected string
19+
}{
20+
{
21+
nil,
22+
nil,
23+
"/_nodes/_all/_all",
24+
},
25+
{
26+
[]string{},
27+
[]string{},
28+
"/_nodes/_all/_all",
29+
},
30+
{
31+
[]string{"node1"},
32+
[]string{},
33+
"/_nodes/node1/_all",
34+
},
35+
{
36+
[]string{"node1", "node2"},
37+
nil,
38+
"/_nodes/node1%2Cnode2/_all",
39+
},
40+
{
41+
[]string{"node1", "node2"},
42+
[]string{"metric1", "metric2"},
43+
"/_nodes/node1%2Cnode2/metric1%2Cmetric2",
44+
},
45+
}
46+
47+
for i, test := range tests {
48+
path, _, err := client.NodesInfo().NodeId(test.NodeIDs...).Metric(test.Metrics...).buildURL()
49+
if err != nil {
50+
t.Fatalf("case #%d: %v", i+1, err)
51+
}
52+
if path != test.Expected {
53+
t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
54+
}
55+
}
56+
}
1257
func TestNodesInfo(t *testing.T) {
1358
client, err := NewClient()
1459
if err != nil {

0 commit comments

Comments
 (0)