From: songweibin Date: Sat, 24 Feb 2018 02:44:30 +0000 (+0800) Subject: rbd: add 'group rename' action to CLI and Python API X-Git-Tag: v13.0.2~165^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F20577%2Fhead;p=ceph.git rbd: add 'group rename' action to CLI and Python API Signed-off-by: songweibin --- diff --git a/doc/man/8/rbd.rst b/doc/man/8/rbd.rst index 95a35ab8e1cd..1f2ba758f0e7 100644 --- a/doc/man/8/rbd.rst +++ b/doc/man/8/rbd.rst @@ -282,6 +282,9 @@ Commands :command:`group ls` [-p | --pool *pool-name*] List rbd groups. +:command:`group rename` *src-group-spec* *dest-group-spec* + Rename a group. Note: rename across pools is not supported. + :command:`group rm` *group-spec* Delete a group. diff --git a/qa/workunits/rbd/rbd_groups.sh b/qa/workunits/rbd/rbd_groups.sh index 3b12d206449d..c41389d549fc 100755 --- a/qa/workunits/rbd/rbd_groups.sh +++ b/qa/workunits/rbd/rbd_groups.sh @@ -33,6 +33,14 @@ remove_group() rbd group remove $group_name } +rename_group() +{ + local src_name=$1 + local dest_name=$2 + + rbd group rename $src_name $dest_name +} + check_group_does_not_exist() { local group_name=$1 @@ -145,10 +153,13 @@ check_snapshot_not_in_group() echo "TEST: create remove consistency group" group="test_consistency_group" +new_group="test_new_consistency_group" create_group $group check_group_exists $group -remove_group $group -check_group_does_not_exist $group +rename_group $group $new_group +check_group_exists $new_group +remove_group $new_group +check_group_does_not_exist $new_group echo "PASSED" echo "TEST: add remove images to consistency group" diff --git a/src/pybind/rbd/rbd.pyx b/src/pybind/rbd/rbd.pyx index 8da4e6514573..14f77caa6874 100644 --- a/src/pybind/rbd/rbd.pyx +++ b/src/pybind/rbd/rbd.pyx @@ -413,6 +413,7 @@ cdef extern from "rbd/librbd.h" nogil: int rbd_group_create(rados_ioctx_t p, const char *name) int rbd_group_remove(rados_ioctx_t p, const char *name) int rbd_group_list(rados_ioctx_t p, char *names, size_t *size) + int rbd_group_rename(rados_ioctx_t p, const char *src, const char *dest) void rbd_group_info_cleanup(rbd_group_info_t *group_info, size_t group_info_size) int rbd_group_image_add(rados_ioctx_t group_p, const char *group_name, @@ -1410,6 +1411,32 @@ class RBD(object): finally: free(c_names) + def group_rename(self, ioctx, src, dest): + """ + Rename an RBD group. + + :param ioctx: determines which RADOS pool the group is in + :type ioctx: :class:`rados.Ioctx` + :param src: the current name of the group + :type src: str + :param dest: the new name of the group + :type dest: str + :raises: :class:`ObjectExists` + :raises: :class:`ObjectNotFound` + :raises: :class:`InvalidArgument` + :raises: :class:`FunctionNotSupported` + """ + src = cstr(src, 'src') + dest = cstr(dest, 'dest') + cdef: + rados_ioctx_t _ioctx = convert_ioctx(ioctx) + char *_src = src + char *_dest = dest + with nogil: + ret = rbd_group_rename(_ioctx, _src, _dest) + if ret != 0: + raise make_ex(ret, 'error renaming group') + cdef class MirrorPeerIterator(object): """ Iterator over mirror peer info for a pool. diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index 231321fa5a53..f87d3347c614 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -30,6 +30,7 @@ group image remove (group image rm) Remove an image from a group. group list (group ls) List rbd groups. group remove (group rm) Delete a group. + group rename Rename a group within pool. group snap create Make a snapshot of a group. group snap list (group snap ls) List snapshots of a group. group snap remove (group snap rm) Remove a snapshot from a group. @@ -632,6 +633,25 @@ -p [ --pool ] arg pool name --group arg group name + rbd help group rename + usage: rbd group rename [--pool ] [--group ] + [--dest-pool ] [--dest-group ] + + + Rename a group within pool. + + Positional arguments + source group specification + (example: [/]) + destination group specification + (example: [/]) + + Optional arguments + -p [ --pool ] arg source pool name + --group arg source group name + --dest-pool arg destination pool name + --dest-group arg destination group name + rbd help group snap create usage: rbd group snap create [--pool ] [--group ] [--snap ] diff --git a/src/test/pybind/test_rbd.py b/src/test/pybind/test_rbd.py index 3a71bee7abf9..6ce6c32c9d5c 100644 --- a/src/test/pybind/test_rbd.py +++ b/src/test/pybind/test_rbd.py @@ -101,6 +101,10 @@ def remove_group(): if group_name is not None: RBD().group_remove(ioctx, group_name) +def rename_group(): + new_group_name = "new" + group_name + RBD().group_rename(ioctx, group_name, new_group_name) + def require_new_format(): def wrapper(fn): def _require_new_format(*args, **kwargs): @@ -1719,6 +1723,15 @@ def test_create_group(): create_group() remove_group() +def test_rename_group(): + create_group() + if group_name is not None: + rename_group() + eq(["new" + group_name], RBD().group_list(ioctx)) + RBD().group_remove(ioctx, "new" + group_name) + else: + remove_group() + def test_list_groups_empty(): eq([], RBD().group_list(ioctx)) diff --git a/src/tools/rbd/action/Group.cc b/src/tools/rbd/action/Group.cc index 1d77cef5739d..04217f7bcce7 100644 --- a/src/tools/rbd/action/Group.cc +++ b/src/tools/rbd/action/Group.cc @@ -124,6 +124,57 @@ int execute_remove(const po::variables_map &vm, return 0; } +int execute_rename(const po::variables_map &vm, + const std::vector &ceph_global_init_args) { + size_t arg_index = 0; + + std::string group_name; + std::string pool_name; + + int r = utils::get_pool_group_names(vm, at::ARGUMENT_MODIFIER_NONE, + &arg_index, &pool_name, &group_name, + nullptr); + if (r < 0) { + return r; + } + + std::string dest_group_name; + std::string dest_pool_name; + + r = utils::get_pool_group_names(vm, at::ARGUMENT_MODIFIER_NONE, + &arg_index, &dest_pool_name, + &dest_group_name, nullptr); + if (r < 0) { + return r; + } + + if (pool_name != dest_pool_name) { + std::cerr << "rbd: group rename across pools not supported" << std::endl + << "source pool: " << pool_name<< ", dest pool: " << dest_pool_name + << std::endl; + return -EINVAL; + } + + librados::Rados rados; + librados::IoCtx io_ctx; + r = utils::init(pool_name, &rados, &io_ctx); + if (r < 0) { + return r; + } + + librbd::RBD rbd; + r = rbd.group_rename(io_ctx, group_name.c_str(), + dest_group_name.c_str()); + + if (r < 0) { + std::cerr << "rbd: failed to rename group: " + << cpp_strerror(r) << std::endl; + return r; + } + + return 0; +} + int execute_add(const po::variables_map &vm, const std::vector &ceph_global_init_args) { size_t arg_index = 0; @@ -560,6 +611,14 @@ void get_list_arguments(po::options_description *positional, at::add_format_options(options); } +void get_rename_arguments(po::options_description *positional, + po::options_description *options) { + at::add_group_spec_options(positional, options, at::ARGUMENT_MODIFIER_SOURCE, + false); + at::add_group_spec_options(positional, options, at::ARGUMENT_MODIFIER_DEST, + false); +} + void get_add_arguments(po::options_description *positional, po::options_description *options) { positional->add_options() @@ -651,6 +710,9 @@ Shell::Action action_remove( Shell::Action action_list( {"group", "list"}, {"group", "ls"}, "List rbd groups.", "", &get_list_arguments, &execute_list); +Shell::Action action_rename( + {"group", "rename"}, {}, "Rename a group within pool.", + "", &get_rename_arguments, &execute_rename); Shell::Action action_add( {"group", "image", "add"}, {}, "Add an image to a group.", "", &get_add_arguments, &execute_add);