Skip to content

Commit

Permalink
node-list: handle pool filter in node list using pool field
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Sep 1, 2017
1 parent 017c006 commit 546f277
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
66 changes: 31 additions & 35 deletions tsuru/admin/node.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tsuru/gnuflag"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/healer"
"github.com/tsuru/tsuru/iaas"
"github.com/tsuru/tsuru/net"
"github.com/tsuru/tsuru/provision"
)
Expand Down Expand Up @@ -309,45 +310,42 @@ func (c *ListNodesCmd) Run(ctx *cmd.Context, client *cmd.Client) error {
ctx.Stdout.Write(t.Bytes())
return nil
}
var result map[string]interface{}
var result struct {
Nodes []provision.NodeSpec
Machines []iaas.Machine
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return err
}
machineMap := map[string]map[string]interface{}{}
if result["machines"] != nil {
machines := result["machines"].([]interface{})
for _, m := range machines {
machine := m.(map[string]interface{})
machineMap[machine["Address"].(string)] = m.(map[string]interface{})
machineMap := map[string]iaas.Machine{}
if len(result.Machines) > 0 {
for _, m := range result.Machines {
machineMap[m.Address] = m
}
}
var nodes []map[string]interface{}
if result["nodes"] != nil {
nodes = c.filterNodes(result["nodes"].([]interface{}))
var nodes []provision.NodeSpec
if len(result.Nodes) > 0 {
nodes = c.filterNodes(result.Nodes)
}
if c.simplified {
for _, node := range nodes {
fmt.Fprintln(ctx.Stdout, node["Address"].(string))
fmt.Fprintln(ctx.Stdout, node.Address)
}
return nil
}
for _, node := range nodes {
addr := node["Address"].(string)
status := node["Status"].(string)
addr := node.Address
status := node.Status
result := []string{}
metadataField, _ := node["Metadata"]
if metadataField != nil {
metadata := metadataField.(map[string]interface{})
for key, value := range metadata {
result = append(result, fmt.Sprintf("%s=%s", key, value.(string)))
}
for key, value := range node.Metadata {
result = append(result, fmt.Sprintf("%s=%s", key, value))
}
sort.Strings(result)
m, ok := machineMap[net.URLToHost(addr)]
var iaasID string
if ok {
iaasID = m["Id"].(string)
iaasID = m.Id
}
t.AddRow(cmd.Row([]string{addr, iaasID, status, strings.Join(result, "\n")}))
}
Expand All @@ -356,29 +354,27 @@ func (c *ListNodesCmd) Run(ctx *cmd.Context, client *cmd.Client) error {
return nil
}

func (c *ListNodesCmd) filterNodes(nodes []interface{}) []map[string]interface{} {
filteredNodes := make([]map[string]interface{}, 0)
func (c *ListNodesCmd) filterNodes(nodes []provision.NodeSpec) []provision.NodeSpec {
filteredNodes := make([]provision.NodeSpec, 0)
for _, n := range nodes {
node := n.(map[string]interface{})
if c.nodeMetadataMatchesFilters(node) {
filteredNodes = append(filteredNodes, node)
if c.nodeMetadataMatchesFilters(n) {
filteredNodes = append(filteredNodes, n)
}
}
return filteredNodes
}

func (c *ListNodesCmd) nodeMetadataMatchesFilters(node map[string]interface{}) bool {
metadataField, _ := node["Metadata"]
if c.filter != nil && metadataField == nil {
return false
}
if metadataField != nil {
metadata := metadataField.(map[string]interface{})
for key, value := range c.filter {
metaVal, _ := metadata[key]
if metaVal != value {
func (c *ListNodesCmd) nodeMetadataMatchesFilters(node provision.NodeSpec) bool {
for key, value := range c.filter {
if key == provision.PoolMetadataName {
if value != node.Pool {
return false
}
continue
}
metaVal, _ := node.Metadata[key]
if metaVal != value {
return false
}
}
return true
Expand Down
31 changes: 31 additions & 0 deletions tsuru/admin/node_test.go
Expand Up @@ -255,6 +255,37 @@ func (s *S) TestListNodesCmdRunWithFilters(c *check.C) {
c.Assert(buf.String(), check.Equals, expected)
}

func (s *S) TestListNodesCmdRunWithPoolFilter(c *check.C) {
var buf bytes.Buffer
context := cmd.Context{Args: []string{}, Stdout: &buf}
trans := &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Message: `{
"machines": [{"Id": "m-id-1", "Address": "localhost2"}],
"nodes": [
{"Address": "http://localhost1:8080", "Status": "disabled", "Pool": "pool1", "Metadata": {"meta1": "foo", "meta2": "bar"}},
{"Address": "http://localhost2:8089", "Status": "disabled", "Pool": "pool2"},
{"Address": "http://localhost2:9090", "Status": "disabled", "Pool": "pool3", "Metadata": {"meta1": "foo"}}
]
}`, Status: http.StatusOK},
CondFunc: func(req *http.Request) bool {
return req.URL.Path == "/1.2/node"
},
}
manager := cmd.Manager{}
client := cmd.NewClient(&http.Client{Transport: trans}, nil, &manager)
cmd := ListNodesCmd{}
cmd.Flags().Parse(true, []string{"--filter", "pool=pool2"})
err := cmd.Run(&context, client)
c.Assert(err, check.IsNil)
expected := `+------------------------+---------+----------+----------+
| Address | IaaS ID | Status | Metadata |
+------------------------+---------+----------+----------+
| http://localhost2:8089 | m-id-1 | disabled | |
+------------------------+---------+----------+----------+
`
c.Assert(buf.String(), check.Equals, expected)
}

func (s *S) TestListNodesCmdRunEmptyAll(c *check.C) {
var buf bytes.Buffer
context := cmd.Context{Args: []string{}, Stdout: &buf}
Expand Down

0 comments on commit 546f277

Please sign in to comment.