]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/entity_name.cc
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / common / entity_name.cc
CommitLineData
7c673cae
FG
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"
9f95a23c 16#include "common/ceph_strings.h"
7c673cae 17
7c673cae 18#include <sstream>
7c673cae
FG
19
20using std::string;
21
7c673cae 22
11fdf7f2 23const std::array<EntityName::str_to_entity_type_t, 6> EntityName::STR_TO_ENTITY_TYPE = {{
7c673cae
FG
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" },
11fdf7f2 30}};
7c673cae
FG
31
32const std::string& EntityName::
33to_str() const
34{
35 return type_id;
36}
37
38const char* EntityName::
39to_cstr() const
40{
41 return type_id.c_str();
42}
43
44bool EntityName::
f67539c2 45from_str(std::string_view s)
7c673cae
FG
46{
47 size_t pos = s.find('.');
48
49 if (pos == string::npos)
50 return false;
f67539c2
TL
51
52 auto type_ = s.substr(0, pos);
53 auto id_ = s.substr(pos + 1);
7c673cae
FG
54 if (set(type_, id_))
55 return false;
56 return true;
57}
58
59void EntityName::
f67539c2 60set(uint32_t type_, std::string_view id_)
7c673cae
FG
61{
62 type = type_;
63 id = id_;
64
31f18b77
FG
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 }
7c673cae
FG
72}
73
74int EntityName::
f67539c2 75set(std::string_view type_, std::string_view id_)
7c673cae 76{
f67539c2 77 uint32_t t = str_to_ceph_entity_type(type_);
7c673cae
FG
78 if (t == CEPH_ENTITY_TYPE_ANY)
79 return -EINVAL;
80 set(t, id_);
81 return 0;
82}
83
84void EntityName::
85set_type(uint32_t type_)
86{
87 set(type_, id);
88}
89
90int EntityName::
f67539c2 91set_type(std::string_view type_)
7c673cae
FG
92{
93 return set(type_, id);
94}
95
96void EntityName::
f67539c2 97set_id(std::string_view id_)
7c673cae
FG
98{
99 set(type, id_);
100}
101
102void 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
20effc67 109const char* EntityName::
7c673cae
FG
110get_type_str() const
111{
112 return ceph_entity_type_name(type);
113}
114
f67539c2 115std::string_view EntityName::
7c673cae
FG
116get_type_name() const
117{
118 return ceph_entity_type_name(type);
119}
120
121const std::string &EntityName::
122get_id() const
123{
124 return id;
125}
126
127bool EntityName::
128has_default_id() const
129{
130 return (id == "admin");
131}
132
133std::string EntityName::
134get_valid_types_as_str()
135{
11fdf7f2
TL
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
147uint32_t EntityName::str_to_ceph_entity_type(std::string_view s)
148{
7c673cae 149 size_t i;
11fdf7f2
TL
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;
7c673cae 153 }
11fdf7f2 154 return CEPH_ENTITY_TYPE_ANY;
7c673cae
FG
155}
156
157bool operator<(const EntityName& a, const EntityName& b)
158{
159 return (a.type < b.type) || (a.type == b.type && a.id < b.id);
160}
161
162std::ostream& operator<<(std::ostream& out, const EntityName& n)
163{
164 return out << n.to_str();
165}