Skip to content

Commit

Permalink
Fix issue #150: Modified the behaviour of few "tink hardware cli"
Browse files Browse the repository at this point in the history
Following are the change in this commit
1. `tink hardware mac <mac address>` will provide only relevent information instead of complete hardware json
2. `tink hardware ip <ip address>` will provide only relevent information instead of complete hardware json
3. There will be a flag in both of the above clis (--details) which can be provided to get the complete hardware data
4. `tink hardware all` cli has been renamed to `tink hardware list`
5. The above cli will only list the relevent information (which is 'hardware-id', 'mac address', 'ip address' and 'hostname for now) instead of providing the complete json of all the hardware present in the database.
  • Loading branch information
parauliya committed Jul 7, 2020
1 parent 8ade723 commit 4102699
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 22 deletions.
32 changes: 19 additions & 13 deletions cmd/tink-cli/cmd/hardware/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"

"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
)

// allCmd represents the all command
var allCmd = &cobra.Command{
Use: "all",
Short: "get all known hardware for facility",
// listCmd represents the all command
var listCmd = &cobra.Command{
Use: "list",
Short: "list all known hardware",
Run: func(cmd *cobra.Command, args []string) {
alls, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Hardware-Id", "MAC Address", "IP address", "Hostname"})

list, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})
if err != nil {
log.Fatal(err)
}

var hw *hardware.Hardware
err = nil
for hw, err = alls.Recv(); err == nil && hw != nil; hw, err = alls.Recv() {
b, err := json.Marshal(hw)
if err != nil {
log.Println(err)
for hw, err = list.Recv(); err == nil && hw != nil; hw, err = list.Recv() {
interfaces := hw.GetNetwork().GetInterfaces()
for i := range hw.Network.Interfaces {
t.AppendRow(table.Row{hw.Id, interfaces[i].Dhcp.Mac, interfaces[i].Dhcp.Ip.Address, interfaces[i].Dhcp.Hostname})
}
fmt.Println(string(b))
}
if err != nil && err != io.EOF {
log.Println(err)
} else {
t.Render()
}
},
}

func init() {
SubCommands = append(SubCommands, allCmd)
SubCommands = append(SubCommands, listCmd)
}
7 changes: 7 additions & 0 deletions cmd/tink-cli/cmd/hardware/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hardware
import (
"fmt"

"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
Expand All @@ -23,3 +24,9 @@ func verifyUUIDs(args []string) error {
}
return nil
}

func renderTable(req bool, t table.Writer) {
if req {
t.Render()
}
}
40 changes: 36 additions & 4 deletions cmd/tink-cli/cmd/hardware/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"fmt"
"log"
"net"
"os"

"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
)

var data bool

// ipCmd represents the ip command
var ipCmd = &cobra.Command{
Use: "ip",
Expand All @@ -28,20 +32,48 @@ var ipCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Field Name", "Value"})

for _, ip := range args {
hw, err := client.HardwareClient.ByIP(context.Background(), &hardware.GetRequest{Ip: ip})
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
if hw.GetId() == "" {
log.Fatal("IP address not found in the database ", ip)
}
if !data {
interfaces := hw.GetNetwork().GetInterfaces()
for i := range hw.Network.Interfaces {
if hw.Network.Interfaces[i].Dhcp.Ip.Address == ip {
t.AppendRow(table.Row{"IP Address", interfaces[i].Dhcp.Ip.Address})
t.AppendRow(table.Row{"Hardware-Id", hw.Id})
t.AppendRow(table.Row{"MAC Address", interfaces[i].Dhcp.Mac})
t.AppendRow(table.Row{"Hostname", interfaces[i].Dhcp.Hostname})
}
}
} else {
hwData, err := json.Marshal(hw)
if err != nil {
fmt.Errorf("Failed to marshal hardware data", err)
} else {
fmt.Println(string(hwData))
}
}
fmt.Println(string(b))
}
renderTable(!data, t)
},
}

func addIPFlags() {
flags := ipCmd.Flags()
flags.BoolVarP(&data, "details", "d", false, "Provide the complete hardware details in json format")
}

func init() {
addIPFlags()
SubCommands = append(SubCommands, ipCmd)
}
37 changes: 33 additions & 4 deletions cmd/tink-cli/cmd/hardware/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"fmt"
"log"
"net"
"os"

"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
Expand All @@ -28,20 +30,47 @@ var macCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Field Name", "Value"})

for _, mac := range args {
hw, err := client.HardwareClient.ByMAC(context.Background(), &hardware.GetRequest{Mac: mac})
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
if hw.GetId() == "" {
log.Fatal("Mac address not found in the database ", mac)
}
if !data {
interfaces := hw.GetNetwork().GetInterfaces()
for i := range hw.Network.Interfaces {
if hw.Network.Interfaces[i].Dhcp.Mac == mac {
t.AppendRow(table.Row{"MAC Address", interfaces[i].Dhcp.Mac})
t.AppendRow(table.Row{"Hardware-Id", hw.Id})
t.AppendRow(table.Row{"IP Address", interfaces[i].Dhcp.Ip.Address})
t.AppendRow(table.Row{"Hostname", interfaces[i].Dhcp.Hostname})
}
}
} else {
hwData, err := json.Marshal(hw)
if err != nil {
fmt.Errorf("Failed to marshal hardware data", err)
} else {
fmt.Println(string(hwData))
}
}
fmt.Println(string(b))
}
renderTable(!data, t)
},
}

func addMacFlags() {
flags := macCmd.Flags()
flags.BoolVarP(&data, "details", "d", false, "Provide the complete hardware details in json format")
}

func init() {
addMacFlags()
SubCommands = append(SubCommands, macCmd)
}
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
Short: "push new hardware to tinkerbell",
Short: "push new hardware to tink",
Example: `cat /tmp/data.json | tink hardware push
tink hardware push --file /tmp/data.json`,
PreRunE: func(c *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func SetupMetrics(facility string, logger log.Logger) {
{"method": "Ingest", "op": ""},
{"method": "Watch", "op": "get"},
{"method": "Watch", "op": "push"},
{"method": "Delete", "op": "delete"},
}
initCounterLabels(CacheErrors, labels)
initGaugeLabels(CacheInFlight, labels)
Expand Down

0 comments on commit 4102699

Please sign in to comment.