From 1c3960ffdacff4130bd4fb8f8b95e85dc221bc6a Mon Sep 17 00:00:00 2001 From: parauliya Date: Fri, 3 Jul 2020 10:56:35 +0530 Subject: [PATCH] Fix issue #150: Modified the behaviour of few "tink hardware cli" Following are the change in this commit 1. `tink hardware mac ` will provide only relevent information instead of complete hardware json 2. `tink hardware ip ` 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. --- cmd/tink-cli/cmd/hardware/all.go | 31 ++++++++++++++++----------- cmd/tink-cli/cmd/hardware/commands.go | 29 +++++++++++++++++++++++++ cmd/tink-cli/cmd/hardware/ip.go | 16 +++++++++----- cmd/tink-cli/cmd/hardware/mac.go | 14 +++++++----- cmd/tink-cli/cmd/hardware/push.go | 2 +- metrics/metrics.go | 1 + 6 files changed, 69 insertions(+), 24 deletions(-) diff --git a/cmd/tink-cli/cmd/hardware/all.go b/cmd/tink-cli/cmd/hardware/all.go index 1e8a1eef1..0f95c7997 100644 --- a/cmd/tink-cli/cmd/hardware/all.go +++ b/cmd/tink-cli/cmd/hardware/all.go @@ -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) } diff --git a/cmd/tink-cli/cmd/hardware/commands.go b/cmd/tink-cli/cmd/hardware/commands.go index 40fbf9d11..7cdb4b076 100644 --- a/cmd/tink-cli/cmd/hardware/commands.go +++ b/cmd/tink-cli/cmd/hardware/commands.go @@ -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 @@ -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)) + } + } +} diff --git a/cmd/tink-cli/cmd/hardware/ip.go b/cmd/tink-cli/cmd/hardware/ip.go index a63afbd4f..9d66341a6 100644 --- a/cmd/tink-cli/cmd/hardware/ip.go +++ b/cmd/tink-cli/cmd/hardware/ip.go @@ -4,7 +4,6 @@ package hardware import ( "context" - "encoding/json" "fmt" "log" "net" @@ -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", @@ -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) } diff --git a/cmd/tink-cli/cmd/hardware/mac.go b/cmd/tink-cli/cmd/hardware/mac.go index 43c6ee317..82ad856c2 100644 --- a/cmd/tink-cli/cmd/hardware/mac.go +++ b/cmd/tink-cli/cmd/hardware/mac.go @@ -4,7 +4,6 @@ package hardware import ( "context" - "encoding/json" "fmt" "log" "net" @@ -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) } diff --git a/cmd/tink-cli/cmd/hardware/push.go b/cmd/tink-cli/cmd/hardware/push.go index 4d70bfd75..2fbd24d0f 100644 --- a/cmd/tink-cli/cmd/hardware/push.go +++ b/cmd/tink-cli/cmd/hardware/push.go @@ -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 { diff --git a/metrics/metrics.go b/metrics/metrics.go index 246bf9895..e3426b039 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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)