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

Commit

Permalink
Add netutil and rpcutil to pkg directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jcooklin authored and IRCody committed Jun 10, 2016
1 parent af2505f commit 172f518
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/netutil/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package netutil

import "net"

// GetIP returns the first non-loopback ipv4 interface. The loopback interface
// is returned if no other interface is present.
func GetIP() string {
ifaces, err := net.Interfaces()
if err != nil {
return "127.0.0.1"
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
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"
}
40 changes: 40 additions & 0 deletions pkg/netutil/net_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package netutil

import (
"net"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestNetUtil(t *testing.T) {
Convey("Get the first non-loopback ipv4 interface", t, func() {
ip := GetIP()
ifaces, err := net.Interfaces()
So(err, ShouldBeNil)
if len(ifaces) > 1 {
So(ip, ShouldNotResemble, "127.0.0.1")
} else {
So(ip, ShouldResemble, "127.0.0.1")
}
})
}
41 changes: 41 additions & 0 deletions pkg/rpcutil/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rpcutil

import (
"fmt"
"time"

"google.golang.org/grpc"
)

// GetClientConnection returns a grcp.ClientConn that is unsecured
// TODO: Add TLS security to connection
func GetClientConnection(addr string, port int) (*grpc.ClientConn, error) {
grpcDialOpts := []grpc.DialOption{
grpc.WithTimeout(2 * time.Second),
}
grpcDialOpts = append(grpcDialOpts, grpc.WithInsecure())
conn, err := grpc.Dial(fmt.Sprintf("%v:%v", addr, port), grpcDialOpts...)
if err != nil {
return nil, err
}
return conn, nil
}
36 changes: 36 additions & 0 deletions pkg/rpcutil/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rpcutil

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestRpcUtil(t *testing.T) {
Convey("Get a client connection", t, func() {
conn, err := GetClientConnection("127.0.0.1", 8183)
Convey("Provided a valid address, port", func() {
So(err, ShouldBeNil)
So(conn, ShouldNotBeNil)
})
})
}

0 comments on commit 172f518

Please sign in to comment.