diff options
| author | Simon MacMullen <simon@rabbitmq.com> | 2012-10-04 10:39:44 +0100 |
|---|---|---|
| committer | Simon MacMullen <simon@rabbitmq.com> | 2012-10-04 10:39:44 +0100 |
| commit | 6569080b1126ba41a967847015451f94d81d7cd5 (patch) | |
| tree | 978db165a162895e3c1e744c5ea10cb3eed7cbbe | |
| parent | 12558dfe9c50017cf3e3b009805d2ba911b2e9e3 (diff) | |
| parent | 74cb4d5975c10bf2ca2275157999798626e14d6e (diff) | |
| download | rabbitmq-server-git-6569080b1126ba41a967847015451f94d81d7cd5.tar.gz | |
Merge default
| -rw-r--r-- | src/rabbit_control_main.erl | 24 | ||||
| -rw-r--r-- | src/rabbit_policy.erl | 22 | ||||
| -rw-r--r-- | src/rabbit_policy_validator.erl | 37 | ||||
| -rw-r--r-- | src/rabbit_registry.erl | 3 | ||||
| -rw-r--r-- | src/rabbit_runtime_parameters.erl | 11 | ||||
| -rw-r--r-- | src/rabbit_runtime_parameters_test.erl | 22 | ||||
| -rw-r--r-- | src/rabbit_tests.erl | 17 |
7 files changed, 132 insertions, 4 deletions
diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl index e75e1f6f7c..1efde13686 100644 --- a/src/rabbit_control_main.erl +++ b/src/rabbit_control_main.erl @@ -70,6 +70,10 @@ {clear_parameter, [?VHOST_DEF]}, {list_parameters, [?VHOST_DEF]}, + {set_policy, [?VHOST_DEF]}, + {clear_policy, [?VHOST_DEF]}, + {list_policies, [?VHOST_DEF]}, + {list_queues, [?VHOST_DEF]}, {list_exchanges, [?VHOST_DEF]}, {list_bindings, [?VHOST_DEF]}, @@ -458,6 +462,26 @@ action(list_parameters, Node, [], Opts, Inform) -> rpc_call(Node, rabbit_runtime_parameters, list_formatted, [VHostArg]), rabbit_runtime_parameters:info_keys()); +action(set_policy, Node, [Key, Value], Opts, Inform) -> + VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)), + Inform("Setting policy ~p to ~p", [Key, Value]), + rpc_call(Node, rabbit_runtime_parameters, parse_set, + [VHostArg, <<"policy">>, list_to_binary(Key), Value]); + +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, [VHostArg, + <<"policy">>, + 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() -- [component]); + action(report, Node, _Args, _Opts, Inform) -> Inform("Reporting server status on ~p~n~n", [erlang:universaltime()]), [begin ok = action(Action, N, [], [], Inform), io:nl() end || diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl index f4c1f42b21..c9af46e99c 100644 --- a/src/rabbit_policy.erl +++ b/src/rabbit_policy.erl @@ -80,6 +80,7 @@ notify_clear(VHost, <<"policy">>, _Name) -> %%---------------------------------------------------------------------------- + list(VHost) -> [[{<<"name">>, pget(key, P)} | pget(value, P)] || P <- rabbit_runtime_parameters:list(VHost, <<"policy">>)]. @@ -136,4 +137,23 @@ sort_pred(A, B) -> pget(<<"priority">>, A, 0) >= pget(<<"priority">>, B, 0). policy_validation() -> [{<<"priority">>, fun rabbit_parameter_validation:number/2, optional}, {<<"pattern">>, fun rabbit_parameter_validation:regex/2, mandatory}, - {<<"policy">>, fun rabbit_parameter_validation:list/2, mandatory}]. + {<<"policy">>, fun validation/2, mandatory}]. + +validation(_Name, Terms) when is_list(Terms) -> + [validation0(T) || T <- Terms ]; +validation(Name, Term) -> + {error, "~s should be list, actually was ~p", [Name, Term]}. + +validation0({Key, Value}) when is_binary(Key) -> + case rabbit_registry:lookup_module(policy_validator, + list_to_atom(binary_to_list(Key))) of + {ok, Mod} -> + Mod:validate_policy(Key, Value); + {error, not_found} -> + {error, "~p is not a recognised policy option", [Key]}; + Error -> + Error + end; +validation0(Term) -> + {error, "parse error while reading policy: ~p", [Term]}. + diff --git a/src/rabbit_policy_validator.erl b/src/rabbit_policy_validator.erl new file mode 100644 index 0000000000..3cc02ecc33 --- /dev/null +++ b/src/rabbit_policy_validator.erl @@ -0,0 +1,37 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is VMware, Inc. +%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved. +%% + +-module(rabbit_policy_validator). + +-ifdef(use_specs). + +-type(validate_results() :: + 'ok' | {error, string(), [term()]} | [validate_results()]). + +-callback validate_policy(binary(), term()) -> validate_results(). + +-else. + +-export([behaviour_info/1]). + +behaviour_info(callbacks) -> + [ + {validate_policy, 2}, + ]; +behaviour_info(_Other) -> + undefined. + +-endif. diff --git a/src/rabbit_registry.erl b/src/rabbit_registry.erl index e14bbba018..32709d2484 100644 --- a/src/rabbit_registry.erl +++ b/src/rabbit_registry.erl @@ -107,7 +107,8 @@ sanity_check_module(ClassModule, Module) -> class_module(exchange) -> rabbit_exchange_type; class_module(auth_mechanism) -> rabbit_auth_mechanism; class_module(runtime_parameter) -> rabbit_runtime_parameter; -class_module(exchange_decorator) -> rabbit_exchange_decorator. +class_module(exchange_decorator) -> rabbit_exchange_decorator; +class_module(policy_validator) -> rabbit_policy_validator. %%--------------------------------------------------------------------------- diff --git a/src/rabbit_runtime_parameters.erl b/src/rabbit_runtime_parameters.erl index b58b459a7f..c4608b427f 100644 --- a/src/rabbit_runtime_parameters.erl +++ b/src/rabbit_runtime_parameters.erl @@ -20,7 +20,7 @@ -export([parse_set/4, set/4, clear/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]). + list_formatted_policies/1, lookup/3, value/3, value/4, info_keys/0]). %%---------------------------------------------------------------------------- @@ -41,6 +41,8 @@ -spec(list_strict/2 :: (rabbit_types:vhost(), binary()) -> [rabbit_types:infos()] | 'not_found'). -spec(list_formatted/1 :: (rabbit_types:vhost()) -> [rabbit_types:infos()]). +-spec(list_formatted_policies/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()). @@ -141,7 +143,12 @@ list(VHost, Component, Default) -> end. list_formatted(VHost) -> - [pset(value, format(pget(value, P)), P) || P <- list(VHost)]. + [pset(value, format(pget(value, P)), P) + || P <- list(VHost), pget(component, P) /= <<"policy">>]. + +list_formatted_policies(VHost) -> + [proplists:delete(component, pset(value, format(pget(value, P)), P)) + || P <- list(VHost), pget(component, P) == <<"policy">>]. lookup(VHost, Component, Key) -> case lookup0(VHost, Component, Key, rabbit_misc:const(not_found)) of diff --git a/src/rabbit_runtime_parameters_test.erl b/src/rabbit_runtime_parameters_test.erl index 5224ccaa36..4ac19ff142 100644 --- a/src/rabbit_runtime_parameters_test.erl +++ b/src/rabbit_runtime_parameters_test.erl @@ -16,9 +16,14 @@ -module(rabbit_runtime_parameters_test). -behaviour(rabbit_runtime_parameter). +-behaviour(rabbit_policy_validator). -export([validate/4, validate_clear/3, notify/4, notify_clear/3]). -export([register/0, unregister/0]). +-export([validate_policy/2]). +-export([register_policy_validator/0, unregister_policy_validator/0]). + +%---------------------------------------------------------------------------- register() -> rabbit_registry:register(runtime_parameter, <<"test">>, ?MODULE). @@ -36,3 +41,20 @@ validate_clear(_, <<"test">>, _) -> {error, "meh", []}. notify(_, _, _, _) -> ok. notify_clear(_, _, _) -> ok. + +%---------------------------------------------------------------------------- + +register_policy_validator() -> + rabbit_registry:register(policy_validator, <<"testpolicy">>, ?MODULE). + +unregister_policy_validator() -> + rabbit_registry:unregister(policy_validator, <<"testpolicy">>). + +validate_policy(<<"testpolicy">>, Terms) when is_list(Terms) -> + rabbit_log:info("pol val ~p~n", [Terms]), + case length(Terms) rem 2 =:= 0 of + true -> ok; + false -> {error, "meh", []} + end; +validate_policy(<<"testpolicy">>, _) -> + {error, "meh", []}. diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl index 11f280bb7f..a7eab2d522 100644 --- a/src/rabbit_tests.erl +++ b/src/rabbit_tests.erl @@ -57,6 +57,7 @@ all_tests() -> passed = test_dynamic_mirroring(), passed = test_user_management(), passed = test_runtime_parameters(), + passed = test_policy_validation(), passed = test_server_status(), passed = test_confirms(), passed = @@ -1028,6 +1029,22 @@ test_runtime_parameters() -> rabbit_runtime_parameters_test:unregister(), passed. +test_policy_validation() -> + rabbit_runtime_parameters_test:register_policy_validator(), + SetPol = fun (Pol, Val) -> + control_action( + set_policy, + ["name", lists:flatten( + io_lib:format("{\"pattern\":\"pat\", \"policy\":" + "{\"~s\":~p}}", [Pol, Val]))]) + end, + ok = SetPol("testpolicy", []), + ok = SetPol("testpolicy", [1, 2]), + ok = SetPol("testpolicy", [1, 2, 3, 4]), + {error_string, _} = SetPol("testpolicy", [1, 2, 3]), + {error_string, _} = SetPol("not_registered", []), + rabbit_runtime_parameters_test:unregister_policy_validator(). + test_server_status() -> %% create a few things so there is some useful information to list Writer = spawn(fun () -> receive shutdown -> ok end end), |
