Skip to content

Commit

Permalink
Add ListQosVolume method.
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <[email protected]>
  • Loading branch information
artek-koltun committed May 4, 2023
1 parent 5c3f285 commit a3182b7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/middleend/qos.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@ import (
"context"
"fmt"
"log"
"sort"

"github.com/google/uuid"
"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/server"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)

type qosVolumesSortedByID []*pb.QosVolume

func (s qosVolumesSortedByID) Len() int {
return len(s)
}

func (s qosVolumesSortedByID) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s qosVolumesSortedByID) Less(i, j int) bool {
return s[i].QosVolumeId.Value < s[j].QosVolumeId.Value
}

// CreateQosVolume creates a QoS volume
func (s *Server) CreateQosVolume(_ context.Context, in *pb.CreateQosVolumeRequest) (*pb.QosVolume, error) {
log.Printf("CreateQosVolume: Received from client: %v", in)
Expand Down Expand Up @@ -87,6 +104,33 @@ func (s *Server) UpdateQosVolume(_ context.Context, in *pb.UpdateQosVolumeReques
return in.QosVolume, nil
}

// ListQosVolumes lists QoS volumes
func (s *Server) ListQosVolumes(_ context.Context, in *pb.ListQosVolumesRequest) (*pb.ListQosVolumesResponse, error) {
log.Printf("ListQosVolume: Received from client: %v", in)

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

volumes := []*pb.QosVolume{}
for _, qosVolume := range s.volumes.qosVolumes {
volumes = append(volumes, proto.Clone(qosVolume).(*pb.QosVolume))
}
sort.Sort(qosVolumesSortedByID(volumes))

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

return &pb.ListQosVolumesResponse{QosVolumes: volumes, NextPageToken: token}, nil
}

func (s *Server) verifyQosVolume(volume *pb.QosVolume) error {
if volume.QosVolumeId == nil || volume.QosVolumeId.Value == "" {
return fmt.Errorf("qos_volume_id cannot be empty")
Expand Down

0 comments on commit a3182b7

Please sign in to comment.