Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Update tribe config to use netutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
jcooklin authored and IRCody committed Jun 10, 2016
1 parent 172f518 commit 0c9871f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 36 deletions.
37 changes: 2 additions & 35 deletions mgmt/tribe/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package tribe
import (
"encoding/json"
"fmt"
"net"
"os"
"time"

"github.com/hashicorp/memberlist"
"github.com/intelsdi-x/snap/pkg/netutil"
"github.com/pborman/uuid"
)

Expand Down Expand Up @@ -95,7 +95,7 @@ func GetDefaultConfig() *Config {
return &Config{
Name: getHostname(),
Enable: defaultEnable,
BindAddr: getIP(),
BindAddr: netutil.GetIP(),
BindPort: defaultBindPort,
Seed: defaultSeed,
MemberlistConfig: mlCfg,
Expand Down Expand Up @@ -154,36 +154,3 @@ func getHostname() string {
}
return hostname
}

func getIP() string {
ifaces, err := net.Interfaces()
if err != nil {
logger.WithField("_block", "getIP").Error(err)
return "127.0.0.1"
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
logger.WithField("_block", "getIP").Error(err)
return "127.0.0.1"
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPAddr:
ip = v.IP
case *net.IPNet:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String()
}
}
return "127.0.0.1"
}
3 changes: 2 additions & 1 deletion mgmt/tribe/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/intelsdi-x/snap/pkg/cfgfile"
"github.com/intelsdi-x/snap/pkg/netutil"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -127,7 +128,7 @@ func TestTribeDefaultConfig(t *testing.T) {
So(cfg.Enable, ShouldEqual, false)
})
Convey("BindAddr should be not empty", func() {
So(cfg.BindAddr, ShouldEqual, getIP())
So(cfg.BindAddr, ShouldEqual, netutil.GetIP())
})
Convey("BindPort should be 6000", func() {
So(cfg.BindPort, ShouldEqual, 6000)
Expand Down

0 comments on commit 0c9871f

Please sign in to comment.