Skip to content

Commit

Permalink
feat: add ListSnapshots RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen005 committed Nov 28, 2024
1 parent 1eaa5d6 commit c45ee8b
Showing 1 changed file with 92 additions and 2 deletions.
94 changes: 92 additions & 2 deletions pkg/driver/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,84 @@ func (d *Driver) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequ
return nil, status.Error(codes.Unimplemented, "")
}

// ListSnapshots is part of implementing Snapshot & Restore functionality, but we don't support that
func (d *Driver) ListSnapshots(context.Context, *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
// ListSnapshots retrieves a list of existing snapshots as part of the Snapshot & Restore functionality.
func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
log.Info().Msg("Request: ListSnapshots")

snapshotID := req.GetSnapshotId()
sourceVolumeID := req.GetSourceVolumeId()

if req.GetStartingToken() != "" {
log.Error().Msgf("Pagination not supported")
return nil, status.Errorf(codes.Aborted, "%v not supported", "starting-token")
}

// case 1: SnapshotId is not empty, return snapshots that match the snapshot id
if len(snapshotID) != 0 {
log.Debug().Msgf("Fetching snapshot with ID %v", snapshotID)

// Retrieve a specific snapshot by ID
// Todo: GetSnapshot to be implemented in civogo
// Todo: Un-comment post client implementation
// snapshot, err := d.CivoClient.GetSnapshot(snapshotID)
// if err != nil {
// log.Error().Err(err).Msgf("No snapshot found matching the ID %v", snapshotID)
// if strings.Contains(err.Error(), "DatabaseSnapshotNotFoundError") {
// return &csi.ListSnapshotsResponse{}, nil
// }
// return nil, status.Errorf(codes.Internal, "failed to list snapshot %v: %v", snapshotID, err)
// }
// return &csi.ListSnapshotsResponse{
// Entries: []*csi.ListSnapshotsResponse_Entry{convertSnapshot(snapshot)},
// }, nil
}

// case 2: Retrieve snapshots by source volume ID
if len(sourceVolumeID) != 0 {
log.Debug().Msgf("Fetching snapshots for volume ID %v", sourceVolumeID)

// snapshots, err := d.CivoClient.ListSnapshots() // Todo: ListSnapshots to be implemented in civogo
// if err != nil{
// log.Error().Err(err).Msgf("Failed to list snapshots for volume %v", sourceVolumeID)
// return nil, status.Errorf(codes.Internal, "failed to list snapshots for volume %v: %v", sourceVolumeID, err)
// }

// entries := []*csi.ListSnapshotsResponse_Entry{}
// for _, snapshot := range snapshots {
// if snapshot.VolID == sourceVolumeID {
// entries = append(entries, convertSnapshot(snapshot))
// }
// }

// return &csi.ListSnapshotsResponse{
// Entries: entries,
// }, nil
}

log.Debug().Msg("Fetching all snapshots")

// case 3: Retrieve all snapshots if no filters are provided
// Todo: un-comment post client(civogo) implementation
// snapshots, err := d.CivoClient.ListSnapshots() // Todo: ListSnapshots to be implemented in civogo
// if err != nil{
// log.Error().Err(err).Msg("Failed to list snapshots from Civo API")
// return nil, status.Errorf(codes.Internal, "failed to list snapshots from Civo API %v", err)
// }

// sort.Slice(snapshots, func(i, j int) bool {
// return snapshots[i].Id < snapshots[j].Id
// })

// entries := []*csi.ListSnapshotsResponse_Entry{}
// for _, snap := range snapshots {
// entries = append(entries, convertSnapshot(snap))
// }

// log.Info().Msgf("Successfully listed snapshots. Total snapshots: %d", len(entries))

// return &csi.ListSnapshotsResponse{
// Entries: entries,
// }, nil
return nil, status.Error(codes.Unimplemented, "")
}

Expand All @@ -640,3 +716,17 @@ func getVolSizeInBytes(capRange *csi.CapacityRange) (int64, error) {

return bytes, nil
}

// Todo: Un-comment post client implementation is complete
// Todo: Snapshot to be defined in civogo
// func convertSnapshot(snap *civogo.Snapshot) *csi.ListSnapshotsResponse_Entry {
// return &csi.ListSnapshotsResponse_Entry{
// Snapshot: &csi.Snapshot{
// SnapshotId: snap.Id,
// SourceVolumeId: snap.VolID,
// CreationTime: snap.CreationTime,
// SizeBytes: snap.SizeBytes,
// ReadyToUse: snap.ReadyToUse,
// },
// }
// }

0 comments on commit c45ee8b

Please sign in to comment.