From aff5ac7c2e3b6687738473f1763f06a3cc04740b Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Tue, 1 Dec 2020 19:32:46 -0500 Subject: [PATCH] librbd/migration: simplify the native format JSON spec Allow the pool id integer to be specified via a string, support an optional pool name alternative, and make the pool namespace optional as well. Signed-off-by: Jason Dillaman --- src/librbd/migration/NativeFormat.cc | 43 ++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/librbd/migration/NativeFormat.cc b/src/librbd/migration/NativeFormat.cc index e15e2820f38fa..91846f61d284a 100644 --- a/src/librbd/migration/NativeFormat.cc +++ b/src/librbd/migration/NativeFormat.cc @@ -10,6 +10,7 @@ #include "librbd/asio/ContextWQ.h" #include "librbd/io/ImageDispatchSpec.h" #include "json_spirit/json_spirit.h" +#include "boost/lexical_cast.hpp" #include #define dout_subsys ceph_subsys_rbd @@ -24,6 +25,7 @@ namespace { const std::string TYPE_KEY{"type"}; const std::string POOL_ID_KEY{"pool_id"}; +const std::string POOL_NAME_KEY{"pool_name"}; const std::string POOL_NAMESPACE_KEY{"pool_namespace"}; const std::string IMAGE_NAME_KEY{"image_name"}; const std::string IMAGE_ID_KEY{"image_id"}; @@ -57,21 +59,52 @@ void NativeFormat::open(Context* on_finish) { auto cct = m_image_ctx->cct; ldout(cct, 10) << dendl; + auto& pool_name_val = m_json_object[POOL_NAME_KEY]; + if (pool_name_val.type() == json_spirit::str_type) { + librados::Rados rados(m_image_ctx->md_ctx); + librados::IoCtx io_ctx; + int r = rados.ioctx_create(pool_name_val.get_str().c_str(), io_ctx); + if (r < 0 ) { + lderr(cct) << "invalid pool name" << dendl; + on_finish->complete(r); + return; + } + + m_pool_id = io_ctx.get_id(); + } else if (pool_name_val.type() != json_spirit::null_type) { + lderr(cct) << "invalid pool name" << dendl; + on_finish->complete(-EINVAL); + return; + } + auto& pool_id_val = m_json_object[POOL_ID_KEY]; - if (pool_id_val.type() != json_spirit::int_type) { + if (m_pool_id != -1 && pool_id_val.type() != json_spirit::null_type) { + lderr(cct) << "cannot specify both pool name and pool id" << dendl; + on_finish->complete(-EINVAL); + return; + } else if (pool_id_val.type() == json_spirit::int_type) { + m_pool_id = pool_id_val.get_int64(); + } else if (pool_id_val.type() == json_spirit::str_type) { + try { + m_pool_id = boost::lexical_cast(pool_id_val.get_str()); + } catch (boost::bad_lexical_cast &) { + } + } + + if (m_pool_id == -1) { lderr(cct) << "missing or invalid pool id" << dendl; on_finish->complete(-EINVAL); return; } - m_pool_id = pool_id_val.get_int64(); auto& pool_namespace_val = m_json_object[POOL_NAMESPACE_KEY]; - if (pool_namespace_val.type() != json_spirit::str_type) { - lderr(cct) << "missing or invalid pool namespace" << dendl; + if (pool_namespace_val.type() == json_spirit::str_type) { + m_pool_namespace = pool_namespace_val.get_str(); + } else if (pool_namespace_val.type() != json_spirit::null_type) { + lderr(cct) << "invalid pool namespace" << dendl; on_finish->complete(-EINVAL); return; } - m_pool_namespace = pool_namespace_val.get_str(); auto& image_name_val = m_json_object[IMAGE_NAME_KEY]; if (image_name_val.type() != json_spirit::str_type) { -- 2.39.5