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 9, 2020
1 parent 9973940 commit 1c3960f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
31 changes: 18 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,46 @@ 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{"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() {
for _, iface := range hw.GetNetwork().GetInterfaces() {
t.AppendRow(table.Row{hw.Id, iface.Dhcp.Mac, iface.Dhcp.Ip.Address, iface.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)
}
29 changes: 29 additions & 0 deletions cmd/tink-cli/cmd/hardware/commands.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package hardware

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

"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/protos/hardware"
)

// SubCommands holds the sub commands for template command
Expand All @@ -23,3 +28,27 @@ func verifyUUIDs(args []string) error {
}
return nil
}

func printOutput(data bool, hw *hardware.Hardware, input string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Field Name", "Value"})
if !data {
for _, iface := range hw.GetNetwork().GetInterfaces() {
if iface.Dhcp.Ip.Address == input || iface.Dhcp.Mac == input {
t.AppendRow(table.Row{"ID", hw.Id})
t.AppendRow(table.Row{"MAC Address", iface.Dhcp.Mac})
t.AppendRow(table.Row{"IP Address", iface.Dhcp.Ip.Address})
t.AppendRow(table.Row{"Hostname", iface.Dhcp.Hostname})
}
}
t.Render()
} else {
hwData, err := json.Marshal(hw)
if err != nil {
log.Println("Failed to marshal hardware data", err)
} else {
log.Println(string(hwData))
}
}
}
16 changes: 11 additions & 5 deletions cmd/tink-cli/cmd/hardware/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/tinkerbell/tink/protos/hardware"
)

var data bool

// ipCmd represents the ip command
var ipCmd = &cobra.Command{
Use: "ip",
Expand All @@ -33,15 +34,20 @@ var ipCmd = &cobra.Command{
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)
}
fmt.Println(string(b))
printOutput(data, hw, ip)
}
},
}

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)
}
14 changes: 9 additions & 5 deletions cmd/tink-cli/cmd/hardware/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -33,15 +32,20 @@ var macCmd = &cobra.Command{
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)
}
fmt.Println(string(b))
printOutput(data, hw, mac)
}
},
}

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 1c3960f

Please sign in to comment.