mirror of https://github.com/ceph/go-ceph
rbd: add metadata functions on image to get, set, remove metadata
Signed-off-by: Anthony ARNAUD <github@anthony-arnaud.fr>
This commit is contained in:
parent
2b2fcc121d
commit
777e79c046
63
rbd/rbd.go
63
rbd/rbd.go
|
@ -1019,6 +1019,69 @@ func (image *Image) GetParentInfo(p_pool, p_name, p_snapname []byte) error {
|
|||
}
|
||||
}
|
||||
|
||||
// int rbd_metadata_get(rbd_image_t image, const char *key, char *value, size_t *vallen)
|
||||
func (image *Image) GetMetadata(key string) (string, error) {
|
||||
if err := image.validate(imageIsOpen); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
c_key := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(c_key))
|
||||
|
||||
var c_vallen C.size_t
|
||||
ret := C.rbd_metadata_get(image.image, c_key, nil, (*C.size_t)(&c_vallen))
|
||||
// get size of value
|
||||
// ret -34 because we pass nil as value pointer
|
||||
if ret != 0 && ret != -C.ERANGE {
|
||||
return "", RBDError(ret)
|
||||
}
|
||||
|
||||
// make a bytes array with a good size
|
||||
value := make([]byte, c_vallen-1)
|
||||
ret = C.rbd_metadata_get(image.image, c_key, (*C.char)(unsafe.Pointer(&value[0])), (*C.size_t)(&c_vallen))
|
||||
if ret < 0 {
|
||||
return "", RBDError(ret)
|
||||
}
|
||||
|
||||
return string(value), nil
|
||||
}
|
||||
|
||||
// int rbd_metadata_set(rbd_image_t image, const char *key, const char *value)
|
||||
func (image *Image) SetMetadata(key string, value string) error {
|
||||
if err := image.validate(imageIsOpen); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c_key := C.CString(key)
|
||||
c_value := C.CString(value)
|
||||
defer C.free(unsafe.Pointer(c_key))
|
||||
defer C.free(unsafe.Pointer(c_value))
|
||||
|
||||
ret := C.rbd_metadata_set(image.image, c_key, c_value)
|
||||
if ret < 0 {
|
||||
return RBDError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// int rbd_metadata_remove(rbd_image_t image, const char *key)
|
||||
func (image *Image) RemoveMetadata(key string) error {
|
||||
if err := image.validate(imageIsOpen); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c_key := C.CString(key)
|
||||
defer C.free(unsafe.Pointer(c_key))
|
||||
|
||||
ret := C.rbd_metadata_remove(image.image, c_key)
|
||||
if ret < 0 {
|
||||
return RBDError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// int rbd_snap_remove(rbd_image_t image, const char *snapname);
|
||||
func (snapshot *Snapshot) Remove() error {
|
||||
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
||||
|
|
|
@ -1014,3 +1014,65 @@ func TestTrashImage(t *testing.T) {
|
|||
conn.DeletePool(poolname)
|
||||
conn.Shutdown()
|
||||
}
|
||||
|
||||
func TestImageMetadata(t *testing.T) {
|
||||
metadataKey := "mykey"
|
||||
metadataValue := "myvalue"
|
||||
|
||||
conn, _ := rados.NewConn()
|
||||
conn.ReadDefaultConfigFile()
|
||||
conn.Connect()
|
||||
|
||||
poolname := GetUUID()
|
||||
err := conn.MakePool(poolname)
|
||||
assert.NoError(t, err)
|
||||
|
||||
ioctx, err := conn.OpenIOContext(poolname)
|
||||
require.NoError(t, err)
|
||||
|
||||
name := GetUUID()
|
||||
image, err := Create(ioctx, name, 1<<22, 22)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Set a metadata key/value on unopen image
|
||||
err = image.SetMetadata(metadataKey, metadataValue)
|
||||
assert.Equal(t, err, ErrImageNotOpen)
|
||||
// Get the metadata value on unopen image
|
||||
value, err := image.GetMetadata(metadataKey)
|
||||
assert.Equal(t, err, ErrImageNotOpen)
|
||||
assert.Equal(t, "", value)
|
||||
// Remove the metadata key on unopen image
|
||||
err = image.RemoveMetadata(metadataKey)
|
||||
assert.Equal(t, err, ErrImageNotOpen)
|
||||
// check key is removed on unopen image
|
||||
value, err = image.GetMetadata(metadataKey)
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, err, ErrImageNotOpen)
|
||||
|
||||
err = image.Open()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Set a metadata key/value
|
||||
err = image.SetMetadata(metadataKey, metadataValue)
|
||||
assert.NoError(t, err)
|
||||
// Get the metadata value
|
||||
value, err = image.GetMetadata(metadataKey)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, metadataValue, value)
|
||||
// Remove the metadata key
|
||||
err = image.RemoveMetadata(metadataKey)
|
||||
assert.NoError(t, err)
|
||||
// check key is removed
|
||||
value, err = image.GetMetadata(metadataKey)
|
||||
assert.Equal(t, "", value)
|
||||
assert.Error(t, err)
|
||||
|
||||
err = image.Close()
|
||||
assert.NoError(t, err)
|
||||
err = image.Remove()
|
||||
assert.NoError(t, err)
|
||||
|
||||
ioctx.Destroy()
|
||||
conn.DeletePool(poolname)
|
||||
conn.Shutdown()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue