]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
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.
15 #include "common/entity_name.h"
16 #include "common/ceph_strings.h"
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" },
32 const std::string
& EntityName::
38 const char* EntityName::
41 return type_id
.c_str();
45 from_str(std::string_view s
)
47 size_t pos
= s
.find('.');
49 if (pos
== string::npos
)
52 auto type_
= s
.substr(0, pos
);
53 auto id_
= s
.substr(pos
+ 1);
60 set(uint32_t type_
, std::string_view id_
)
66 std::ostringstream oss
;
67 oss
<< ceph_entity_type_name(type_
) << "." << id_
;
75 set(std::string_view type_
, std::string_view id_
)
77 uint32_t t
= str_to_ceph_entity_type(type_
);
78 if (t
== CEPH_ENTITY_TYPE_ANY
)
85 set_type(uint32_t type_
)
91 set_type(std::string_view type_
)
93 return set(type_
, id
);
97 set_id(std::string_view id_
)
102 void EntityName::set_name(entity_name_t n
)
105 sprintf(s
, "%lld", (long long)n
.num());
109 const char* EntityName::
112 return ceph_entity_type_name(type
);
115 std::string_view
EntityName::
116 get_type_name() const
118 return ceph_entity_type_name(type
);
121 const std::string
&EntityName::
128 has_default_id() const
130 return (id
== "admin");
133 std::string
EntityName::
134 get_valid_types_as_str()
136 std::ostringstream out
;
138 for (i
= 0; i
< STR_TO_ENTITY_TYPE
.size(); ++i
) {
142 out
<< STR_TO_ENTITY_TYPE
[i
].str
;
147 uint32_t EntityName::str_to_ceph_entity_type(std::string_view s
)
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
;
154 return CEPH_ENTITY_TYPE_ANY
;
157 bool operator<(const EntityName
& a
, const EntityName
& b
)
159 return (a
.type
< b
.type
) || (a
.type
== b
.type
&& a
.id
< b
.id
);
162 std::ostream
& operator<<(std::ostream
& out
, const EntityName
& n
)
164 return out
<< n
.to_str();