]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/entity_name.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / common / entity_name.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "common/entity_name.h"
16 #include "common/ceph_strings.h"
17
18 #include <sstream>
19
20 using std::string;
21
22
23 const std::array<EntityName::str_to_entity_type_t, 6> EntityName::STR_TO_ENTITY_TYPE = {{
24 { CEPH_ENTITY_TYPE_AUTH, "auth" },
25 { CEPH_ENTITY_TYPE_MON, "mon" },
26 { CEPH_ENTITY_TYPE_OSD, "osd" },
27 { CEPH_ENTITY_TYPE_MDS, "mds" },
28 { CEPH_ENTITY_TYPE_MGR, "mgr" },
29 { CEPH_ENTITY_TYPE_CLIENT, "client" },
30 }};
31
32 const std::string& EntityName::
33 to_str() const
34 {
35 return type_id;
36 }
37
38 const char* EntityName::
39 to_cstr() const
40 {
41 return type_id.c_str();
42 }
43
44 bool EntityName::
45 from_str(std::string_view s)
46 {
47 size_t pos = s.find('.');
48
49 if (pos == string::npos)
50 return false;
51
52 auto type_ = s.substr(0, pos);
53 auto id_ = s.substr(pos + 1);
54 if (set(type_, id_))
55 return false;
56 return true;
57 }
58
59 void EntityName::
60 set(uint32_t type_, std::string_view id_)
61 {
62 type = type_;
63 id = id_;
64
65 if (type) {
66 std::ostringstream oss;
67 oss << ceph_entity_type_name(type_) << "." << id_;
68 type_id = oss.str();
69 } else {
70 type_id.clear();
71 }
72 }
73
74 int EntityName::
75 set(std::string_view type_, std::string_view id_)
76 {
77 uint32_t t = str_to_ceph_entity_type(type_);
78 if (t == CEPH_ENTITY_TYPE_ANY)
79 return -EINVAL;
80 set(t, id_);
81 return 0;
82 }
83
84 void EntityName::
85 set_type(uint32_t type_)
86 {
87 set(type_, id);
88 }
89
90 int EntityName::
91 set_type(std::string_view type_)
92 {
93 return set(type_, id);
94 }
95
96 void EntityName::
97 set_id(std::string_view id_)
98 {
99 set(type, id_);
100 }
101
102 void EntityName::set_name(entity_name_t n)
103 {
104 char s[40];
105 sprintf(s, "%lld", (long long)n.num());
106 set(n.type(), s);
107 }
108
109 const char* EntityName::
110 get_type_str() const
111 {
112 return ceph_entity_type_name(type);
113 }
114
115 std::string_view EntityName::
116 get_type_name() const
117 {
118 return ceph_entity_type_name(type);
119 }
120
121 const std::string &EntityName::
122 get_id() const
123 {
124 return id;
125 }
126
127 bool EntityName::
128 has_default_id() const
129 {
130 return (id == "admin");
131 }
132
133 std::string EntityName::
134 get_valid_types_as_str()
135 {
136 std::ostringstream out;
137 size_t i;
138 for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) {
139 if (i > 0) {
140 out << ", ";
141 }
142 out << STR_TO_ENTITY_TYPE[i].str;
143 }
144 return out.str();
145 }
146
147 uint32_t EntityName::str_to_ceph_entity_type(std::string_view s)
148 {
149 size_t i;
150 for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) {
151 if (s == STR_TO_ENTITY_TYPE[i].str)
152 return STR_TO_ENTITY_TYPE[i].type;
153 }
154 return CEPH_ENTITY_TYPE_ANY;
155 }
156
157 bool operator<(const EntityName& a, const EntityName& b)
158 {
159 return (a.type < b.type) || (a.type == b.type && a.id < b.id);
160 }
161
162 std::ostream& operator<<(std::ostream& out, const EntityName& n)
163 {
164 return out << n.to_str();
165 }