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

Implement ListVolumes Call #292

Merged
merged 1 commit into from
Jun 24, 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
17 changes: 17 additions & 0 deletions api/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ func NewVolumeService(logger log.Logger, client *hcloud.Client) *VolumeService {
}
}

func (s *VolumeService) All(ctx context.Context) ([]*csi.Volume, error) {
hcloudVolumes, err := s.client.Volume.All(ctx)
if err != nil {
level.Info(s.logger).Log(
"msg", "failed to get volumes",
"err", err,
)
return nil, err
}

volumes := make([]*csi.Volume, 0, len(hcloudVolumes))
for i, hcloudVolume := range hcloudVolumes {
volumes[i] = toDomainVolume(hcloudVolume)
}
return volumes, nil
}

func (s *VolumeService) Create(ctx context.Context, opts volumes.CreateOpts) (*csi.Volume, error) {
level.Info(s.logger).Log(
"msg", "creating volume",
Expand Down
34 changes: 32 additions & 2 deletions driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,31 @@ func (s *ControllerService) ValidateVolumeCapabilities(ctx context.Context, req
return resp, nil
}

func (s *ControllerService) ListVolumes(context.Context, *proto.ListVolumesRequest) (*proto.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "listing volumes is not supported")
func (s *ControllerService) ListVolumes(ctx context.Context, req *proto.ListVolumesRequest) (*proto.ListVolumesResponse, error) {
vols, err := s.volumeService.All(ctx)

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

resp := &proto.ListVolumesResponse{Entries: make([]*proto.ListVolumesResponse_Entry, 0, len(vols))}
for i, volume := range vols {
resp.Entries[i] = &proto.ListVolumesResponse_Entry{
Volume: &proto.Volume{
VolumeId: strconv.FormatUint(volume.ID, 10),
CapacityBytes: volume.SizeBytes(),
AccessibleTopology: []*proto.Topology{
{
Segments: map[string]string{
TopologySegmentLocation: volume.Location,
},
},
},
},
}
}

return resp, nil
}

func (s *ControllerService) GetCapacity(context.Context, *proto.GetCapacityRequest) (*proto.GetCapacityResponse, error) {
Expand Down Expand Up @@ -305,6 +328,13 @@ func (s *ControllerService) ControllerGetCapabilities(context.Context, *proto.Co
},
},
},
{
Type: &proto.ControllerServiceCapability_Rpc{
Rpc: &proto.ControllerServiceCapability_RPC{
Type: proto.ControllerServiceCapability_RPC_LIST_VOLUMES,
},
},
},
},
}
return resp, nil
Expand Down
13 changes: 13 additions & 0 deletions driver/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ type sanityVolumeService struct {
volumes list.List
}

func (s *sanityVolumeService) All(ctx context.Context) ([]*csi.Volume, error) {
s.mu.Lock()
defer s.mu.Unlock()

vols := []*csi.Volume{}
for e := s.volumes.Front(); e != nil; e = e.Next() {
v := e.Value.(*csi.Volume)
vols = append(vols, v)

}
return vols, nil
}

func (s *sanityVolumeService) Create(ctx context.Context, opts volumes.CreateOpts) (*csi.Volume, error) {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down
8 changes: 8 additions & 0 deletions mock/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type VolumeService struct {
CreateFunc func(ctx context.Context, opts volumes.CreateOpts) (*csi.Volume, error)
GetServerByIDFunc func(ctx context.Context, id int) (*hcloud.Server, error)
AllFunc func(ctx context.Context) ([]*csi.Volume, error)
GetByIDFunc func(ctx context.Context, id uint64) (*csi.Volume, error)
GetByNameFunc func(ctx context.Context, name string) (*csi.Volume, error)
DeleteFunc func(ctx context.Context, volume *csi.Volume) error
Expand All @@ -20,6 +21,13 @@ type VolumeService struct {
ResizeFunc func(ctx context.Context, volume *csi.Volume, size int) error
}

func (s *VolumeService) All(ctx context.Context) ([]*csi.Volume, error) {
if s.AllFunc == nil {
panic("not implemented")
}
return s.AllFunc(ctx)
}

func (s *VolumeService) Create(ctx context.Context, opts volumes.CreateOpts) (*csi.Volume, error) {
if s.CreateFunc == nil {
panic("not implemented")
Expand Down
4 changes: 4 additions & 0 deletions volumes/idempotency.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (s *IdempotentService) Create(ctx context.Context, opts CreateOpts) (*csi.V
return nil, err
}

func (s *IdempotentService) All(ctx context.Context) ([]*csi.Volume, error) {
return s.volumeService.All(ctx)
}

func (s *IdempotentService) GetByID(ctx context.Context, id uint64) (*csi.Volume, error) {
return s.volumeService.GetByID(ctx, id)
}
Expand Down
1 change: 1 addition & 0 deletions volumes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Service interface {
Attach(ctx context.Context, volume *csi.Volume, server *csi.Server) error
Detach(ctx context.Context, volume *csi.Volume, server *csi.Server) error
Resize(ctx context.Context, volume *csi.Volume, size int) error
All(ctx context.Context) ([]*csi.Volume, error)
}

// CreateOpts specifies the options for creating a volume.
Expand Down