diff --git a/go.sum b/go.sum index ffdfe68..3d88b41 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,6 @@ 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-20221117170559-ca2c25b808b3 h1:5NztGQz+dkljZD1737jOmmsUA3Hcl+7mZpdcmR5Kn8s= -github.com/opiproject/opi-api v0.0.0-20221117170559-ca2c25b808b3/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY= github.com/opiproject/opi-api v0.0.0-20221121200811-5d2289de810a h1:dFFDDQez4aCsKEkvEnQ+yFnMu5/yZ/VWBxbIRmAvgd4= github.com/opiproject/opi-api v0.0.0-20221121200811-5d2289de810a/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -23,16 +21,12 @@ golang.org/x/net v0.0.0-20221002022538-bcab6841153b h1:6e93nYa3hNqAvLr0pD4PN1fFS 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= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 h1:Ezh2cpcnP5Rq60sLensUsFnxh7P6513NLvNtCm9iyJ4= google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91/go.mod h1:3526vdqwhZAwq4wsRUaVG555sVgsNmIjRtO7t/JH29U= -google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY= -google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U= google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/goopicsi.go b/goopicsi.go index e463199..3f67583 100644 --- a/goopicsi.go +++ b/goopicsi.go @@ -136,6 +136,7 @@ func NVMeControllerConnect(id int64, trAddr string, subnqn string, trSvcID int64 return nil } log.Printf("Remote NVMf controller is already connected with SubNQN: %v", data.GetCtrl().Subnqn) + defer disconnectConnection() return nil } @@ -225,6 +226,68 @@ func NVMeControllerDisconnect(id int64) error { return nil } +// ExposeRemoteNVMe creates a new NVMe Subsystem and NVMe controller +func ExposeRemoteNVMe(subsystemID string, subsystemNQN string, maxNamespaces int64, controllerID string) error { + if conn == nil { + err := dialConnection() + if err != nil { + return err + } + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + client := pb.NewFrontendNvmeServiceClient(conn) + data1, err := client.GetNVMeSubsystem(ctx, &pb.GetNVMeSubsystemRequest{SubsystemId: &pbc.ObjectKey{Value: subsystemID}}) + if err != nil { + log.Printf("No existing NVMe Subsystem found with subsystemID: %v", subsystemID) + } + + if data1 == nil { + response1, err := client.CreateNVMeSubsystem(ctx, &pb.CreateNVMeSubsystemRequest{ + Subsystem: &pb.NVMeSubsystem{ + Spec: &pb.NVMeSubsystemSpec{ + Id: &pbc.ObjectKey{Value: subsystemID}, + Nqn: subsystemNQN, + MaxNamespaces: maxNamespaces, + }, + }, + }) + if err != nil { + log.Println(err) + return err + } + log.Printf("NVMe Subsytem created: %v", response1) + } else { + log.Printf("NVMe Subsystem is already present with the subsytemID: %v", subsystemID) + } + + data2, err := client.GetNVMeController(ctx, &pb.GetNVMeControllerRequest{ControllerId: &pbc.ObjectKey{Value: controllerID}}) + if err != nil { + log.Printf("No existing NVMe Controller found with controllerID %v:", controllerID) + } + if data2 == nil { + response2, err := client.CreateNVMeController(ctx, &pb.CreateNVMeControllerRequest{ + Controller: &pb.NVMeController{ + Spec: &pb.NVMeControllerSpec{ + Id: &pbc.ObjectKey{Value: controllerID}, + SubsystemId: &pbc.ObjectKey{Value: subsystemID}, + MaxNamespaces: int32(maxNamespaces), + }, + }, + }) + if err != nil { + log.Println(err) + return err + } + log.Printf("NVMe Controller created: %v", response2) + return nil + } + log.Printf("NVMe Controller is already present with the controllerID: %v", controllerID) + return nil +} + // CreateNVMeNamespace Creates a new NVMe namespace func CreateNVMeNamespace(id string, subSystemID string, nguid string, hostID int32) (string, error) { if conn == nil { diff --git a/goopicsi_test.go b/goopicsi_test.go index 7c1c39f..d7ac84b 100644 --- a/goopicsi_test.go +++ b/goopicsi_test.go @@ -55,3 +55,10 @@ func TestDeleteNVMeNamespace(t *testing.T) { log.Println(err) } } + +func TestExposeRemoteNVMe(t *testing.T) { + err := ExposeRemoteNVMe("subsystem1", "nqn.2022-09.io.spdk:test", 10, "controller1") + if err != nil { + log.Println(err) + } +}