From 6d1c3aa5b234be169a4d8f18c648e8a1ea7e44df Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Thu, 28 Nov 2024 18:53:03 +1000 Subject: [PATCH] add basic client TCPConnector to directly connect to a gRPC endpoint --- internal/cli/client/connector/tcp.go | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 internal/cli/client/connector/tcp.go diff --git a/internal/cli/client/connector/tcp.go b/internal/cli/client/connector/tcp.go new file mode 100644 index 00000000..711ba560 --- /dev/null +++ b/internal/cli/client/connector/tcp.go @@ -0,0 +1,33 @@ +package connector + +import ( + "context" + "fmt" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "net/netip" +) + +// TCPConnector establishes a connection to the machine API through a direct TCP connection to an API endpoint. +type TCPConnector struct { + apiAddr netip.AddrPort +} + +func NewTCPConnector(apiAddr netip.AddrPort) *TCPConnector { + return &TCPConnector{apiAddr: apiAddr} +} + +func (c *TCPConnector) Connect(_ context.Context) (*grpc.ClientConn, error) { + conn, err := grpc.NewClient( + c.apiAddr.String(), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + if err != nil { + return nil, fmt.Errorf("create machine API client: %w", err) + } + return conn, nil +} + +func (c *TCPConnector) Close() error { + return nil +}