Skip to content

Commit f5fe419

Browse files
Make PUT /api/vhosts/{name} update tags and/or description
1 parent 44ba27f commit f5fe419

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

deps/rabbit/src/rabbit_vhost.erl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ do_add(Name, Description, Tags, ActingUser) ->
203203
{error, Msg}
204204
end.
205205

206+
-spec update(vhost:name(), binary(), [atom()], rabbit_types:username()) -> rabbit_types:ok_or_error(any()).
207+
update(Name, Description, Tags, ActingUser) ->
208+
rabbit_misc:execute_mnesia_transaction(
209+
fun () ->
210+
case mnesia:wread({rabbit_vhost, Name}) of
211+
[] ->
212+
{error, {no_such_vhost, Name}};
213+
[VHost0] ->
214+
VHost = vhost:merge_metadata(VHost0, #{description => Description, tags => Tags}),
215+
rabbit_log:debug("Updating a virtual host record ~p", [VHost]),
216+
ok = mnesia:write(rabbit_vhost, VHost, write),
217+
rabbit_event:notify(vhost_updated, info(VHost)
218+
++ [{user_who_performed_action, ActingUser},
219+
{description, Description},
220+
{tags, Tags}]),
221+
ok
222+
end
223+
end).
224+
225+
206226
-spec delete(vhost:name(), rabbit_types:username()) -> rabbit_types:ok_or_error(any()).
207227

208228
delete(VHost, ActingUser) ->
@@ -240,11 +260,12 @@ put_vhost(Name, Description, Tags0, Trace, Username) ->
240260
"null" -> <<"">>;
241261
Other -> Other
242262
end,
263+
ParsedTags = parse_tags(Tags),
264+
rabbit_log:debug("Parsed tags ~p to ~p", [Tags, ParsedTags]),
243265
Result = case exists(Name) of
244-
true -> ok;
266+
true ->
267+
update(Name, Description, ParsedTags, Username);
245268
false ->
246-
ParsedTags = parse_tags(Tags),
247-
rabbit_log:debug("Parsed tags ~p to ~p", [Tags, ParsedTags]),
248269
add(Name, Description, ParsedTags, Username),
249270
%% wait for up to 45 seconds for the vhost to initialise
250271
%% on all nodes

deps/rabbit/src/vhost.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
get_tags/1,
2828
set_limits/2,
2929
set_metadata/2,
30+
merge_metadata/2,
3031
is_tagged_with/2
3132
]).
3233

@@ -173,6 +174,7 @@ set_limits(VHost, Value) ->
173174
vhost_v1:set_limits(VHost, Value)
174175
end.
175176

177+
-spec set_metadata(vhost(), metadata()) -> vhost().
176178
set_metadata(VHost, Value) ->
177179
case record_version_to_use() of
178180
?record_version ->
@@ -182,6 +184,18 @@ set_metadata(VHost, Value) ->
182184
VHost
183185
end.
184186

187+
-spec merge_metadata(vhost(), metadata()) -> vhost().
188+
merge_metadata(VHost, Value) ->
189+
case record_version_to_use() of
190+
?record_version ->
191+
Meta0 = get_metadata(VHost),
192+
NewMeta = maps:merge(Meta0, Value),
193+
VHost#vhost{metadata = NewMeta};
194+
_ ->
195+
%% the field is not available, so this is a no-op
196+
VHost
197+
end.
198+
185199
-spec is_tagged_with(vhost:vhost(), atom()) -> boolean().
186200
is_tagged_with(VHost, Tag) ->
187201
lists:member(Tag, get_tags(VHost)).

deps/rabbitmq_management/test/rabbit_mgmt_only_http_SUITE.erl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ vhosts_test(Config) ->
261261
?assert(not maps:is_key(recv_oct, GetFirst)),
262262
?assert(maps:is_key(cluster_state, GetFirst)),
263263

264+
%% PUT can update metadata (description, tags)
265+
Desc0 = "desc 0",
266+
Meta0 = [
267+
{description, Desc0},
268+
{tags, "tag1,tag2"}
269+
],
270+
http_put(Config, "/vhosts/myvhost", Meta0, {group, '2xx'}),
271+
#{description := Desc1, tags := Tags1} = http_get(Config, "/vhosts/myvhost", ?OK),
272+
?assertEqual(Desc0, Desc1),
273+
?assertEqual([<<"tag1">>, <<"tag2">>], Tags1),
274+
275+
Desc2 = "desc 2",
276+
Meta2 = [
277+
{description, Desc2},
278+
{tags, "tag3"}
279+
],
280+
http_put(Config, "/vhosts/myvhost", Meta2, {group, '2xx'}),
281+
#{description := Desc3, tags := Tags3} = http_get(Config, "/vhosts/myvhost", ?OK),
282+
?assertEqual(Desc2, Desc3),
283+
?assertEqual([<<"tag3">>], Tags3),
284+
264285
%% Check individually
265286
Get = http_get(Config, "/vhosts/%2F", ?OK),
266287
assert_item(#{name => <<"/">>}, Get),

0 commit comments

Comments
 (0)