Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connect and disconnect RPC #41

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ go 1.19

require (
github.com/opiproject/opi-api v0.0.0-20221109134843-76522ac45e89
github.com/stretchr/testify v1.8.1
google.golang.org/grpc v1.50.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 17 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/opiproject/opi-api v0.0.0-20221108132726-72b6da21c09a h1:VeVARKHUwoVMvIevAOI30Jmjkyy1xQHsa+yNOi97mIU=
github.com/opiproject/opi-api v0.0.0-20221108132726-72b6da21c09a/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221108164256-2a9284753fa3 h1:iwSQEZ9ZzPUsTRdiLLnK8ZBTLnFVAW1Pi88N2Id/rlM=
github.com/opiproject/opi-api v0.0.0-20221108164256-2a9284753fa3/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221108172248-8bc7b1116f8a h1:ZRG10EkYPLVZoh0Rj+frQlcoUr0ExtwucoGu09Ff5LU=
github.com/opiproject/opi-api v0.0.0-20221108172248-8bc7b1116f8a/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20221109134843-76522ac45e89 h1:yg9wTkuglnqdqMehhQ03o3FpYijUQrSvoWmtJq6+jHw=
github.com/opiproject/opi-api v0.0.0-20221109134843-76522ac45e89/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/net v0.0.0-20221002022538-bcab6841153b h1:6e93nYa3hNqAvLr0pD4PN1fFS+gKzp2zAXqrnTCstqU=
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
Expand All @@ -27,3 +33,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
93 changes: 93 additions & 0 deletions goopicsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

var (
conn *grpc.ClientConn
address = "localhost:50051"
)

// ConnectToRemoteAndExpose connects to the remote storage over NVMe/TCP and exposes it as a local NVMe/PCIe device
func ConnectToRemoteAndExpose(addr string) error {
flag.Parse()
Expand Down Expand Up @@ -69,3 +74,91 @@ func ConnectToRemoteAndExpose(addr string) error {
log.Printf("Added: %v", rn1)
return nil
}

// NVMeControllerConnect Connects to remote NVMf controller
func NVMeControllerConnect(request *pb.NVMfRemoteController) (*pb.NVMfRemoteControllerConnectResponse, error) {
if conn == nil {
err := dialConnection()
if err != nil {
return nil, err
}
}

client := pb.NewNVMfRemoteControllerServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

data, err := client.NVMfRemoteControllerGet(ctx, &pb.NVMfRemoteControllerGetRequest{Id: request.Id})
if err != nil {
log.Println(err)
}
log.Println(data)

// we will connect if there is no connection established
if data == nil { // This means we are unable to get a connection with this ID
response, err := client.NVMfRemoteControllerConnect(ctx, &pb.NVMfRemoteControllerConnectRequest{Ctrl: request})
if err != nil {
log.Printf("could not connect to Remote NVMf controller: %v", err)
return nil, err
}
log.Printf("Connected: %v", response)
return response, nil
}
log.Printf("Remote NVMf controller is already connected with SubNQN: %v", data.GetCtrl().Subnqn)
defer disconnectConnection()
return &pb.NVMfRemoteControllerConnectResponse{}, nil
}

// NVMeControllerDisconnect disconnects remote NVMf controller connection
func NVMeControllerDisconnect(request *pb.NVMfRemoteControllerDisconnectRequest) (*pb.NVMfRemoteControllerDisconnectResponse, error) {
if conn == nil {
err := dialConnection()
if err != nil {
return nil, err
}
}

client := pb.NewNVMfRemoteControllerServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

data, err := client.NVMfRemoteControllerGet(ctx, &pb.NVMfRemoteControllerGetRequest{Id: request.Id})
if err != nil {
log.Println(err)
return nil, err
}
log.Println(data)

// we will disconnect if there is a connection
if data != nil {
response, err := client.NVMfRemoteControllerDisconnect(ctx, &pb.NVMfRemoteControllerDisconnectRequest{Id: request.Id})
if err != nil {
log.Printf("could not disconnect Remote NVMf controller: %v", err)
return nil, err
}
log.Printf("disconnected: %v", response)
return response, nil
}
log.Printf("Remote NVMf controller disconnected successfully: %v", data.GetCtrl().Subnqn)
defer disconnectConnection()
return &pb.NVMfRemoteControllerDisconnectResponse{}, nil
}

func dialConnection() error {
var err error
conn, err = grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Printf("Failed to connect: %v", err)
return err
}
return nil
}

func disconnectConnection() {
err := conn.Close()
if err != nil {
log.Fatalf("Failed to close connection: %v", err)
} else {
log.Println("GRPC connection closed successfully")
}
}
26 changes: 25 additions & 1 deletion goopicsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@
package goopicsi

import (
"log"
"testing"

pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/stretchr/testify/assert"
)

func TestValidateSomething(t *testing.T) {
func TestNVMeControllerConnect(t *testing.T) {
resp, err := NVMeControllerConnect(&pb.NVMfRemoteController{
Id: 12,
Traddr: "0.0.0.0", // Add a valid target address
Subnqn: "nqn", // Add a valid NQN
Trsvcid: 4420,
})
if err != nil {
log.Println(err)
}
log.Println(resp)
assert.Error(t, err, "connection failed")
}

func TestNVMeControllerDisconnect(t *testing.T) {
resp, err := NVMeControllerDisconnect(&pb.NVMfRemoteControllerDisconnectRequest{Id: 12})
if err != nil {
log.Println(err)
}
log.Println(resp)
assert.Error(t, err, "disconnect failed")
}