Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat: add http interface to get a specified config #719

Merged
merged 7 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/dsn/utility/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ extern bool has_tag(const std::string &name, const flag_tag &tag);

// list all the flags
extern std::string list_all_flags();

// get the json string of a specified flag
extern error_with<std::string> get_flag_str(const std::string &flag_name);
} // namespace dsn
4 changes: 4 additions & 0 deletions src/http/builtin_http_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ namespace dsn {
[](const http_request &req, http_response &resp) { update_config(req, resp); })
.with_help("Updates the value of a config");

register_http_call("config")
.with_callback([](const http_request &req, http_response &resp) { get_config(req, resp); })
.with_help("get the details of a specified config");

register_http_call("configs")
.with_callback(
[](const http_request &req, http_response &resp) { list_all_configs(req, resp); })
Expand Down
1 change: 1 addition & 0 deletions src/http/builtin_http_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ extern void update_config(const http_request &req, http_response &resp);

extern void list_all_configs(const http_request &req, http_response &resp);

extern void get_config(const http_request &req, http_response &resp);
} // namespace dsn
21 changes: 21 additions & 0 deletions src/http/config_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ void list_all_configs(const http_request &req, http_response &resp)
resp.body = list_all_flags();
resp.status_code = http_status_code::ok;
}

void get_config(const http_request &req, http_response &resp)
{
std::string config_name;
for (const auto &p : req.query_args) {
if ("name" == p.first) {
config_name = p.second;
} else {
resp.status_code = http_status_code::bad_request;
return;
}
}

auto res = get_flag_str(config_name);
if (res.is_ok()) {
resp.body = res.get_value();
} else {
resp.body = res.get_error().description();
}
resp.status_code = http_status_code::ok;
}
} // namespace dsn
15 changes: 15 additions & 0 deletions src/utils/flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ class flag_registry : public utils::singleton<flag_registry>
return it->second.has_tag(tag);
}

error_with<std::string> get_flag_str(const std::string &name) const
{
const auto iter = _flags.find(name);
if (iter == _flags.end()) {
return error_s::make(ERR_OBJECT_NOT_FOUND, fmt::format("{} is not found", name));
}

return iter->second.to_json();
}

std::string list_all_flags() const
{
utils::table_printer tp;
Expand Down Expand Up @@ -272,6 +282,11 @@ flag_tagger::flag_tagger(const char *name, const flag_tag &tag)
return flag_registry::instance().has_tag(name, tag);
}

/*extern*/ error_with<std::string> get_flag_str(const std::string &flag_name)
{
return flag_registry::instance().get_flag_str(flag_name);
}

/*extern*/ std::string list_all_flags() { return flag_registry::instance().list_all_flags(); }

} // namespace dsn
70 changes: 70 additions & 0 deletions src/utils/test/flag_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,75 @@ TEST(flag_test, tag_flag)
res = has_tag("no_flag", flag_tag::FT_MUTABLE);
ASSERT_FALSE(res);
}

DSN_DEFINE_int32("flag_test", get_flag_int32, 5, "test get_flag_int32");
DSN_TAG_VARIABLE(get_flag_int32, FT_MUTABLE);
DSN_DEFINE_uint32("flag_test", get_flag_uint32, 5, "test get_flag_uint32");
DSN_TAG_VARIABLE(get_flag_uint32, FT_MUTABLE);
DSN_DEFINE_int64("flag_test", get_flag_int64, 5, "test get_flag_int64");
DSN_TAG_VARIABLE(get_flag_int64, FT_MUTABLE);
DSN_DEFINE_uint64("flag_test", get_flag_uint64, 5, "test get_flag_uint64");
DSN_TAG_VARIABLE(get_flag_uint64, FT_MUTABLE);
DSN_DEFINE_double("flag_test", get_flag_double, 5.12, "test get_flag_double");
DSN_TAG_VARIABLE(get_flag_double, FT_MUTABLE);
DSN_DEFINE_bool("flag_test", get_flag_bool, true, "test get_flag_bool");
DSN_TAG_VARIABLE(get_flag_bool, FT_MUTABLE);
DSN_DEFINE_string("flag_test", get_flag_string, "flag_string", "test get_flag_string");
DSN_TAG_VARIABLE(get_flag_string, FT_MUTABLE);

TEST(flag_test, get_config)
{
auto res = get_flag_str("get_flag_not_exist");
ASSERT_EQ(res.get_error().code(), ERR_OBJECT_NOT_FOUND);

res = get_flag_str("get_flag_int32");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_int32\",\"section\":\"flag_test\",\"type\":\"FV_INT32\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_int32\",\"value\":\"5\"}\n");
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

res = get_flag_str("get_flag_uint32");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_uint32\",\"section\":\"flag_test\",\"type\":\"FV_UINT32\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_uint32\",\"value\":\"5\"}\n");

res = get_flag_str("get_flag_int64");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_int64\",\"section\":\"flag_test\",\"type\":\"FV_INT64\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_int64\",\"value\":\"5\"}\n");

res = get_flag_str("get_flag_uint64");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_uint64\",\"section\":\"flag_test\",\"type\":\"FV_UINT64\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_uint64\",\"value\":\"5\"}\n");

res = get_flag_str("get_flag_double");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_double\",\"section\":\"flag_test\",\"type\":\"FV_DOUBLE\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_double\",\"value\":\"5.12\"}\n");

res = get_flag_str("get_flag_bool");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(
res.get_value(),
"{\"name\":\"get_flag_bool\",\"section\":\"flag_test\",\"type\":\"FV_BOOL\",\"tags\":"
"\"flag_tag::FT_MUTABLE\",\"description\":\"test get_flag_bool\",\"value\":\"true\"}\n");

res = get_flag_str("get_flag_string");
ASSERT_TRUE(res.is_ok());
ASSERT_EQ(res.get_value(),
"{\"name\":\"get_flag_string\",\"section\":\"flag_test\",\"type\":\"FV_STRING\","
"\"tags\":\"flag_tag::FT_MUTABLE\",\"description\":\"test "
"get_flag_string\",\"value\":\"flag_string\"}\n");
}
} // namespace utils
} // namespace dsn