generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kumarabd <[email protected]>
- Loading branch information
Showing
10 changed files
with
4,651 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.