]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
rbd: added image-id optional to Info command
authorRicardo Dias <rdias@suse.com>
Fri, 24 Mar 2017 13:50:52 +0000 (13:50 +0000)
committerRicardo Dias <rdias@suse.com>
Tue, 11 Apr 2017 11:09:41 +0000 (12:09 +0100)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/test/cli/rbd/help.t
src/tools/rbd/action/Info.cc

index 0eb9fa935f79da56e1faac0316274871ae28cbb3..8d4d5f59c3c4308ef5482d04a74ed15cf43fc709 100644 (file)
   
   rbd help info
   usage: rbd info [--pool <pool>] [--image <image>] [--snap <snap>] 
-                  [--format <format>] [--pretty-format] 
+                  [--image-id <image-id>] [--format <format>] [--pretty-format] 
                   <image-or-snap-spec> 
   
   Show information about image size, striping, etc.
     -p [ --pool ] arg     pool name
     --image arg           image name
     --snap arg            snapshot name
+    --image-id arg        image id
     --format arg          output format [plain, json, or xml]
     --pretty-format       pretty formatting (json and xml)
   
index 59ce6c9aefecfa78ae9f15361d7b91aae4ba2484..3bfdeeb7d67a5f7621b845b64c70f76b2d34a782 100644 (file)
@@ -65,7 +65,8 @@ static void format_flags(Formatter *f, uint64_t flags)
 }
 
 static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
-                        const char *imgname, const char *snapname, Formatter *f)
+                        const std::string &imgname, const std::string &imgid,
+                        const std::string &snapname, Formatter *f)
 {
   librbd::image_info_t info;
   std::string parent_pool, parent_name, parent_snapname;
@@ -111,8 +112,8 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
     return r;
   }
 
-  if (snapname) {
-    r = image.snap_is_protected(snapname, &snap_protected);
+  if (!snapname.empty()) {
+    r = image.snap_is_protected(snapname.c_str(), &snap_protected);
     if (r < 0)
       return r;
   }
@@ -142,7 +143,11 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
 
   if (f) {
     f->open_object_section("image");
-    f->dump_string("name", imgname);
+    if (!imgname.empty()) {
+      f->dump_string("name", imgname);
+    } else {
+      f->dump_string("id", imgid);
+    }
     f->dump_unsigned("size", info.size);
     f->dump_unsigned("objects", info.num_objs);
     f->dump_int("order", info.order);
@@ -153,7 +158,7 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
     f->dump_string("block_name_prefix", prefix);
     f->dump_int("format", (old_format ? 1 : 2));
   } else {
-    std::cout << "rbd image '" << imgname << "':\n"
+    std::cout << "rbd image '" << (imgname.empty() ? imgid : imgname) << "':\n"
               << "\tsize " << prettybyte_t(info.size) << " in "
               << info.num_objs << " objects"
               << std::endl
@@ -184,7 +189,7 @@ static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
   }
 
   // snapshot info, if present
-  if (snapname) {
+  if (!snapname.empty()) {
     if (f) {
       f->dump_string("protected", snap_protected ? "true" : "false");
     } else {
@@ -272,6 +277,7 @@ void get_arguments(po::options_description *positional,
                    po::options_description *options) {
   at::add_image_or_snap_spec_options(positional, options,
                                      at::ARGUMENT_MODIFIER_NONE);
+  at::add_image_id_option(options);
   at::add_format_options(options);
 }
 
@@ -280,10 +286,34 @@ int execute(const po::variables_map &vm) {
   std::string pool_name;
   std::string image_name;
   std::string snap_name;
-  int r = utils::get_pool_image_snapshot_names(
-    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
-    &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
-    utils::SPEC_VALIDATION_NONE);
+  std::string image_id;
+
+  if (vm.count(at::IMAGE_ID)) {
+    image_id = vm[at::IMAGE_ID].as<std::string>();
+  }
+
+  bool has_image_spec = utils::check_if_image_spec_present(
+      vm, at::ARGUMENT_MODIFIER_NONE, arg_index);
+
+  if (!image_id.empty() && has_image_spec) {
+    std::cerr << "rbd: trying to access image using both name and id. "
+              << std::endl;
+    return -EINVAL;
+  }
+
+  int r;
+  if (image_id.empty()) {
+    r = utils::get_pool_image_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE,
+                                             &arg_index, &pool_name,
+                                             &image_name, &snap_name,
+                                             utils::SNAPSHOT_PRESENCE_PERMITTED,
+                                             utils::SPEC_VALIDATION_NONE);
+  } else {
+    r = utils::get_pool_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE,
+                                       &arg_index, &pool_name, &snap_name,
+                                       utils::SNAPSHOT_PRESENCE_PERMITTED,
+                                       utils::SPEC_VALIDATION_NONE);
+  }
   if (r < 0) {
     return r;
   }
@@ -297,14 +327,13 @@ int execute(const po::variables_map &vm) {
   librados::Rados rados;
   librados::IoCtx io_ctx;
   librbd::Image image;
-  r = utils::init_and_open_image(pool_name, image_name, snap_name, true,
-                                 &rados, &io_ctx, &image);
+  r = utils::init_and_open_image(pool_name, image_name, image_id, snap_name,
+                                 true, &rados, &io_ctx, &image);
   if (r < 0) {
     return r;
   }
 
-  r = do_show_info(io_ctx, image, image_name.c_str(),
-                   snap_name.empty() ? nullptr : snap_name.c_str(),
+  r = do_show_info(io_ctx, image, image_name, image_id, snap_name,
                    formatter.get());
   if (r < 0) {
     std::cerr << "rbd: info: " << cpp_strerror(r) << std::endl;