Skip to content

Commit

Permalink
Provide ListNVMfRemoteNamespaces implementation.
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <artsiom.koltun@intel.com>
  • Loading branch information
artek-koltun committed Jun 15, 2023
1 parent 17e5134 commit 6bc1b58
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions pkg/backend/nvme_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.
// Copyright (C) 2023 Intel Corporation

// Package backend implements the BackEnd APIs (network facing) of the storage Server
package backend

import (
"context"
"log"
"path"
"sort"
"strings"

"github.com/google/uuid"
"github.com/opiproject/gospdk/spdk"
pc "github.com/opiproject/opi-api/common/v1/gen/go"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/server"
"go.einride.tech/aip/fieldbehavior"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// ListNVMfRemoteNamespaces lists NVMfRemoteNamespaces exposed by connected NVMfRemoteController.
func (s *Server) ListNVMfRemoteNamespaces(_ context.Context, in *pb.ListNVMfRemoteNamespacesRequest) (*pb.ListNVMfRemoteNamespacesResponse, error) {
log.Printf("ListNVMfRemoteNamespaces: Received from client: %v", in)
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}

Check warning on line 31 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L26-L31

Added lines #L26 - L31 were not covered by tests

if _, ok := s.Volumes.NvmeControllers[in.Parent]; !ok {
err := status.Errorf(codes.InvalidArgument, "unable to find key %s", in.Parent)
log.Printf("error: %v", err)
return nil, err
}

Check warning on line 37 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L33-L37

Added lines #L33 - L37 were not covered by tests

size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
}

Check warning on line 43 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L39-L43

Added lines #L39 - L43 were not covered by tests

var result []spdk.BdevGetBdevsResult
err := s.rpc.Call("bdev_get_bdevs", nil, &result)
if err != nil {
log.Printf("error: %v", err)
return nil, err
}
log.Printf("Received from SPDK: %v", result)

Blobarray := []*pb.NVMfRemoteNamespace{}
resourceID := path.Base(in.Parent)
namespacePrefix := resourceID + "n"
for _, bdev := range result {
if strings.HasPrefix(bdev.Name, namespacePrefix) {
Blobarray = append(Blobarray, &pb.NVMfRemoteNamespace{
Name: server.ResourceIDToVolumeName(bdev.Name),
Uuid: &pc.Uuid{Value: bdev.UUID},
})
}

Check warning on line 62 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L45-L62

Added lines #L45 - L62 were not covered by tests
}

sortNVMfRemoteNamespaces(Blobarray)

token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(Blobarray), offset, size)
Blobarray, hasMoreElements := server.LimitPagination(Blobarray, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}

Check warning on line 73 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L65-L73

Added lines #L65 - L73 were not covered by tests

return &pb.ListNVMfRemoteNamespacesResponse{
NvMfRemoteNamespaces: Blobarray,
NextPageToken: token,
}, nil

Check warning on line 78 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L75-L78

Added lines #L75 - L78 were not covered by tests
}

func sortNVMfRemoteNamespaces(namespaces []*pb.NVMfRemoteNamespace) {
sort.Slice(namespaces, func(i int, j int) bool {
return namespaces[i].Name < namespaces[j].Name
})

Check warning on line 84 in pkg/backend/nvme_namespace.go

View check run for this annotation

Codecov / codecov/patch

pkg/backend/nvme_namespace.go#L81-L84

Added lines #L81 - L84 were not covered by tests
}

0 comments on commit 6bc1b58

Please sign in to comment.