Skip to content

Commit

Permalink
Unify printers.PrintDevices to use the API model
Browse files Browse the repository at this point in the history
  • Loading branch information
ernoaapa committed Feb 12, 2018
1 parent 52fd3de commit ba63ec5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 34 deletions.
3 changes: 2 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ernoaapa/eliot/pkg/discovery"
"github.com/ernoaapa/eliot/pkg/printers"
"github.com/ernoaapa/eliot/pkg/sync"
"github.com/ernoaapa/eliot/pkg/utils"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -139,7 +140,7 @@ func GetConfigProvider(clicontext *cli.Context) *config.Provider {
for _, device := range devices {
endpoints = append(endpoints, config.Endpoint{
Name: device.Hostname,
URL: device.GetPrimaryEndpoint(),
URL: utils.GetFirst(device.Addresses, ""),
})
}
provider.OverrideEndpoints(endpoints)
Expand Down
10 changes: 5 additions & 5 deletions pkg/discovery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"time"

"github.com/ernoaapa/eliot/pkg/model"
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
"github.com/grandcat/zeroconf"
"github.com/pkg/errors"
)

// Devices return list of DeviceInfos synchronously with given timeout
func Devices(timeout time.Duration) (devices []model.DeviceInfo, err error) {
results := make(chan model.DeviceInfo)
func Devices(timeout time.Duration) (devices []*device.Info, err error) {
results := make(chan *device.Info)
defer close(results)

go func() {
Expand All @@ -29,7 +29,7 @@ func Devices(timeout time.Duration) (devices []model.DeviceInfo, err error) {
}

// DevicesAsync search for devices in network asynchronously for given timeout
func DevicesAsync(results chan<- model.DeviceInfo, timeout time.Duration) error {
func DevicesAsync(results chan<- *device.Info, timeout time.Duration) error {
resolver, err := zeroconf.NewResolver(nil)
if err != nil {
return errors.Wrapf(err, "Failed to initialize new zeroconf resolver")
Expand All @@ -38,7 +38,7 @@ func DevicesAsync(results chan<- model.DeviceInfo, timeout time.Duration) error
entries := make(chan *zeroconf.ServiceEntry)
go func(entries <-chan *zeroconf.ServiceEntry) {
for entry := range entries {
results <- MapToInternalModel(entry)
results <- MapToAPIModel(entry)
}
}(entries)

Expand Down
19 changes: 13 additions & 6 deletions pkg/discovery/mapping.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package discovery

import (
"net"
"strings"

"github.com/ernoaapa/eliot/pkg/model"
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
"github.com/grandcat/zeroconf"
)

// MapToInternalModel takes zeroconf entry and maps it to internal DeviceInfo model
func MapToInternalModel(entry *zeroconf.ServiceEntry) model.DeviceInfo {
func MapToAPIModel(entry *zeroconf.ServiceEntry) *device.Info {
version := "unknown"

for _, val := range entry.Text {
Expand All @@ -18,10 +18,17 @@ func MapToInternalModel(entry *zeroconf.ServiceEntry) model.DeviceInfo {
}
}

return model.DeviceInfo{
return &device.Info{
Hostname: entry.HostName,
Addresses: append(entry.AddrIPv4, entry.AddrIPv6...),
GrpcPort: entry.Port,
Addresses: addressesToString(append(entry.AddrIPv4, entry.AddrIPv6...)),
GrpcPort: int64(entry.Port),
Version: version,
}
}

func addressesToString(addresses []net.IP) (result []string) {
for _, ip := range addresses {
result = append(result, ip.String())
}
return result
}
8 changes: 6 additions & 2 deletions pkg/discovery/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMapToInternalModel(t *testing.T) {
result := MapToInternalModel(&zeroconf.ServiceEntry{
result := MapToAPIModel(&zeroconf.ServiceEntry{
HostName: "hostname",
AddrIPv4: []net.IP{net.IPv4zero},
AddrIPv6: []net.IP{net.IPv6loopback},
Expand All @@ -18,5 +18,9 @@ func TestMapToInternalModel(t *testing.T) {

assert.Equal(t, "hostname", result.Hostname)
assert.Equal(t, "1.2.3-abcd", result.Version)
assert.Equal(t, []net.IP{net.IPv4zero, net.IPv6loopback}, result.Addresses)
assert.Equal(t, addressesToString([]net.IP{net.IPv4zero, net.IPv6loopback}), result.Addresses)
}

func TestAddressesToString(t *testing.T) {
assert.Equal(t, []string{"0.0.0.0", "::1"}, addressesToString([]net.IP{net.IPv4zero, net.IPv6loopback}))
}
9 changes: 0 additions & 9 deletions pkg/model/device.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"fmt"
"net"
)

Expand Down Expand Up @@ -49,11 +48,3 @@ type DeviceState struct {
type PodState struct {
ID string `validate:"required,gt=0"`
}

// GetPrimaryEndpoint return primary GRPC endpoint address
func (d DeviceInfo) GetPrimaryEndpoint() string {
if len(d.Addresses) == 0 {
return ""
}
return fmt.Sprintf("%s:%d", d.Addresses[0], d.GrpcPort)
}
6 changes: 3 additions & 3 deletions pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/ernoaapa/eliot/pkg/printers/humanreadable"
"github.com/ernoaapa/eliot/pkg/utils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -79,15 +79,15 @@ func getKeys(source map[string]int) (result []string) {
}

// PrintDevices writes list of Devices in human readable table format to the writer
func (p *HumanReadablePrinter) PrintDevices(devices []model.DeviceInfo, writer io.Writer) error {
func (p *HumanReadablePrinter) PrintDevices(devices []*device.Info, writer io.Writer) error {
if len(devices) == 0 {
fmt.Fprintf(writer, "\n\t(No devices)\n\n")
return nil
}
fmt.Fprintln(writer, "\nHOSTNAME\tENDPOINT\tVERSION")

for _, device := range devices {
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, device.GetPrimaryEndpoint(), device.Version)
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, utils.GetFirst(device.Addresses, ""), device.Version)
if err != nil {
return errors.Wrapf(err, "Error while writing device row")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/printers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
)

// ResourcePrinter is an interface that knows how to print runtime objects.
type ResourcePrinter interface {
PrintPods([]*pods.Pod, io.Writer) error
PrintDevices([]model.DeviceInfo, io.Writer) error
PrintDevices([]*device.Info, io.Writer) error
PrintDevice(*device.Info, io.Writer) error
PrintPod(*pods.Pod, io.Writer) error
PrintConfig(*config.Config, io.Writer) error
Expand Down
7 changes: 3 additions & 4 deletions pkg/printers/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -57,11 +56,11 @@ func testYamlPrintPods(t *testing.T, printer ResourcePrinter) {
func testYamlPrintDevices(t *testing.T, printer ResourcePrinter) {
var buffer bytes.Buffer

data := []model.DeviceInfo{
data := []*device.Info{
{
Hostname: "foobar",
Labels: map[string]string{
"env": "test",
Labels: []*device.Label{
{Key: "env", Value: "test"},
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/printers/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
Expand All @@ -29,7 +28,7 @@ func (p *YamlPrinter) PrintPods(pods []*pods.Pod, w io.Writer) error {
}

// PrintDevices takes list of devices and prints to Writer in YAML format
func (p *YamlPrinter) PrintDevices(devices []model.DeviceInfo, w io.Writer) error {
func (p *YamlPrinter) PrintDevices(devices []*device.Info, w io.Writer) error {
if err := writeAsYml(devices, w); err != nil {
return errors.Wrap(err, "Failed to write devices yaml")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ func RotateRBy(a *[]string, i int) {
x, b := (*a)[:(len(*a)-i)], (*a)[(len(*a)-i):]
*a = append(b, x...)
}

// GetFirst return first element from the list or default if list is empty
func GetFirst(l []string, d string) string {
if len(l) == 0 {
return d
}
return l[0]
}

0 comments on commit ba63ec5

Please sign in to comment.