rbd: add wrappers for rbd_namespace_create(), rbd_namespace_exists() and rbd_namespace_remove().

rbd_namespace_create() function creates namespace for a given iocontext.
rbd_namespace_exists() function checks whether the given namespace exists or not.
rbd_namespace_remove() function removes the given namespace.

Signed-off-by: Mudit Agarwal <muagarwa@redhat.com>
This commit is contained in:
Mudit Agarwal 2020-06-06 11:13:47 +05:30 committed by John Mulligan
parent 7be8bbfe81
commit 08536b9d93
3 changed files with 153 additions and 0 deletions

View File

@ -66,6 +66,9 @@ var (
// ErrNotFound may be returned from an api call when the requested item is
// missing.
ErrNotFound = errors.New("RBD image not found")
// ErrNoNamespaceName maye be returned if an api call requires a namespace
// name and it is not provided.
ErrNoNamespaceName = errors.New("Namespace value is missing")
// revive:disable:exported for compatibility with old versions
RbdErrorImageNotOpen = ErrImageNotOpen

74
rbd/namespace_nautilus.go Normal file
View File

@ -0,0 +1,74 @@
// +build !luminous,!mimic
//
// Ceph Nautilus is the first release that includes rbd_namespace_create(),
// rbd_namespace_remove(), rbd_namespace_exists().
package rbd
// #cgo LDFLAGS: -lrbd
// #include <rados/librados.h>
// #include <rbd/librbd.h>
// #include <stdlib.h>
// #include <errno.h>
import "C"
import (
"unsafe"
"github.com/ceph/go-ceph/rados"
)
// NamespaceCreate creates the namespace for a given Rados IOContext.
//
// Implements:
// int rbd_namespace_create(rados_ioctx_t io, const char *namespace_name);
func NamespaceCreate(ioctx *rados.IOContext, namespaceName string) error {
if ioctx == nil {
return ErrNoIOContext
}
if namespaceName == "" {
return ErrNoNamespaceName
}
cNamespaceName := C.CString(namespaceName)
defer C.free(unsafe.Pointer(cNamespaceName))
ret := C.rbd_namespace_create(cephIoctx(ioctx), cNamespaceName)
return getError(ret)
}
// NamespaceRemove removes a given namespace.
//
// Implements:
// int rbd_namespace_remove(rados_ioctx_t io, const char *namespace_name);
func NamespaceRemove(ioctx *rados.IOContext, namespaceName string) error {
if ioctx == nil {
return ErrNoIOContext
}
if namespaceName == "" {
return ErrNoNamespaceName
}
cNamespaceName := C.CString(namespaceName)
defer C.free(unsafe.Pointer(cNamespaceName))
ret := C.rbd_namespace_remove(cephIoctx(ioctx), cNamespaceName)
return getError(ret)
}
// NamespaceExists checks whether a given namespace exists or not.
//
// Implements:
// int rbd_namespace_exists(rados_ioctx_t io, const char *namespace_name, bool *exists);
func NamespaceExists(ioctx *rados.IOContext, namespaceName string) (bool, error) {
if ioctx == nil {
return false, ErrNoIOContext
}
if namespaceName == "" {
return false, ErrNoNamespaceName
}
cNamespaceName := C.CString(namespaceName)
defer C.free(unsafe.Pointer(cNamespaceName))
var exists C.bool
ret := C.rbd_namespace_exists(cephIoctx(ioctx), cNamespaceName, &exists)
return bool(exists), getErrorIfNegative(ret)
}

View File

@ -0,0 +1,76 @@
// +build !luminous,!mimic
package rbd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNamespace(t *testing.T) {
conn := radosConnect(t)
poolName := GetUUID()
err := conn.MakePool(poolName)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolName)
assert.NoError(t, err)
defer func() {
ioctx.Destroy()
conn.DeletePool(poolName)
conn.Shutdown()
}()
t.Run("invalidInputNamespace", func(t *testing.T) {
// NamespaceCreate.
err := NamespaceCreate(nil, "someName")
assert.Error(t, err)
err = NamespaceCreate(ioctx, "")
assert.Error(t, err)
// NamespaceRemove.
err = NamespaceRemove(nil, "someName")
assert.Error(t, err)
err = NamespaceRemove(ioctx, "")
assert.Error(t, err)
// NamespaceExists.
_, err = NamespaceExists(nil, "someName")
assert.Error(t, err)
_, err = NamespaceExists(ioctx, "")
assert.Error(t, err)
})
t.Run("CreateNamespace", func(t *testing.T) {
nameSpace := "myNamespace"
err := NamespaceCreate(ioctx, nameSpace)
assert.NoError(t, err)
// Check whether it exists or not.
val, err := NamespaceExists(ioctx, nameSpace)
assert.NoError(t, err)
assert.Equal(t, val, true)
// Create again with same name.
err = NamespaceCreate(ioctx, nameSpace)
assert.Error(t, err)
// Remove the namespace.
err = NamespaceRemove(ioctx, nameSpace)
assert.NoError(t, err)
})
t.Run("NonExistingNamespace", func(t *testing.T) {
// Try to remove.
err := NamespaceRemove(ioctx, "someNamespace")
assert.Error(t, err)
// Check the existence.
val, err := NamespaceExists(ioctx, "someNamespace")
assert.NoError(t, err)
assert.Equal(t, val, false)
})
}