summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmile Joubert <emile@rabbitmq.com>2012-10-15 17:46:25 +0100
committerEmile Joubert <emile@rabbitmq.com>2012-10-15 17:46:25 +0100
commit3c0d32a5a3fc9c53301cbc21d2e8a40c5ca2a9a3 (patch)
treea719f9470be16593bfb16907bf628fbb41fc7603
parent5c9503287fa53f03e81e187dcfb01dc98a83dd99 (diff)
downloadrabbitmq-server-3c0d32a5a3fc9c53301cbc21d2e8a40c5ca2a9a3.tar.gz
Rename and refactor policies
-rw-r--r--src/rabbit_control_main.erl35
-rw-r--r--src/rabbit_policy.erl91
-rw-r--r--src/rabbit_runtime_parameters.erl197
-rw-r--r--src/rabbit_vhost.erl13
4 files changed, 155 insertions, 181 deletions
diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl
index d85418c5..3f3a0b8b 100644
--- a/src/rabbit_control_main.erl
+++ b/src/rabbit_control_main.erl
@@ -445,47 +445,44 @@ action(set_parameter, Node, [Component, Key, Value], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Setting runtime parameter ~p for component ~p to ~p",
[Key, Component, Value]),
- rpc_call(Node, rabbit_runtime_parameters, parse_set_param,
+ rpc_call(Node, rabbit_runtime_parameters, parse_set,
[VHostArg, list_to_binary(Component), list_to_binary(Key), Value]);
action(clear_parameter, Node, [Component, Key], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Clearing runtime parameter ~p for component ~p", [Key, Component]),
- rpc_call(Node, rabbit_runtime_parameters, clear_param,
- [VHostArg, list_to_binary(Component), list_to_binary(Key)]);
+ rpc_call(Node, rabbit_runtime_parameters, clear, [VHostArg,
+ list_to_binary(Component),
+ list_to_binary(Key)]);
action(list_parameters, Node, [], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Listing runtime parameters", []),
display_info_list(
- rpc_call(Node, rabbit_runtime_parameters, list_formatted_param,
- [VHostArg]),
+ rpc_call(Node, rabbit_runtime_parameters, list_formatted, [VHostArg]),
rabbit_runtime_parameters:info_keys());
-action(set_policy, Node, [Key, Pattern, Defn | Priority], Opts, Inform)
- when Priority == [] orelse length(Priority) == 1 ->
+action(set_policy, Node, [Key, Pattern, Defn | Prio], Opts, Inform)
+ when Prio == [] orelse length(Prio) == 1 ->
Msg = "Setting policy ~p for pattern ~p to ~p",
- InformMsg = case Priority of [] -> Msg;
- [_] -> Msg ++ " with priority ~p"
- end,
+ {InformMsg, Prio1} = case Prio of [] -> {Msg, undefined};
+ [P] -> {Msg ++ " with priority ~s", P}
+ end,
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
- Inform(InformMsg, [Key, Pattern, Defn] ++ Priority),
- rpc_call(Node, rabbit_runtime_parameters, parse_set_policy,
- [VHostArg, list_to_binary(Key), Pattern, Defn] ++ Priority);
+ Inform(InformMsg, [Key, Pattern, Defn] ++ Prio),
+ rpc_call(Node, rabbit_policy, parse_add,
+ [VHostArg, list_to_binary(Key), Pattern, Defn, Prio1]);
action(clear_policy, Node, [Key], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Clearing policy ~p", [Key]),
- rpc_call(Node, rabbit_runtime_parameters, clear_policy,
- [VHostArg, list_to_binary(Key)]);
+ rpc_call(Node, rabbit_policy, delete, [VHostArg, list_to_binary(Key)]);
action(list_policies, Node, [], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Listing policies", []),
- display_info_list(
- rpc_call(Node, rabbit_runtime_parameters, list_formatted_policies,
- [VHostArg]),
- rabbit_runtime_parameters:info_keys_policy());
+ display_info_list(rpc_call(Node, rabbit_policy, list_formatted, [VHostArg]),
+ rabbit_policy:info_keys());
action(report, Node, _Args, _Opts, Inform) ->
Inform("Reporting server status on ~p~n~n", [erlang:universaltime()]),
diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl
index 5b997875..3e7472cc 100644
--- a/src/rabbit_policy.erl
+++ b/src/rabbit_policy.erl
@@ -27,6 +27,10 @@
-export([register/0]).
-export([name/1, get/2, set/1]).
-export([validate/4, validate_clear/3, notify/4, notify_clear/3]).
+-export([parse_add/5, add/5, delete/2, lookup/2, list/0, list/1,
+ list_formatted/1, info_keys/0]).
+
+-define(TABLE, rabbit_runtime_parameters).
-rabbit_boot_step({?MODULE,
[{description, "policy parameters"},
@@ -55,7 +59,7 @@ get(Name, EntityName = #resource{virtual_host = VHost}) ->
get0(Name, match(EntityName, list(VHost))).
get0(_Name, undefined) -> {error, not_found};
-get0(Name, List) -> case pget(<<"policy">>, List) of
+get0(Name, List) -> case pget(definition, List) of
undefined -> {error, not_found};
Policy -> case pget(Name, Policy) of
undefined -> {error, not_found};
@@ -65,6 +69,83 @@ get0(Name, List) -> case pget(<<"policy">>, List) of
%%----------------------------------------------------------------------------
+parse_add(VHost, Key, Pattern, Definition, undefined) ->
+ parse_add_policy0(VHost, Key, Pattern, Definition, []);
+parse_add(VHost, Key, Pattern, Definition, Priority) ->
+ try list_to_integer(Priority) of
+ Num -> parse_add_policy0(VHost, Key, Pattern, Definition,
+ [{<<"priority">>, Num}])
+ catch
+ error:badarg -> {error, "~p priority must be a number", [Priority]}
+ end.
+
+parse_add_policy0(VHost, Key, Pattern, Defn, Priority) ->
+ case rabbit_misc:json_decode(Defn) of
+ {ok, JSON} ->
+ add0(VHost, Key, [{<<"pattern">>, list_to_binary(Pattern)},
+ {<<"policy">>, rabbit_misc:json_to_term(JSON)}] ++
+ Priority);
+ error ->
+ {error_string, "JSON decoding error"}
+ end.
+
+add(VHost, Key, Pattern, Definition, Priority) ->
+ PolicyProps = [{<<"pattern">>, Pattern}, {<<"policy">>, Definition}],
+ add0(VHost, Key, case Priority of
+ undefined -> [];
+ _ -> [{<<"priority">>, Priority}]
+ end ++ PolicyProps).
+
+add0(VHost, Key, Term) ->
+ rabbit_runtime_parameters:set_any(VHost, <<"policy">>, Key, Term).
+
+delete(VHost, Key) ->
+ rabbit_runtime_parameters:clear_any(VHost, <<"policy">>, Key).
+
+lookup(VHost, Key) ->
+ case mnesia:dirty_read(?TABLE, {VHost, <<"policy">>, Key}) of
+ [] -> not_found;
+ [P] -> p(P, fun ident/1)
+ end.
+
+list() ->
+ list('_').
+
+list(VHost) ->
+ list0(VHost, fun ident/1).
+
+list_formatted(VHost) ->
+ order_policies(list0(VHost, fun format/1)).
+
+list0(VHost, DefnFun) ->
+ Match = #runtime_parameters{key = {VHost, <<"policy">>, '_'}, _ = '_'},
+ [p(P, DefnFun) || P <- mnesia:dirty_match_object(?TABLE, Match)].
+
+order_policies(PropList) ->
+ lists:sort(fun (A, B) -> pget(priority, A, 0) < pget(priority, B, 0) end,
+ PropList).
+
+p(#runtime_parameters{key = {VHost, <<"policy">>, Key}, value = Value},
+ DefnFun) ->
+ [{vhost, VHost},
+ {key, Key},
+ {pattern, pget(<<"pattern">>, Value)},
+ {definition, DefnFun(pget(<<"policy">>, Value))}] ++
+ case pget(<<"priority">>, Value) of
+ undefined -> [];
+ Priority -> [{priority, Priority}]
+ end.
+
+format(Term) ->
+ {ok, JSON} = rabbit_misc:json_encode(rabbit_misc:term_to_json(Term)),
+ list_to_binary(JSON).
+
+ident(X) -> X.
+
+info_keys() -> [vhost, key, pattern, definition, priority].
+
+%%----------------------------------------------------------------------------
+
validate(_VHost, <<"policy">>, Name, Term) ->
rabbit_parameter_validation:proplist(
Name, policy_validation(), Term).
@@ -80,10 +161,6 @@ notify_clear(VHost, <<"policy">>, _Name) ->
%%----------------------------------------------------------------------------
-list(VHost) ->
- [[{<<"name">>, pget(key, P)} | pget(value, P)]
- || P <- rabbit_runtime_parameters:list_policies_raw(VHost)].
-
update_policies(VHost) ->
Policies = list(VHost),
{Xs, Qs} = rabbit_misc:execute_mnesia_transaction(
@@ -127,9 +204,9 @@ match(Name, Policies) ->
end.
matches(#resource{name = Name}, Policy) ->
- match =:= re:run(Name, pget(<<"pattern">>, Policy), [{capture, none}]).
+ match =:= re:run(Name, pget(pattern, Policy), [{capture, none}]).
-sort_pred(A, B) -> pget(<<"priority">>, A, 0) >= pget(<<"priority">>, B, 0).
+sort_pred(A, B) -> pget(priority, A, 0) >= pget(priority, B, 0).
%%----------------------------------------------------------------------------
diff --git a/src/rabbit_runtime_parameters.erl b/src/rabbit_runtime_parameters.erl
index d7f08687..e66cb749 100644
--- a/src/rabbit_runtime_parameters.erl
+++ b/src/rabbit_runtime_parameters.erl
@@ -18,15 +18,9 @@
-include("rabbit.hrl").
--export([parse_set_param/4, set_param/4,
- parse_set_policy/4, parse_set_policy/5, set_policy/5,
- clear_param/3, clear_policy/2,
- list_param/0, list_param/1, list_param/2,
- list_param_strict/1, list_param_strict/2,
- list_policies/0, list_policies/1,
- list_formatted_param/1, list_formatted_policies/1, list_policies_raw/1,
- lookup_param/3, lookup_policy/2,
- value/3, value/4, info_keys/0, info_keys_policy/0]).
+-export([parse_set/4, set/4, set_any/4, clear/3, clear_any/3, list/0, list/1,
+ list_strict/1, list/2, list_strict/2, list_formatted/1, lookup/3,
+ value/3, value/4, info_keys/0]).
%%----------------------------------------------------------------------------
@@ -34,102 +28,62 @@
-type(ok_or_error_string() :: 'ok' | {'error_string', string()}).
--spec(parse_set_param/4 :: (rabbit_types:vhost(), binary(), binary(), string())
- -> ok_or_error_string()).
--spec(set_param/4 :: (rabbit_types:vhost(), binary(), binary(), term())
- -> ok_or_error_string()).
--spec(parse_set_policy/4 :: (rabbit_types:vhost(), binary(), string(),
- string()) -> ok_or_error_string()).
--spec(parse_set_policy/5 :: (rabbit_types:vhost(), binary(), string(), string(),
- string()) -> ok_or_error_string()).
--spec(set_policy/5 :: (rabbit_types:vhost(), binary(), term(), term(), term())
- -> ok_or_error_string()).
--spec(clear_param/3 :: (rabbit_types:vhost(), binary(), binary())
- -> ok_or_error_string()).
--spec(clear_policy/2 :: (rabbit_types:vhost(), binary())
- -> ok_or_error_string()).
--spec(list_param/0 :: () -> [rabbit_types:infos()]).
--spec(list_param/1 :: (rabbit_types:vhost()) -> [rabbit_types:infos()]).
--spec(list_param_strict/1 :: (binary())
- -> [rabbit_types:infos()] | 'not_found').
--spec(list_param/2 :: (rabbit_types:vhost(), binary())
- -> [rabbit_types:infos()]).
--spec(list_param_strict/2 :: (rabbit_types:vhost(), binary())
+-spec(parse_set/4 :: (rabbit_types:vhost(), binary(), binary(), string())
+ -> ok_or_error_string()).
+-spec(set/4 :: (rabbit_types:vhost(), binary(), binary(), term())
+ -> ok_or_error_string()).
+-spec(set_any/4 :: (rabbit_types:vhost(), binary(), binary(), term())
+ -> ok_or_error_string()).
+-spec(clear/3 :: (rabbit_types:vhost(), binary(), binary())
+ -> ok_or_error_string()).
+-spec(clear_any/3 :: (rabbit_types:vhost(), binary(), binary())
+ -> ok_or_error_string()).
+-spec(list/0 :: () -> [rabbit_types:infos()]).
+-spec(list/1 :: (rabbit_types:vhost()) -> [rabbit_types:infos()]).
+-spec(list_strict/1 :: (binary()) -> [rabbit_types:infos()] | 'not_found').
+-spec(list/2 :: (rabbit_types:vhost(), binary()) -> [rabbit_types:infos()]).
+-spec(list_strict/2 :: (rabbit_types:vhost(), binary())
-> [rabbit_types:infos()] | 'not_found').
--spec(list_formatted_param/1 :: (rabbit_types:vhost())
- -> [rabbit_types:infos()]).
--spec(list_formatted_policies/1 :: (rabbit_types:vhost()) ->
- [rabbit_types:infos()]).
--spec(lookup_param/3 :: (rabbit_types:vhost(), binary(), binary())
- -> rabbit_types:infos()).
--spec(lookup_policy/2 :: (rabbit_types:vhost(), binary())
- -> rabbit_types:infos()).
+-spec(list_formatted/1 :: (rabbit_types:vhost()) -> [rabbit_types:infos()]).
+-spec(lookup/3 :: (rabbit_types:vhost(), binary(), binary())
+ -> rabbit_types:infos()).
-spec(value/3 :: (rabbit_types:vhost(), binary(), binary()) -> term()).
-spec(value/4 :: (rabbit_types:vhost(), binary(), binary(), term()) -> term()).
-spec(info_keys/0 :: () -> rabbit_types:info_keys()).
--spec(info_keys_policy/0 :: () -> rabbit_types:info_keys()).
-endif.
%%---------------------------------------------------------------------------
--import(rabbit_misc, [pget/2, pget/3, pset/3]).
+-import(rabbit_misc, [pget/2, pset/3]).
-define(TABLE, rabbit_runtime_parameters).
%%---------------------------------------------------------------------------
-parse_set_param(_, <<"policy">>, _, _) ->
+parse_set(_, <<"policy">>, _, _) ->
{error_string, "policies may not be set using this method"};
-parse_set_param(VHost, Component, Key, String) ->
+parse_set(VHost, Component, Key, String) ->
case rabbit_misc:json_decode(String) of
- {ok, JSON} -> set_param(VHost, Component, Key,
- rabbit_misc:json_to_term(JSON));
+ {ok, JSON} -> set(VHost, Component, Key, rabbit_misc:json_to_term(JSON));
error -> {error_string, "JSON decoding error"}
end.
-set_param(_, <<"policy">>, _, _) ->
+set(_, <<"policy">>, _, _) ->
{error_string, "policies may not be set using this method"};
-set_param(VHost, Component, Key, Term) ->
- case set0(VHost, Component, Key, Term) of
- ok -> ok;
- {errors, L} -> format_error(L)
- end.
-
-parse_set_policy(VHost, Key, Pat, Defn) ->
- parse_set_policy0(VHost, Key, Pat, Defn, []).
-parse_set_policy(VHost, Key, Pat, Defn, Priority) ->
- try list_to_integer(Priority) of
- Num -> parse_set_policy0(VHost, Key, Pat, Defn, [{<<"priority">>, Num}])
- catch
- _:_ -> {error, "~p priority must be a number", [Priority]}
- end.
-
-parse_set_policy0(VHost, Key, Pattern, Defn, Priority) ->
- case rabbit_misc:json_decode(Defn) of
- {ok, JSON} ->
- set_policy0(VHost, Key, pset(<<"pattern">>, list_to_binary(Pattern),
- pset(<<"policy">>,
- rabbit_misc:json_to_term(JSON),
- Priority)));
- error ->
- {error_string, "JSON decoding error"}
- end.
+set(VHost, Component, Key, Term) ->
+ set_any(VHost, Component, Key, Term).
-set_policy(VHost, Key, Pattern, Defn, Priority) ->
- PolicyProps = [{<<"pattern">>, Pattern}, {<<"policy">>, Defn}],
- set_policy0(VHost, Key, case Priority of
- undefined -> [];
- _ -> [{<<"priority">>, Priority}]
- end ++ PolicyProps).
+format_error(L) ->
+ {error_string, rabbit_misc:format_many([{"Validation failed~n", []} | L])}.
-set_policy0(VHost, Key, Term) ->
- case set0(VHost, <<"policy">>, Key, Term) of
+set_any(VHost, Component, Key, Term) ->
+ case set_any0(VHost, Component, Key, Term) of
ok -> ok;
{errors, L} -> format_error(L)
end.
-set0(VHost, Component, Key, Term) ->
+set_any0(VHost, Component, Key, Term) ->
case lookup_component(Component) of
{ok, Mod} ->
case flatten_errors(Mod:validate(VHost, Component, Key, Term)) of
@@ -157,23 +111,18 @@ mnesia_update(VHost, Component, Key, Term) ->
Res
end).
-%%---------------------------------------------------------------------------
-
-clear_param(_, <<"policy">> , _) ->
+clear(_, <<"policy">> , _) ->
{error_string, "policies may not be cleared using this method"};
-clear_param(VHost, Component, Key) ->
- case clear0(VHost, Component, Key) of
- ok -> ok;
- {errors, L} -> format_error(L)
- end.
+clear(VHost, Component, Key) ->
+ clear_any(VHost, Component, Key).
-clear_policy(VHost, Key) ->
- case clear0(VHost, <<"policy">>, Key) of
+clear_any(VHost, Component, Key) ->
+ case clear_any0(VHost, Component, Key) of
ok -> ok;
{errors, L} -> format_error(L)
end.
-clear0(VHost, Component, Key) ->
+clear_any0(VHost, Component, Key) ->
case lookup_component(Component) of
{ok, Mod} -> case flatten_errors(
Mod:validate_clear(VHost, Component, Key)) of
@@ -191,18 +140,16 @@ mnesia_clear(VHost, Component, Key) ->
ok = mnesia:delete(?TABLE, {VHost, Component, Key}, write)
end).
-%%---------------------------------------------------------------------------
-
-list_param() ->
+list() ->
[p(P) || #runtime_parameters{ key = {_VHost, Comp, _Key}} = P <-
rabbit_misc:dirty_read_all(?TABLE), Comp /= <<"policy">>].
-list_param(VHost) -> list_param(VHost, '_', []).
-list_param_strict(Component) -> list_param('_', Component, not_found).
-list_param(VHost, Component) -> list_param(VHost, Component, []).
-list_param_strict(VHost, Component) -> list_param(VHost, Component, not_found).
+list(VHost) -> list(VHost, '_', []).
+list_strict(Component) -> list('_', Component, not_found).
+list(VHost, Component) -> list(VHost, Component, []).
+list_strict(VHost, Component) -> list(VHost, Component, not_found).
-list_param(VHost, Component, Default) ->
+list(VHost, Component, Default) ->
case component_good(Component) of
true -> Match = #runtime_parameters{key = {VHost, Component, '_'},
_ = '_'},
@@ -212,44 +159,15 @@ list_param(VHost, Component, Default) ->
_ -> Default
end.
-list_policies() ->
- list_policies('_').
+list_formatted(VHost) ->
+ [pset(value, format(pget(value, P)), P) || P <- list(VHost)].
-list_policies(VHost) ->
- list_policies0(VHost, fun ident/1).
-
-list_formatted_param(VHost) ->
- [pset(value, format(pget(value, P)), P) || P <- list_param(VHost)].
-
-list_formatted_policies(VHost) ->
- order_policies(list_policies0(VHost, fun format/1)).
-
-list_policies0(VHost, DefnFun) ->
- Match = #runtime_parameters{key = {VHost, <<"policy">>, '_'}, _ = '_'},
- [pol(P, DefnFun) || P <- mnesia:dirty_match_object(?TABLE, Match)].
-
-order_policies(PropList) ->
- lists:sort(fun (A, B) -> pget(priority, A, 0) < pget(priority, B, 0) end,
- PropList).
-
-list_policies_raw(VHost) ->
- Match = #runtime_parameters{key = {VHost, <<"policy">>, '_'}, _ = '_'},
- [p(P) || P <- mnesia:dirty_match_object(?TABLE, Match)].
-
-%%---------------------------------------------------------------------------
-
-lookup_param(VHost, Component, Key) ->
+lookup(VHost, Component, Key) ->
case lookup0(VHost, Component, Key, rabbit_misc:const(not_found)) of
not_found -> not_found;
Params -> p(Params)
end.
-lookup_policy(VHost, Key) ->
- case lookup0(VHost, <<"policy">>, Key, rabbit_misc:const(not_found)) of
- not_found -> not_found;
- Policy -> pol(Policy, fun ident/1)
- end.
-
value(VHost, Component, Key) ->
case lookup0(VHost, Component, Key, rabbit_misc:const(not_found)) of
not_found -> not_found;
@@ -290,19 +208,7 @@ p(#runtime_parameters{key = {VHost, Component, Key}, value = Value}) ->
{key, Key},
{value, Value}].
-pol(#runtime_parameters{key = {VHost, <<"policy">>, Key}, value = Value},
- DefnFun) ->
- [{vhost, VHost},
- {key, Key},
- {pattern, pget(<<"pattern">>, Value)},
- {definition, DefnFun(pget(<<"policy">>, Value))}] ++
- case pget(<<"priority">>, Value) of
- undefined -> [];
- Priority -> [{priority, Priority}]
- end.
-
-info_keys() -> [component, key, value].
-info_keys_policy() -> [vhost, key, pattern, definition, priority].
+info_keys() -> [component, key, value].
%%---------------------------------------------------------------------------
@@ -320,15 +226,10 @@ lookup_component(Component) ->
{ok, Module} -> {ok, Module}
end.
-format_error(L) ->
- {error_string, rabbit_misc:format_many([{"Validation failed~n", []} | L])}.
-
format(Term) ->
{ok, JSON} = rabbit_misc:json_encode(rabbit_misc:term_to_json(Term)),
list_to_binary(JSON).
-ident(X) -> X.
-
flatten_errors(L) ->
case [{F, A} || I <- lists:flatten([L]), {error, F, A} <- [I]] of
[] -> ok;
diff --git a/src/rabbit_vhost.erl b/src/rabbit_vhost.erl
index 9beedc51..68b04cb1 100644
--- a/src/rabbit_vhost.erl
+++ b/src/rabbit_vhost.erl
@@ -93,15 +93,14 @@ internal_delete(VHostPath) ->
[ok = rabbit_auth_backend_internal:clear_permissions(
proplists:get_value(user, Info), VHostPath)
|| Info <- rabbit_auth_backend_internal:list_vhost_permissions(VHostPath)],
- [ok = rabbit_runtime_parameters:clear_param(
+ [ok = rabbit_runtime_parameters:clear(VHostPath,
+ proplists:get_value(component, Info),
+ proplists:get_value(key, Info))
+ || Info <- rabbit_runtime_parameters:list(VHostPath)],
+ [ok = rabbit_policy:delete(
VHostPath,
- proplists:get_value(component, Info),
proplists:get_value(key, Info))
- || Info <- rabbit_runtime_parameters:list_param(VHostPath)],
- [ok = rabbit_runtime_parameters:clear_policy(
- VHostPath,
- proplists:get_value(key, Info))
- || Info <- rabbit_runtime_parameters:list_policies(VHostPath)],
+ || Info <- rabbit_policy:list(VHostPath)],
ok = mnesia:delete({rabbit_vhost, VHostPath}),
ok.