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.
Merge pull request #102 from layer5io/kumarabd/feature/getnighthawk
nighthawk package init
- Loading branch information
Showing
9 changed files
with
4,665 additions
and
5 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
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()) | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.