Skip to content

Commit 0bb5b5b

Browse files
authored
Merge pull request #102 from layer5io/kumarabd/feature/getnighthawk
nighthawk package init
2 parents 85dc94b + 3b4805b commit 0bb5b5b

File tree

9 files changed

+4665
-5
lines changed

9 files changed

+4665
-5
lines changed

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ go 1.14
44

55
require (
66
fortio.org/fortio v1.6.8
7-
github.com/kr/text v0.2.0 // indirect
8-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
7+
github.com/envoyproxy/go-control-plane v0.9.8
8+
github.com/envoyproxy/protoc-gen-validate v0.1.0
9+
github.com/golang/protobuf v1.4.2
10+
github.com/layer5io/meshkit v0.2.7
911
github.com/pkg/errors v0.9.1
10-
github.com/sirupsen/logrus v1.6.0
11-
github.com/stretchr/testify v1.6.1 // indirect
12-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
12+
github.com/sirupsen/logrus v1.7.0
13+
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c
14+
google.golang.org/grpc v1.31.0
15+
google.golang.org/protobuf v1.25.0
1316
)

go.sum

Lines changed: 820 additions & 0 deletions
Large diffs are not rendered by default.

pkg/client/error.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nighthawk
2+
3+
import (
4+
"github.com/layer5io/meshkit/errors"
5+
)
6+
7+
const (
8+
ErrGRPCDialCode = "1000"
9+
ErrInvalidEndpointCode = "1001"
10+
ErrResponseNilCode = "1002"
11+
)
12+
13+
var (
14+
ErrInvalidEndpoint = errors.NewDefault(ErrInvalidEndpointCode, "Endpoint is unavailable or endpoint is unreachable")
15+
ErrResponseNil = errors.NewDefault(ErrResponseNilCode, "Response is nil from the generator")
16+
)
17+
18+
func ErrGRPCDial(err error) error {
19+
return errors.NewDefault(ErrGRPCDialCode, "Error creating nighthawk client", err.Error())
20+
}

pkg/client/nighthawk.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package nighthawk
2+
3+
import (
4+
"fmt"
5+
6+
"google.golang.org/grpc"
7+
8+
"github.com/layer5io/meshkit/utils"
9+
nighthawk_client "github.com/layer5io/nighthawk-go/pkg/proto"
10+
)
11+
12+
// Options argument for customizing the client
13+
type Options struct {
14+
ServerHost string
15+
ServerPort int32
16+
}
17+
18+
// Client holds the nighthawk client information
19+
type Client struct {
20+
Handler nighthawk_client.NighthawkServiceClient
21+
connection *grpc.ClientConn
22+
}
23+
24+
// New creates a new instance of the nighthawk client connection
25+
func New(opts Options) (*Client, error) {
26+
if !utils.TcpCheck(&utils.HostPort{
27+
Address: opts.ServerHost,
28+
Port: opts.ServerPort,
29+
}, nil) {
30+
return nil, ErrInvalidEndpoint
31+
}
32+
33+
var dialOptions []grpc.DialOption
34+
dialOptions = append(dialOptions, grpc.WithInsecure())
35+
36+
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", opts.ServerHost, opts.ServerPort), dialOptions...)
37+
if err != nil {
38+
return nil, ErrGRPCDial(err)
39+
}
40+
41+
return &Client{
42+
Handler: nighthawk_client.NewNighthawkServiceClient(conn),
43+
connection: conn,
44+
}, nil
45+
}
46+
47+
// Close closes the client connection
48+
func (c *Client) Close() error {
49+
if c.connection != nil {
50+
return c.connection.Close()
51+
}
52+
return nil
53+
}

pkg/client/nighthawk_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package nighthawk
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNew(t *testing.T) {
9+
type args struct {
10+
opts Options
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want *Client
16+
wantErr bool
17+
}{
18+
// TODO: Add test cases.
19+
{
20+
name: "In case of blank URL",
21+
args: args{
22+
opts: Options{
23+
ServerHost: "",
24+
ServerPort: 0,
25+
},
26+
},
27+
want: nil,
28+
wantErr: true,
29+
},
30+
}
31+
for _, tt := range tests {
32+
tt := tt
33+
t.Run(tt.name, func(t *testing.T) {
34+
got, err := New(tt.args.opts)
35+
if (err != nil) != tt.wantErr {
36+
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
37+
return
38+
}
39+
if !reflect.DeepEqual(got, tt.want) {
40+
t.Errorf("New() = %v, want %v", got, tt.want)
41+
}
42+
})
43+
}
44+
}

0 commit comments

Comments
 (0)