rbd: add wrapper for rbd_namespace_list() function.

Added wrapper for rbd_namespace_list() function which returns the names of all the namespaces
present for a given IOContext.

Signed-off-by: Mudit Agarwal muagarwa@redhat.com
This commit is contained in:
Mudit Agarwal 2020-06-09 06:40:06 +05:30 committed by John Mulligan
parent 08536b9d93
commit 5b6c599176
2 changed files with 70 additions and 1 deletions

View File

@ -1,7 +1,7 @@
// +build !luminous,!mimic
//
// Ceph Nautilus is the first release that includes rbd_namespace_create(),
// rbd_namespace_remove(), rbd_namespace_exists().
// rbd_namespace_remove(), rbd_namespace_exists() and rbd_namespace_list().
package rbd
@ -13,8 +13,10 @@ package rbd
import "C"
import (
"bytes"
"unsafe"
"github.com/ceph/go-ceph/internal/retry"
"github.com/ceph/go-ceph/rados"
)
@ -72,3 +74,38 @@ func NamespaceExists(ioctx *rados.IOContext, namespaceName string) (bool, error)
ret := C.rbd_namespace_exists(cephIoctx(ioctx), cNamespaceName, &exists)
return bool(exists), getErrorIfNegative(ret)
}
// NamespaceList returns a slice containing the names of existing rbd namespaces.
//
// Implements:
// int rbd_namespace_list(rados_ioctx_t io, char *namespace_names, size_t *size);
func NamespaceList(ioctx *rados.IOContext) (names []string, err error) {
if ioctx == nil {
return nil, ErrNoIOContext
}
var (
buf []byte
cSize C.size_t
)
retry.WithSizes(4096, 262144, func(size int) retry.Hint {
cSize = C.size_t(size)
buf = make([]byte, cSize)
ret := C.rbd_namespace_list(cephIoctx(ioctx),
(*C.char)(unsafe.Pointer(&buf[0])),
&cSize)
err = getErrorIfNegative(ret)
return retry.Size(int(cSize)).If(err == errRange)
})
if err != nil {
return nil, err
}
tmpList := bytes.Split(buf[:cSize-1], []byte{0})
for _, s := range tmpList {
if len(s) > 0 {
name := C.GoString((*C.char)(unsafe.Pointer(&s[0])))
names = append(names, name)
}
}
return names, nil
}

View File

@ -42,6 +42,10 @@ func TestNamespace(t *testing.T) {
assert.Error(t, err)
_, err = NamespaceExists(ioctx, "")
assert.Error(t, err)
// NamespaceList.
_, err = NamespaceList(nil)
assert.Error(t, err)
})
t.Run("CreateNamespace", func(t *testing.T) {
@ -73,4 +77,32 @@ func TestNamespace(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, val, false)
})
t.Run("NamespaceList", func(t *testing.T) {
var (
name1 = "name1"
name2 = "name2"
name3 = "name3"
)
err := NamespaceCreate(ioctx, name1)
assert.NoError(t, err)
err = NamespaceCreate(ioctx, name2)
assert.NoError(t, err)
err = NamespaceCreate(ioctx, name3)
assert.NoError(t, err)
defer func() {
assert.NoError(t, NamespaceRemove(ioctx, name1))
assert.NoError(t, NamespaceRemove(ioctx, name2))
assert.NoError(t, NamespaceRemove(ioctx, name3))
}()
eval, err := NamespaceExists(ioctx, name1)
assert.NoError(t, err)
assert.Equal(t, eval, true)
nList, err := NamespaceList(ioctx)
assert.NoError(t, err)
assert.Contains(t, nList, name1)
assert.Contains(t, nList, name2)
assert.Contains(t, nList, name3)
})
}