summaryrefslogtreecommitdiffstats
path: root/src/common/entity_name.cc
blob: 6287e550dae7ccf2d69863f3a9a4ff8ea4a94444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/entity_name.h"
#include "include/msgr.h"

#include <errno.h>
#include <sstream>
#include <string>

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)
{
}

const std::string& EntityName::
to_str() const
{
  return type_id;
}

const char* EntityName::
to_cstr() const
{
  return type_id.c_str();
}

bool EntityName::
from_str(const string& s)
{
  int pos = s.find('.');

  if (pos < 0)
    return false;
 
  string type_ = s.substr(0, pos);
  string id_ = s.substr(pos + 1);
  if (set(type_, id_))
    return false;
  return true;
}

void EntityName::
set(uint32_t type_, const std::string &id_)
{
  type = type_;
  id = id_;

  std::ostringstream oss;
  oss << ceph_entity_type_name(type_) << "." << id_;
  type_id = oss.str();
}

int EntityName::
set(const std::string &type_, const std::string &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(uint32_t type_)
{
  set(type_, id);
}

int EntityName::
set_type(const char *type_)
{
  return set(type_, id);
}

void EntityName::
set_id(const std::string &id_)
{
  set(type, id_);
}

const char* EntityName::
get_type_str() const
{
  return ceph_entity_type_name(type);
}

bool EntityName::
is_admin() const
{
  return (id.compare("admin") == 0);
}

uint32_t EntityName::
get_type() const
{
  return type;
}

const char *EntityName::
get_type_name() const
{
  return ceph_entity_type_name(type);
}

const std::string &EntityName::
get_id() const
{
  return id;
}

bool EntityName::
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);
}

std::ostream& operator<<(std::ostream& out, const EntityName& n)
{
  return out << n.to_str();
}

uint32_t str_to_ceph_entity_type(const char * str)
{
  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;
}