]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/entity_name.h
import ceph quincy 17.2.4
[ceph.git] / ceph / src / common / entity_name.h
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 #ifndef CEPH_COMMON_ENTITY_NAME_H
16 #define CEPH_COMMON_ENTITY_NAME_H
17
18 #include <string_view>
19
20 #include <ifaddrs.h>
21
22 #include "msg/msg_types.h"
23
24 /* Represents a Ceph entity name.
25 *
26 * For example, mds.0 is the name of the first metadata server.
27 * client
28 */
29 struct EntityName
30 {
31 void encode(ceph::buffer::list& bl) const {
32 using ceph::encode;
33 encode(type, bl);
34 encode(id, bl);
35 }
36 void decode(ceph::buffer::list::const_iterator& bl) {
37 using ceph::decode;
38 uint32_t type_;
39 std::string id_;
40 decode(type_, bl);
41 decode(id_, bl);
42 set(type_, id_);
43 }
44
45 const std::string& to_str() const;
46 const char *to_cstr() const;
47 bool from_str(std::string_view s);
48 void set(uint32_t type_, std::string_view id_);
49 int set(std::string_view type_, std::string_view id_);
50 void set_type(uint32_t type_);
51 int set_type(std::string_view type);
52 void set_id(std::string_view id_);
53 void set_name(entity_name_t n);
54
55 const char* get_type_str() const;
56
57 uint32_t get_type() const { return type; }
58 bool is_osd() const { return get_type() == CEPH_ENTITY_TYPE_OSD; }
59 bool is_mgr() const { return get_type() == CEPH_ENTITY_TYPE_MGR; }
60 bool is_mds() const { return get_type() == CEPH_ENTITY_TYPE_MDS; }
61 bool is_client() const { return get_type() == CEPH_ENTITY_TYPE_CLIENT; }
62 bool is_mon() const { return get_type() == CEPH_ENTITY_TYPE_MON; }
63
64 std::string_view get_type_name() const;
65 const std::string &get_id() const;
66 bool has_default_id() const;
67
68 static std::string get_valid_types_as_str();
69 static uint32_t str_to_ceph_entity_type(std::string_view);
70
71 friend bool operator<(const EntityName& a, const EntityName& b);
72 friend std::ostream& operator<<(std::ostream& out, const EntityName& n);
73 friend bool operator==(const EntityName& a, const EntityName& b);
74 friend bool operator!=(const EntityName& a, const EntityName& b);
75
76 private:
77 struct str_to_entity_type_t {
78 uint32_t type;
79 const char *str;
80 };
81 static const std::array<str_to_entity_type_t, 6> STR_TO_ENTITY_TYPE;
82
83 uint32_t type = 0;
84 std::string id;
85 std::string type_id;
86 };
87
88 WRITE_CLASS_ENCODER(EntityName)
89
90 WRITE_EQ_OPERATORS_2(EntityName, type, id)
91
92 #endif