From 8e4b8349aada8f2aa45abda191de4bbc7f934fc9 Mon Sep 17 00:00:00 2001 From: oct28-yjkim <74443370+oct28-yjkim@users.noreply.github.com> Date: Mon, 10 May 2021 14:34:52 +0900 Subject: [PATCH] rbd: add SetSnapshot (re)implementing rbd_snap_set Signed-off-by: oct28.yjkim --- rbd/rbd.go | 16 ++++++++++++++++ rbd/snapshot.go | 6 ++---- rbd/snapshot_test.go | 3 +++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/rbd/rbd.go b/rbd/rbd.go index 066e009..f827bd2 100644 --- a/rbd/rbd.go +++ b/rbd/rbd.go @@ -916,6 +916,22 @@ func (image *Image) GetId() (string, error) { } +// SetSnapshot updates the rbd image (not the Snapshot) such that the snapshot +// is the source of readable data. +// +// Implements: +// int rbd_snap_set(rbd_image_t image, const char *snapname); +func (image *Image) SetSnapshot(snapname string) error { + if err := image.validate(imageIsOpen); err != nil { + return err + } + + c_snapname := C.CString(snapname) + defer C.free(unsafe.Pointer(c_snapname)) + + return getError(C.rbd_snap_set(image.image, c_snapname)) +} + // GetTrashList returns a slice of TrashInfo structs, containing information about all RBD images // currently residing in the trash. func GetTrashList(ioctx *rados.IOContext) ([]TrashInfo, error) { diff --git a/rbd/snapshot.go b/rbd/snapshot.go index 9f10c63..97277d7 100644 --- a/rbd/snapshot.go +++ b/rbd/snapshot.go @@ -149,6 +149,7 @@ func (snapshot *Snapshot) IsProtected() (bool, error) { // Set updates the rbd image (not the Snapshot) such that the snapshot // is the source of readable data. +// This method is deprecated. Refer the SetSnapshot method of the Image type instead. // // Implements: // int rbd_snap_set(rbd_image_t image, const char *snapname); @@ -157,10 +158,7 @@ func (snapshot *Snapshot) Set() error { return err } - c_snapname := C.CString(snapshot.name) - defer C.free(unsafe.Pointer(c_snapname)) - - return getError(C.rbd_snap_set(snapshot.image.image, c_snapname)) + return snapshot.image.SetSnapshot(snapshot.name) } // GetSnapTimestamp returns the timestamp of a snapshot for an image. diff --git a/rbd/snapshot_test.go b/rbd/snapshot_test.go index be8e4b4..6cc7442 100644 --- a/rbd/snapshot_test.go +++ b/rbd/snapshot_test.go @@ -94,6 +94,9 @@ func TestErrorSnapshotNoName(t *testing.T) { _, err = snapshot.IsProtected() assert.Equal(t, err, ErrSnapshotNoName) + err = img.SetSnapshot(snapshot.name) + assert.Equal(t, err, ErrImageNotOpen) + err = snapshot.Set() assert.Equal(t, err, ErrSnapshotNoName)