Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Children.cc
1 // -*- mode:C++; tab-width:8; c-basic-offsset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "common/errno.h"
8 #include "common/Formatter.h"
9 #include <iostream>
10 #include <boost/program_options.hpp>
11
12 namespace rbd {
13 namespace action {
14 namespace children {
15
16 namespace at = argument_types;
17 namespace po = boost::program_options;
18
19 int do_list_children(librbd::Image &image, Formatter *f)
20 {
21   std::set<std::pair<std::string, std::string> > children;
22   int r;
23
24   r = image.list_children(&children);
25   if (r < 0)
26     return r;
27
28   if (f)
29     f->open_array_section("children");
30
31   for (auto &child_it : children) {
32     if (f) {
33       f->open_object_section("child");
34       f->dump_string("pool", child_it.first);
35       f->dump_string("image", child_it.second);
36       f->close_section();
37     } else {
38       std::cout << child_it.first << "/" << child_it.second << std::endl;
39     }
40   }
41
42   if (f) {
43     f->close_section();
44     f->flush(std::cout);
45   }
46
47   return 0;
48 }
49
50 void get_arguments(po::options_description *positional,
51                    po::options_description *options) {
52   at::add_snap_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
53   at::add_format_options(options);
54 }
55
56 int execute(const po::variables_map &vm) {
57   size_t arg_index = 0;
58   std::string pool_name;
59   std::string image_name;
60   std::string snap_name;
61   int r = utils::get_pool_image_snapshot_names(
62     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
63     &snap_name, utils::SNAPSHOT_PRESENCE_REQUIRED, utils::SPEC_VALIDATION_NONE);
64   if (r < 0) {
65     return r;
66   }
67
68   at::Format::Formatter formatter;
69   r = utils::get_formatter(vm, &formatter);
70   if (r < 0) {
71     return r;
72   }
73
74   librados::Rados rados;
75   librados::IoCtx io_ctx;
76   librbd::Image image;
77   r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
78                                  &rados, &io_ctx, &image);
79   if (r < 0) {
80     return r;
81   }
82
83   r = do_list_children(image, formatter.get());
84   if (r < 0) {
85     std::cerr << "rbd: listing children failed: " << cpp_strerror(r)
86               << std::endl;
87     return r;
88   }
89   return 0;
90 }
91
92 Shell::Action action(
93   {"children"}, {}, "Display children of snapshot.", "", &get_arguments,
94   &execute);
95
96 } // namespace children
97 } // namespace action
98 } // namespace rbd