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);
}
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);
}
}
#include "common/entity_name.h"
#include "include/msgr.h"
+#include <errno.h>
#include <sstream>
#include <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)
string type_ = s.substr(0, pos);
string id_ = s.substr(pos + 1);
- set(type_, id_);
+ if (set(type_, id_))
+ return false;
return true;
}
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::
set(type_, id);
}
-void EntityName::
+int EntityName::
set_type(const char *type_)
{
- set(type_, id);
+ return set(type_, id);
}
void EntityName::
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);
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;
}
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;
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);