]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
config: complain if --name gives an invalid type
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 6 Apr 2011 19:32:39 +0000 (12:32 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 6 Apr 2011 19:35:38 +0000 (12:35 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/ceph_argparse.cc
src/common/entity_name.cc
src/common/entity_name.h
src/test/cli/cconf/invalid-args.t

index 0cc7288f87605cfb57b60aa44df2517d97f4a102..9583a5d5af1edb9387c5cb90a2b9d337a25ffd90 100644 (file)
@@ -324,7 +324,7 @@ bool ceph_argparse_witharg(std::vector<const char*> &args,
       else if (first[strlen_a] == '\0') {
        // find second part (or not)
        if (i+1 == args.end()) {
-         std::cerr << "Option " << *i << " requires an argument." << std::endl;
+         cerr << "Option " << *i << " requires an argument." << std::endl;
          _exit(1);
        }
        i = args.erase(i);
@@ -362,8 +362,9 @@ CephInitParameters ceph_argparse_early_args
     }
     else if (ceph_argparse_witharg(args, i, &val, "--name", "-n", (char*)NULL)) {
       if (!iparams.name.from_str(val)) {
-       std::cerr << "You must pass a string of the form TYPE.ID to "
-         "the --name option." << std::endl;
+       cerr << "You must pass a string of the form TYPE.ID to "
+         << "the --name option. Valid types are: "
+         << EntityName::get_valid_types_as_str() << std::endl;
        _exit(1);
       }
     }
index 2864a0c6bd12183fde0ed822c4b125f7c37af753..6287e550dae7ccf2d69863f3a9a4ff8ea4a94444 100644 (file)
@@ -15,6 +15,7 @@
 #include "common/entity_name.h"
 #include "include/msgr.h"
 
+#include <errno.h>
 #include <sstream>
 #include <string>
 
@@ -22,6 +23,19 @@ using std::string;
 
 extern const char *ceph_entity_type_name(int type);
 
+struct str_to_entity_type_t {
+  uint32_t type;
+  const char *str;
+};
+
+static const str_to_entity_type_t STR_TO_ENTITY_TYPE[] = {
+  { CEPH_ENTITY_TYPE_AUTH, "auth" },
+  { CEPH_ENTITY_TYPE_MON, "mon" },
+  { CEPH_ENTITY_TYPE_OSD, "osd" },
+  { CEPH_ENTITY_TYPE_MDS, "mds" },
+  { CEPH_ENTITY_TYPE_CLIENT, "client" },
+};
+
 EntityName::
 EntityName()
   : type(0)
@@ -50,7 +64,8 @@ from_str(const string& s)
  
   string type_ = s.substr(0, pos);
   string id_ = s.substr(pos + 1);
-  set(type_, id_);
+  if (set(type_, id_))
+    return false;
   return true;
 }
 
@@ -65,10 +80,14 @@ set(uint32_t type_, const std::string &id_)
   type_id = oss.str();
 }
 
-void EntityName::
+int EntityName::
 set(const std::string &type_, const std::string &id_)
 {
-  set(str_to_ceph_entity_type(type_.c_str()), id_);
+  uint32_t t = str_to_ceph_entity_type(type_.c_str());
+  if (t == CEPH_ENTITY_TYPE_ANY)
+    return -EINVAL;
+  set(t, id_);
+  return 0;
 }
 
 void EntityName::
@@ -77,10 +96,10 @@ set_type(uint32_t type_)
   set(type_, id);
 }
 
-void EntityName::
+int EntityName::
 set_type(const char *type_)
 {
-  set(type_, id);
+  return set(type_, id);
 }
 
 void EntityName::
@@ -125,6 +144,20 @@ has_default_id() const
   return (id == "admin");
 }
 
+std::string EntityName::
+get_valid_types_as_str()
+{
+  std::string out;
+  size_t i;
+  std::string sep("");
+  for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) {
+    out += sep;
+    out += STR_TO_ENTITY_TYPE[i].str;
+    sep = ", ";
+  }
+  return out;
+}
+
 bool operator<(const EntityName& a, const EntityName& b)
 {
   return (a.type < b.type) || (a.type == b.type && a.id < b.id);
@@ -137,15 +170,10 @@ std::ostream& operator<<(std::ostream& out, const EntityName& n)
 
 uint32_t str_to_ceph_entity_type(const char * str)
 {
-  if (strcmp(str, "auth") == 0) {
-    return CEPH_ENTITY_TYPE_AUTH;
-  } else if (strcmp(str, "mon") == 0) {
-    return CEPH_ENTITY_TYPE_MON;
-  } else if (strcmp(str, "osd") == 0) {
-    return CEPH_ENTITY_TYPE_OSD;
-  } else if (strcmp(str, "mds") == 0) {
-    return CEPH_ENTITY_TYPE_MDS;
-  } else {
-    return CEPH_ENTITY_TYPE_CLIENT;
+  size_t i;
+  for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) {
+    if (strcmp(str, STR_TO_ENTITY_TYPE[i].str) == 0)
+      return STR_TO_ENTITY_TYPE[i].type;
   }
+  return CEPH_ENTITY_TYPE_ANY;
 }
index cfc7278924ab0a4814109d10f4fb159da974b517..b2d4a67d6a7274d666dc98fc433a3bbd9afd547c 100644 (file)
@@ -47,9 +47,9 @@ struct EntityName
   const char *to_cstr() const;
   bool from_str(const std::string& s);
   void set(uint32_t type_, const std::string &id_);
-  void set(const std::string &type_, const std::string &id_);
+  int set(const std::string &type_, const std::string &id_);
   void set_type(uint32_t type_);
-  void set_type(const char *type);
+  int set_type(const char *type);
   void set_id(const std::string &id_);
 
   const char* get_type_str() const;
@@ -59,6 +59,8 @@ struct EntityName
   const std::string &get_id() const;
   bool has_default_id() const;
 
+  static std::string get_valid_types_as_str();
+
   friend bool operator<(const EntityName& a, const EntityName& b);
   friend std::ostream& operator<<(std::ostream& out, const EntityName& n);
 
index eeff0944077f9576e31a098614e019634993c692..d1247fc9f48c5b8aa2acaaf4b570cf7fd969d249 100644 (file)
@@ -7,6 +7,10 @@
   $ cconf -c test.conf broken
   [1]
 
+  $ cconf -c test.conf --name total.garbage
+  You must pass a string of the form TYPE.ID to the --name option. Valid types are: auth, mon, osd, mds, client
+  [1]
+
 # TODO output an error (missing key), not the whole usage
   $ cconf -c test.conf -s bar
   lookup: expected exactly one argument