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 CreateNamespace RPC #45

Merged
merged 3 commits into from
Nov 14, 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
67 changes: 52 additions & 15 deletions goopicsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,43 @@ func ConnectToRemoteAndExpose(addr string) error {
}

// NVMeControllerConnect Connects to remote NVMf controller
func NVMeControllerConnect(request *pb.NVMfRemoteController) (*pb.NVMfRemoteControllerConnectResponse, error) {
func NVMeControllerConnect(id int64, trAddr string, subnqn string, trSvcID int64) error {
if conn == nil {
err := dialConnection()
if err != nil {
return nil, err
return 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})
data, err := client.NVMfRemoteControllerGet(ctx, &pb.NVMfRemoteControllerGetRequest{Id: 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})
request := &pb.NVMfRemoteControllerConnectRequest{Ctrl: &pb.NVMfRemoteController{
Id: id,
Traddr: trAddr,
Subnqn: subnqn,
Trsvcid: trSvcID,
}}
response, err := client.NVMfRemoteControllerConnect(ctx, request)
if err != nil {
log.Printf("could not connect to Remote NVMf controller: %v", err)
return nil, err
return err
}
log.Printf("Connected: %v", response)
return response, nil
return nil
}
log.Printf("Remote NVMf controller is already connected with SubNQN: %v", data.GetCtrl().Subnqn)
defer disconnectConnection()
return &pb.NVMfRemoteControllerConnectResponse{}, nil
return nil
}

// NVMeControllerList lists all the connections to the remote NVMf controller
Expand Down Expand Up @@ -171,38 +177,69 @@ func NVMeControllerGet(id int64) error {
}

// NVMeControllerDisconnect disconnects remote NVMf controller connection
func NVMeControllerDisconnect(request *pb.NVMfRemoteControllerDisconnectRequest) (*pb.NVMfRemoteControllerDisconnectResponse, error) {
func NVMeControllerDisconnect(id int64) error {
if conn == nil {
err := dialConnection()
if err != nil {
return nil, err
return 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})
data, err := client.NVMfRemoteControllerGet(ctx, &pb.NVMfRemoteControllerGetRequest{Id: id})
if err != nil {
log.Println(err)
return nil, err
return 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})
response, err := client.NVMfRemoteControllerDisconnect(ctx, &pb.NVMfRemoteControllerDisconnectRequest{Id: id})
if err != nil {
log.Printf("could not disconnect Remote NVMf controller: %v", err)
return nil, err
return err
}
log.Printf("disconnected: %v", response)
return response, nil
return nil
}
log.Printf("Remote NVMf controller disconnected successfully: %v", data.GetCtrl().Subnqn)
defer disconnectConnection()
return &pb.NVMfRemoteControllerDisconnectResponse{}, nil
return nil
}

// CreateNVMeNamespace Creates a new NVMe namespace
func CreateNVMeNamespace(id string, subSystemID string, volumeID string, hostID int32) (string, error) {
if conn == nil {
err := dialConnection()
if err != nil {
return "", err
}
}

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

resp, err := client.CreateNVMeNamespace(ctx, &pb.CreateNVMeNamespaceRequest{
Namespace: &pb.NVMeNamespace{
Spec: &pb.NVMeNamespaceSpec{
Id: &pbc.ObjectKey{Value: id},
SubsystemId: &pbc.ObjectKey{Value: subSystemID},
VolumeId: &pbc.ObjectKey{Value: volumeID},
HostNsid: hostID,
},
},
})
if err != nil {
log.Println(err)
return "", err
}
log.Println(resp)
return resp.Spec.Id.Value, nil
}

func dialConnection() error {
Expand Down
21 changes: 10 additions & 11 deletions goopicsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ import (
"log"
"testing"

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

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,
})
err := NVMeControllerConnect(12, "", "", 44565)
if err != nil {
log.Println(err)
}
log.Println(resp)
assert.Error(t, err, "connection failed")
assert.Error(t, err)
}

func TestNVMeControllerList(t *testing.T) {
Expand All @@ -40,10 +33,16 @@ func TestNVMeControllerGet(t *testing.T) {
}

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

func TestCreateNVMeNamespace(t *testing.T) {
resp, err := CreateNVMeNamespace("1", "nqn", "opi", 1)
if err != nil {
log.Println(err)
}
log.Println(resp)
assert.Error(t, err, "disconnect failed")
}