rbd: add manager GetSnapshotByID and SnapshotResolver interface

A (CSI) VolumeGroupSnapshot object contains references to Snapshot IDs
(or CSI Snapshot handles). In order to work with a VolumeGroupSnapshot
struct, the Snapshot IDs need to be resolved into rbdSnapshot structs.

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2024-10-04 15:40:33 +02:00 committed by mergify[bot]
parent 455a90e9f4
commit 9bea3feff1
2 changed files with 34 additions and 0 deletions

View File

@ -139,6 +139,31 @@ func (mgr *rbdManager) GetVolumeByID(ctx context.Context, id string) (types.Volu
return volume, nil
}
func (mgr *rbdManager) GetSnapshotByID(ctx context.Context, id string) (types.Snapshot, error) {
creds, err := mgr.getCredentials()
if err != nil {
return nil, err
}
snapshot, err := genSnapFromSnapID(ctx, id, creds, mgr.secrets)
if err != nil {
switch {
case errors.Is(err, ErrImageNotFound):
err = fmt.Errorf("volume %s not found: %w", id, err)
return nil, err
case errors.Is(err, util.ErrPoolNotFound):
err = fmt.Errorf("pool %s not found for %s: %w", snapshot.Pool, id, err)
return nil, err
default:
return nil, fmt.Errorf("failed to get volume from id %q: %w", id, err)
}
}
return snapshot, nil
}
func (mgr *rbdManager) GetVolumeGroupByID(ctx context.Context, id string) (types.VolumeGroup, error) {
creds, err := mgr.getCredentials()
if err != nil {

View File

@ -26,6 +26,12 @@ type VolumeResolver interface {
GetVolumeByID(ctx context.Context, id string) (Volume, error)
}
// SnapshotResolver can be used to construct a Snapshot from a CSI SnapshotId.
type SnapshotResolver interface {
// GetSnapshotByID uses the CSI SnapshotId to resolve the returned Snapshot.
GetSnapshotByID(ctx context.Context, id string) (Snapshot, error)
}
// Manager provides a way for other packages to get Volumes and VolumeGroups.
// It handles the operations on the backend, and makes sure the journal
// reflects the expected state.
@ -33,6 +39,9 @@ type Manager interface {
// VolumeResolver is fully implemented by the Manager.
VolumeResolver
// SnapshotResolver is fully implemented by the Manager.
SnapshotResolver
// Destroy frees all resources that the Manager allocated.
Destroy(ctx context.Context)