Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / cephfs / RoleSelector.cc
1
2 #include "RoleSelector.h"
3
4 int MDSRoleSelector::parse_rank(
5     const FSMap &fsmap,
6     std::string const &str)
7 {
8   if (str == "all" || str == "*") {
9     std::set<mds_rank_t> in;
10     const MDSMap &mds_map = fsmap.get_filesystem(fscid)->mds_map;
11     mds_map.get_mds_set(in);
12
13     for (auto rank : in) {
14       roles.push_back(mds_role_t(fscid, rank));
15     }
16
17     return 0;
18   } else {
19     std::string rank_err;
20     mds_rank_t rank = strict_strtol(str.c_str(), 10, &rank_err);
21     if (!rank_err.empty()) {
22       return -EINVAL;
23     }
24     if (fsmap.get_filesystem(fscid)->mds_map.is_dne(rank)) {
25       return -ENOENT;
26     }
27     roles.push_back(mds_role_t(fscid, rank));
28     return 0;
29   }
30 }
31
32 int MDSRoleSelector::parse(const FSMap &fsmap, std::string const &str)
33 {
34   auto colon_pos = str.find(":");
35   if (colon_pos == std::string::npos) {
36     // An unqualified rank.  Only valid if there is only one
37     // namespace.
38     if (fsmap.filesystem_count() == 1) {
39       fscid = fsmap.get_filesystem()->fscid;
40       return parse_rank(fsmap, str);
41     } else {
42       return -EINVAL;
43     }
44   } else if (colon_pos == 0 || colon_pos == str.size() - 1) {
45     return -EINVAL;
46   } else {
47     const std::string ns_str = str.substr(0, colon_pos);
48     const std::string rank_str = str.substr(colon_pos + 1);
49     std::shared_ptr<const Filesystem> fs_ptr;
50     int r = fsmap.parse_filesystem(ns_str, &fs_ptr);
51     if (r != 0) {
52       return r;
53     }
54     fscid = fs_ptr->fscid;
55     return parse_rank(fsmap, rank_str);
56   }
57 }
58