Skip to content

Commit

Permalink
Merge pull request #18 from LLParse/expose-host-labels
Browse files Browse the repository at this point in the history
Expose host labels
  • Loading branch information
ibuildthecloud authored Oct 4, 2016
2 parents 9240cb5 + 54cc867 commit 94c6a3a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd/host.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"bytes"
"strings"

"github.com/rancher/go-rancher/v2"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -49,6 +52,7 @@ type HostsData struct {
Host client.Host
State string
IPAddresses []client.IpAddress
Labels string
}

func getHostState(host *client.Host) string {
Expand All @@ -59,6 +63,24 @@ func getHostState(host *client.Host) string {
return state
}

func getLabels(host *client.Host) string {
var buffer bytes.Buffer
it := 0
for key, value := range host.Labels {
if strings.HasPrefix(key, "io.rancher") {
continue
} else if it > 0 {
buffer.WriteString(",")
}

buffer.WriteString(key)
buffer.WriteString("=")
buffer.WriteString(value.(string))
it++
}
return buffer.String()
}

func hostLs(ctx *cli.Context) error {
c, err := GetClient(ctx)
if err != nil {
Expand Down Expand Up @@ -104,6 +126,7 @@ func hostLs(ctx *cli.Context) error {
{"HOSTNAME", "Host.Hostname"},
{"STATE", "State"},
{"IP", "{{ips .IPAddresses}}"},
{"LABELS", "Labels"},
{"DETAIL", "Host.TransitioningMessage"},
}, ctx)

Expand All @@ -118,6 +141,7 @@ func hostLs(ctx *cli.Context) error {
ID: item.Id,
Host: item,
State: getHostState(&item),
Labels: getLabels(&item),
IPAddresses: ips.Data,
})
}
Expand Down

0 comments on commit 94c6a3a

Please sign in to comment.