rbd: create a standalone RemoveImage function

Currently, the code only provides a .Remove function on the Image type.
But this is unnecessary as the underlying api only requires an io
context and name. Make a function that matches the underlying api
better.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-01-24 09:34:29 -05:00 committed by John Mulligan
parent 635ca3cdb2
commit 2a74b02c2b
2 changed files with 49 additions and 7 deletions

View File

@ -376,10 +376,7 @@ func (image *Image) Remove() error {
if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil { if err := image.validate(imageNeedsIOContext | imageNeedsName); err != nil {
return err return err
} }
return RemoveImage(image.ioctx, image.name)
c_name := C.CString(image.name)
defer C.free(unsafe.Pointer(c_name))
return GetError(C.rbd_remove(C.rados_ioctx_t(image.ioctx.Pointer()), c_name))
} }
// Trash will move an image into the RBD trash, where it will be protected (i.e., salvageable) for // Trash will move an image into the RBD trash, where it will be protected (i.e., salvageable) for
@ -1352,3 +1349,13 @@ func CreateImage(ioctx *rados.IOContext, name string, size uint64, rio *RbdImage
C.uint64_t(size), C.rbd_image_options_t(rio.options)) C.uint64_t(size), C.rbd_image_options_t(rio.options))
return GetError(ret) return GetError(ret)
} }
// RemoveImage removes the specified rbd image.
//
// Implements:
// int rbd_remove(rados_ioctx_t io, const char *name);
func RemoveImage(ioctx *rados.IOContext, name string) error {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
return GetError(C.rbd_remove(C.rados_ioctx_t(ioctx.Pointer()), c_name))
}

View File

@ -169,7 +169,7 @@ func TestCreateImageWithOptions(t *testing.T) {
name = GetUUID() name = GetUUID()
err = CreateImage(ioctx, name, 1<<22, options) err = CreateImage(ioctx, name, 1<<22, options)
assert.NoError(t, err) assert.NoError(t, err)
err = GetImage(ioctx, name).Remove() err = RemoveImage(ioctx, name)
assert.NoError(t, err) assert.NoError(t, err)
// create image with RbdImageOptionOrder // create image with RbdImageOptionOrder
@ -178,7 +178,7 @@ func TestCreateImageWithOptions(t *testing.T) {
name = GetUUID() name = GetUUID()
err = CreateImage(ioctx, name, 1<<22, options) err = CreateImage(ioctx, name, 1<<22, options)
assert.NoError(t, err) assert.NoError(t, err)
err = GetImage(ioctx, name).Remove() err = RemoveImage(ioctx, name)
assert.NoError(t, err) assert.NoError(t, err)
options.Clear() options.Clear()
@ -191,7 +191,7 @@ func TestCreateImageWithOptions(t *testing.T) {
name = GetUUID() name = GetUUID()
err = CreateImage(ioctx, name, 1<<22, options) err = CreateImage(ioctx, name, 1<<22, options)
assert.NoError(t, err) assert.NoError(t, err)
err = GetImage(ioctx, name).Remove() err = RemoveImage(ioctx, name)
assert.NoError(t, err) assert.NoError(t, err)
conn.DeletePool(datapool) conn.DeletePool(datapool)
@ -1310,3 +1310,38 @@ func TestOpenImage(t *testing.T) {
conn.DeletePool(poolname) conn.DeletePool(poolname)
conn.Shutdown() conn.Shutdown()
} }
func TestRemoveImage(t *testing.T) {
conn := radosConnect(t)
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolname)
require.NoError(t, err)
// trying to remove a non-existent image is an error
err = RemoveImage(ioctx, "bananarama")
require.Error(t, err)
// create and then remove an image
name := GetUUID()
err = CreateImage(ioctx, name, 1<<22, NewRbdImageOptions())
assert.NoError(t, err)
imageNames, err := GetImageNames(ioctx)
assert.NoError(t, err)
assert.Contains(t, imageNames, name)
err = RemoveImage(ioctx, name)
assert.NoError(t, err)
imageNames, err = GetImageNames(ioctx)
assert.NoError(t, err)
assert.NotContains(t, imageNames, name)
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}