Skip to content

Commit ec235a3

Browse files
authored
Merge pull request #75 from Nordix/add_config_manipulation
Add describe_configs and alter_configs to API
2 parents fd7c4a1 + 1a789e0 commit ec235a3

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
* 2.4.0
2+
- Add Describe and Alter Configs APIs, part of KIP-133
3+
14
* 2.3.6
2-
- Upgrade snappyer and crc32cer to fix build in windows
5+
- Upgrade snappyer and crc32cer to fix build in windows
36

47
* 2.3.5
58
- Improve produce request encoding performance by 35%

src/kpro_req_lib.erl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
, delete_topics/3
4444
]).
4545

46+
-export([ describe_configs/3
47+
, alter_configs/3
48+
]).
49+
4650
-export([ encode/3
4751
, make/3
4852
]).
@@ -285,7 +289,7 @@ add_offsets_to_txn(TxnCtx, CgId) ->
285289
%% @doc Make `create_topics' request.
286290
%% if 0 is given as `timeout' option the request will trigger a creation
287291
%% but return immediately.
288-
%% `validate_only' option is only relavent when the API version is
292+
%% `validate_only' option is only relevant when the API version is
289293
%% greater than 0.
290294
-spec create_topics(vsn(), [Topics :: kpro:struct()],
291295
#{timeout => kpro:int32(),
@@ -321,6 +325,28 @@ delete_topics(Vsn, Topics, Opts) ->
321325
},
322326
make(delete_topics, Vsn, Body).
323327

328+
%% @doc Make a `describe_configs' request.
329+
%% `include_synonyms' option is only relevant when the API version is
330+
%% greater than 0.
331+
-spec describe_configs(vsn(), [Resources :: kpro:struct()],
332+
#{include_synonyms => boolean()}) -> req().
333+
describe_configs(Vsn, Resources, Opts) ->
334+
IncludeSynonyms = maps:get(include_synonyms, Opts, false),
335+
Body = #{ resources => Resources
336+
, include_synonyms => IncludeSynonyms
337+
},
338+
make(describe_configs, Vsn, Body).
339+
340+
%% @doc Make an `alter_configs' request.
341+
-spec alter_configs(vsn(), [Resources :: kpro:struct()],
342+
#{validate_only => boolean()}) -> req().
343+
alter_configs(Vsn, Resources, Opts) ->
344+
ValidateOnly = maps:get(validate_only, Opts, false),
345+
Body = #{ resources => Resources
346+
, validate_only => ValidateOnly
347+
},
348+
make(alter_configs, Vsn, Body).
349+
324350
%% @doc Help function to make a request body.
325351
-spec make(api(), vsn(), struct()) -> req().
326352
make(API, Vsn, Fields) ->

test/kpro_test_lib.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ parse_rsp(#kpro_rsp{ api = create_partitions
180180
, msg = Msg
181181
}) ->
182182
error_if_any(kpro:find(topic_errors, Msg));
183+
parse_rsp(#kpro_rsp{ api = describe_configs
184+
, msg = Msg
185+
}) ->
186+
Resources = kpro:find(resources, Msg),
187+
ok = error_if_any(Resources),
188+
Resources;
189+
parse_rsp(#kpro_rsp{ api = alter_configs
190+
, msg = Msg
191+
}) ->
192+
error_if_any(kpro:find(resources, Msg));
183193
parse_rsp(#kpro_rsp{msg = Msg}) ->
184194
Msg.
185195

test/kpro_topic_mngr_tests.erl

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
%% create_topics
1818
%% delete_topics
1919
%% delete_records
20+
%% describe_configs
21+
%% alter_configs
2022
-module(kpro_topic_mngr_tests).
2123

2224
-include_lib("eunit/include/eunit.hrl").
2325
-include("kpro_private.hrl").
2426

27+
-define(RESOURCE_TYPE_TOPIC, 2).
28+
2529
%% Create a random-name partition with 1 partition 1 replica
2630
%% Increase partition number to 2
2731
create_topic_partition_test() ->
@@ -68,6 +72,59 @@ test_create_topic_partition(CreateTopicsVsn, CreatePartitionsVsn) ->
6872
end
6973
end).
7074

75+
%% Get all configurations for a topic.
76+
describe_configs_test() ->
77+
Vsn = get_max_api_vsn(describe_configs),
78+
test_describe_configs(Vsn).
79+
80+
test_describe_configs(false) ->
81+
io:format(user, " skipped ", []);
82+
test_describe_configs(Vsn) ->
83+
{ok, [Topic | _]} = get_test_topics(),
84+
DescribeConfigArgs =
85+
#{ resource_type => ?RESOURCE_TYPE_TOPIC
86+
, resource_name => Topic
87+
, config_names => ?null %% Get all configs
88+
},
89+
Opts = #{include_synonyms => false},
90+
Req = kpro_req_lib:describe_configs(Vsn, [DescribeConfigArgs], Opts),
91+
kpro_test_lib:with_connection(
92+
fun(Endpoints, Config) -> kpro:connect_controller(Endpoints, Config) end,
93+
fun(Conn) ->
94+
{ok, Rsp} = kpro:request_sync(Conn, Req, infinity),
95+
Resources = kpro_test_lib:parse_rsp(Rsp),
96+
?assertMatch([#{ resource_name := Topic }], Resources)
97+
end).
98+
99+
%% Alter the configuration for a topic.
100+
alter_configs_test() ->
101+
Vsn = get_max_api_vsn(alter_configs),
102+
test_alter_configs(Vsn).
103+
104+
test_alter_configs(false) ->
105+
io:format(user, " skipped ", []);
106+
test_alter_configs(Vsn) ->
107+
{ok, [Topic | _]} = get_test_topics(),
108+
AlterConfigsArgs =
109+
#{ resource_type => ?RESOURCE_TYPE_TOPIC
110+
, resource_name => Topic
111+
, config_entries => [
112+
[ {config_name, "cleanup.policy"}
113+
, {config_value, <<"compact">>}]
114+
]
115+
},
116+
Opts = #{validate_only => false},
117+
Req = kpro_req_lib:alter_configs(Vsn, [AlterConfigsArgs], Opts),
118+
DescribeVsn = get_max_api_vsn(describe_configs),
119+
kpro_test_lib:with_connection(
120+
fun(Endpoints, Config) -> kpro:connect_controller(Endpoints, Config) end,
121+
fun(Conn) ->
122+
validate_topic_config(DescribeVsn, Conn, Topic, "cleanup.policy", <<"delete">>),
123+
{ok, Rsp} = kpro:request_sync(Conn, Req, infinity),
124+
ok = kpro_test_lib:parse_rsp(Rsp),
125+
validate_topic_config(DescribeVsn, Conn, Topic, "cleanup.policy", <<"compact">>)
126+
end).
127+
71128
%% Delete all topics created in this test module.
72129
delete_topics_test() ->
73130
Timeout = case is_integer(get_max_api_vsn(create_partitions)) of
@@ -119,7 +176,7 @@ get_test_topics(Connection) ->
119176
ErrorCode = ?no_error, %% assert
120177
Name = kpro:find(topic, Topic),
121178
case lists:prefix(atom_to_list(?MODULE),
122-
binary_to_list(Name)) of
179+
binary_to_list(Name)) of
123180
true -> [Name | Acc];
124181
false -> Acc
125182
end
@@ -141,6 +198,20 @@ get_max_api_vsn(API) ->
141198

142199
rand() -> rand:uniform(1000000).
143200

201+
validate_topic_config(false, _, _, _, _) ->
202+
not_available;
203+
validate_topic_config(Vsn, Conn, Topic, ConfigName, ConfigValue) ->
204+
DescribeConfigArgs =
205+
#{ resource_type => ?RESOURCE_TYPE_TOPIC
206+
, resource_name => Topic
207+
, config_names => [ConfigName]
208+
},
209+
Req = kpro_req_lib:describe_configs(Vsn, [DescribeConfigArgs], #{}),
210+
{ok, Rsp} = kpro:request_sync(Conn, Req, infinity),
211+
[Resource] = kpro_test_lib:parse_rsp(Rsp),
212+
[Entry] = kpro:find(config_entries, Resource),
213+
?assertEqual(ConfigValue, kpro:find(config_value, Entry)).
214+
144215
%%%_* Emacs ====================================================================
145216
%%% Local Variables:
146217
%%% allout-layout: t

0 commit comments

Comments
 (0)