Skip to content

Commit

Permalink
Merge pull request #102 from layer5io/kumarabd/feature/getnighthawk
Browse files Browse the repository at this point in the history
nighthawk package init
  • Loading branch information
kumarabd authored May 10, 2021
2 parents 85dc94b + 3b4805b commit 0bb5b5b
Show file tree
Hide file tree
Showing 9 changed files with 4,665 additions and 5 deletions.
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.14

require (
fortio.org/fortio v1.6.8
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/envoyproxy/go-control-plane v0.9.8
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/golang/protobuf v1.4.2
github.com/layer5io/meshkit v0.2.7
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
github.com/sirupsen/logrus v1.7.0
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c
google.golang.org/grpc v1.31.0
google.golang.org/protobuf v1.25.0
)
820 changes: 820 additions & 0 deletions go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pkg/client/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nighthawk

import (
"github.com/layer5io/meshkit/errors"
)

const (
ErrGRPCDialCode = "1000"
ErrInvalidEndpointCode = "1001"
ErrResponseNilCode = "1002"
)

var (
ErrInvalidEndpoint = errors.NewDefault(ErrInvalidEndpointCode, "Endpoint is unavailable or endpoint is unreachable")
ErrResponseNil = errors.NewDefault(ErrResponseNilCode, "Response is nil from the generator")
)

func ErrGRPCDial(err error) error {
return errors.NewDefault(ErrGRPCDialCode, "Error creating nighthawk client", err.Error())
}
53 changes: 53 additions & 0 deletions pkg/client/nighthawk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nighthawk

import (
"fmt"

"google.golang.org/grpc"

"github.com/layer5io/meshkit/utils"
nighthawk_client "github.com/layer5io/nighthawk-go/pkg/proto"
)

// Options argument for customizing the client
type Options struct {
ServerHost string
ServerPort int32
}

// Client holds the nighthawk client information
type Client struct {
Handler nighthawk_client.NighthawkServiceClient
connection *grpc.ClientConn
}

// New creates a new instance of the nighthawk client connection
func New(opts Options) (*Client, error) {
if !utils.TcpCheck(&utils.HostPort{
Address: opts.ServerHost,
Port: opts.ServerPort,
}, nil) {
return nil, ErrInvalidEndpoint
}

var dialOptions []grpc.DialOption
dialOptions = append(dialOptions, grpc.WithInsecure())

conn, err := grpc.Dial(fmt.Sprintf("%s:%d", opts.ServerHost, opts.ServerPort), dialOptions...)
if err != nil {
return nil, ErrGRPCDial(err)
}

return &Client{
Handler: nighthawk_client.NewNighthawkServiceClient(conn),
connection: conn,
}, nil
}

// Close closes the client connection
func (c *Client) Close() error {
if c.connection != nil {
return c.connection.Close()
}
return nil
}
44 changes: 44 additions & 0 deletions pkg/client/nighthawk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nighthawk

import (
"reflect"
"testing"
)

func TestNew(t *testing.T) {
type args struct {
opts Options
}
tests := []struct {
name string
args args
want *Client
wantErr bool
}{
// TODO: Add test cases.
{
name: "In case of blank URL",
args: args{
opts: Options{
ServerHost: "",
ServerPort: 0,
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 0bb5b5b

Please sign in to comment.