@@ -20,103 +20,102 @@ namespace uservice_dynconf::handlers::admin_v1_configs::post {
2020namespace {
2121
2222bool ConsistsOfIdsFromConfigs (
23- const std::unordered_set<std::string> &kill_switches,
24- const userver::formats::json::Value &configs) {
25- for (const auto &kill_switch : kill_switches) {
26- if (!configs.HasMember (kill_switch)) {
27- return false ;
23+ const std::unordered_set<std::string>& kill_switches,
24+ const userver::formats::json::Value& configs
25+ ) {
26+ for (const auto & kill_switch : kill_switches) {
27+ if (!configs.HasMember (kill_switch)) {
28+ return false ;
29+ }
2830 }
29- }
30- return true ;
31+ return true ;
3132}
3233
33- bool HasIntersection (const std::unordered_set<std::string> & first,
34- const std::unordered_set<std::string> &second ) {
35- for ( const auto & key : first ) {
36- if (second. count (key) > 0 ) {
37- return true ;
34+ bool HasIntersection (const std::unordered_set<std::string>& first, const std::unordered_set<std::string>& second) {
35+ for ( const auto & key : first ) {
36+ if (second. count ( key) > 0 ) {
37+ return true ;
38+ }
3839 }
39- }
40- return false ;
40+ return false ;
4141}
4242
4343userver::formats::json::Value MakeConfigModeMap (
44- const userver::formats::json::Value &configs,
45- const std::unordered_set<std::string> &kill_switches_enabled,
46- const std::unordered_set<std::string> &kill_switches_disabled) {
47- using Mode = uservice_dynconf::models::Mode;
48-
49- userver::formats::json::ValueBuilder builder;
50- for (const auto &[config_name, config_value] : Items (configs)) {
51- Mode config_mode = Mode::kDynamicConfig ;
52- if (kill_switches_enabled.count (config_name) > 0 ) {
53- config_mode = Mode::kKillSwitchEnabled ;
54- } else if (kill_switches_disabled.count (config_name) > 0 ) {
55- config_mode = Mode::kKillSwitchDisabled ;
44+ const userver::formats::json::Value& configs,
45+ const std::unordered_set<std::string>& kill_switches_enabled,
46+ const std::unordered_set<std::string>& kill_switches_disabled
47+ ) {
48+ using Mode = uservice_dynconf::models::Mode;
49+
50+ userver::formats::json::ValueBuilder builder;
51+ for (const auto & [config_name, config_value] : Items (configs)) {
52+ Mode config_mode = Mode::kDynamicConfig ;
53+ if (kill_switches_enabled.count (config_name) > 0 ) {
54+ config_mode = Mode::kKillSwitchEnabled ;
55+ } else if (kill_switches_disabled.count (config_name) > 0 ) {
56+ config_mode = Mode::kKillSwitchDisabled ;
57+ }
58+ builder[config_name] = uservice_dynconf::models::ToString (config_mode);
5659 }
57- builder[config_name] = uservice_dynconf::models::ToString (config_mode);
58- }
5960
60- return builder.ExtractValue ();
61+ return builder.ExtractValue ();
6162}
6263
63- } // namespace
64+ } // namespace
6465
65- Handler::Handler (const userver::components::ComponentConfig &config,
66- const userver::components::ComponentContext &context)
66+ Handler::Handler (
67+ const userver::components::ComponentConfig& config,
68+ const userver::components::ComponentContext& context
69+ )
6770 : HttpHandlerJsonBase(config, context),
68- cluster_ (
69- context
70- .FindComponent<userver::components::Postgres>(" settings-database" )
71- .GetCluster()) {}
72-
73- userver::formats::json::Value Handler::HandleRequestJsonThrow (
74- const userver::server::http::HttpRequest &request,
75- const userver::formats::json::Value &request_json,
76- userver::server::request::RequestContext &) const {
77- auto &http_response = request.GetHttpResponse ();
78- auto &&request_data = request_json.As <AdminConfigsRequestBody>();
79-
80- if (!request_data.configs .has_value () ||
81- request_data.configs .value ().extra .IsEmpty () ||
82- !request_data.service .has_value () ||
83- request_data.service .value ().empty ()) {
84- http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
85- return uservice_dynconf::utils::MakeError (
86- " 400" , " Fields 'configs' and 'service' are required" );
87- }
88-
89- const auto configs = request_data.configs .value ().extra ;
90- const auto kill_switches_enabled =
91- request_data.kill_switches_enabled .value_or (
92- std::unordered_set<std::string>{});
93- const auto kill_switches_disabled =
94- request_data.kill_switches_disabled .value_or (
95- std::unordered_set<std::string>{});
96-
97- if (!ConsistsOfIdsFromConfigs (kill_switches_enabled, configs) ||
98- !ConsistsOfIdsFromConfigs (kill_switches_disabled, configs)) {
99- http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
100- return uservice_dynconf::utils::MakeError (
101- " 400" , " Fields 'kill_switches_enabled' and 'kill_switches_disabled' "
102- " must consist of ids from 'configs' field" );
103- }
104- if (HasIntersection (kill_switches_enabled, kill_switches_disabled)) {
105- http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
106- return uservice_dynconf::utils::MakeError (
107- " 400" , " Ids in 'kill_switches_enabled' and 'kill_switches_disabled' "
108- " must not overlap" );
109- }
110-
111- const auto config_mode_map =
112- MakeConfigModeMap (configs, kill_switches_enabled, kill_switches_disabled);
113- cluster_->Execute (userver::storages::postgres::ClusterHostType::kMaster ,
114- uservice_dynconf::sql::kInsertConfigValue ,
115- request_data.service .value (), configs, config_mode_map);
116-
117- http_response.SetStatus (userver::server::http::HttpStatus::kNoContent );
118-
119- return {};
71+ cluster_ (context.FindComponent<userver::components::Postgres>(" settings-database" ).GetCluster()) {}
72+
73+ userver::formats::json::Value Handler::
74+ HandleRequestJsonThrow (const userver::server::http::HttpRequest& request, const userver::formats::json::Value& request_json, userver::server::request::RequestContext&)
75+ const {
76+ auto & http_response = request.GetHttpResponse ();
77+ auto && request_data = request_json.As <AdminConfigsRequestBody>();
78+
79+ if (!request_data.configs .has_value () || request_data.configs .value ().extra .IsEmpty () ||
80+ !request_data.service .has_value () || request_data.service .value ().empty ()) {
81+ http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
82+ return uservice_dynconf::utils::MakeError (" 400" , " Fields 'configs' and 'service' are required" );
83+ }
84+
85+ const auto configs = request_data.configs .value ().extra ;
86+ const auto kill_switches_enabled = request_data.kill_switches_enabled .value_or (std::unordered_set<std::string>{});
87+ const auto kill_switches_disabled = request_data.kill_switches_disabled .value_or (std::unordered_set<std::string>{});
88+
89+ if (!ConsistsOfIdsFromConfigs (kill_switches_enabled, configs) ||
90+ !ConsistsOfIdsFromConfigs (kill_switches_disabled, configs)) {
91+ http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
92+ return uservice_dynconf::utils::MakeError (
93+ " 400" ,
94+ " Fields 'kill_switches_enabled' and 'kill_switches_disabled' "
95+ " must consist of ids from 'configs' field"
96+ );
97+ }
98+ if (HasIntersection (kill_switches_enabled, kill_switches_disabled)) {
99+ http_response.SetStatus (userver::server::http::HttpStatus::kBadRequest );
100+ return uservice_dynconf::utils::MakeError (
101+ " 400" ,
102+ " Ids in 'kill_switches_enabled' and 'kill_switches_disabled' "
103+ " must not overlap"
104+ );
105+ }
106+
107+ const auto config_mode_map = MakeConfigModeMap (configs, kill_switches_enabled, kill_switches_disabled);
108+ cluster_->Execute (
109+ userver::storages::postgres::ClusterHostType::kMaster ,
110+ uservice_dynconf::sql::kInsertConfigValue ,
111+ request_data.service .value (),
112+ configs,
113+ config_mode_map
114+ );
115+
116+ http_response.SetStatus (userver::server::http::HttpStatus::kNoContent );
117+
118+ return {};
120119}
121120
122- } // namespace uservice_dynconf::handlers::admin_v1_configs::post
121+ } // namespace uservice_dynconf::handlers::admin_v1_configs::post
0 commit comments