Skip to content

Commit

Permalink
nighthawk package init
Browse files Browse the repository at this point in the history
Signed-off-by: kumarabd <[email protected]>
  • Loading branch information
kumarabd committed May 6, 2021
1 parent b81dc18 commit fb7e794
Show file tree
Hide file tree
Showing 10 changed files with 4,651 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
strategy:
max-parallel: 10
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, macos-latest]
architecture: [amd64]
distribution: [client, server, test_server, nighthawk_output_transform]
runs-on: ${{ matrix.os }}
Expand Down
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.

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

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

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

var (
ErrInvalidEndpoint = errors.NewDefault(ErrInvalidEndpointCode, "Endpoint not reachable")
)

func ErrGRPCDial(err error) error {
return errors.NewDefault(ErrGRPCDialCode, "Error creating nighthawk client", err.Error())
}
54 changes: 54 additions & 0 deletions pkg/client/nighthawk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 dial_options []grpc.DialOption
dial_options = append(dial_options, grpc.WithInsecure())

conn, err := grpc.Dial(fmt.Sprintf("%s:%d", opts.ServerHost, opts.ServerPort), dial_options...)
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
}
43 changes: 43 additions & 0 deletions pkg/client/nighthawk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 {
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 fb7e794

Please sign in to comment.