diff options
author | Emile Joubert <emile@rabbitmq.com> | 2012-10-09 15:44:47 +0100 |
---|---|---|
committer | Emile Joubert <emile@rabbitmq.com> | 2012-10-09 15:44:47 +0100 |
commit | 3b6b74c13ebd0490ccfe85e7a1c25d22bd1dd598 (patch) | |
tree | fec7a51b6be6290c1b730c3e11fc16713c7449f0 | |
parent | c0db9df803e28355acf8978923061473e0f302fe (diff) | |
download | rabbitmq-server-3b6b74c13ebd0490ccfe85e7a1c25d22bd1dd598.tar.gz |
Simpler policy interface
-rw-r--r-- | docs/rabbitmqctl.1.xml | 19 | ||||
-rw-r--r-- | src/rabbit_control_main.erl | 9 | ||||
-rw-r--r-- | src/rabbit_runtime_parameters.erl | 26 |
3 files changed, 40 insertions, 14 deletions
diff --git a/docs/rabbitmqctl.1.xml b/docs/rabbitmqctl.1.xml index eea42484..564af9fd 100644 --- a/docs/rabbitmqctl.1.xml +++ b/docs/rabbitmqctl.1.xml @@ -905,7 +905,7 @@ </para> <variablelist> <varlistentry> - <term><cmdsynopsis><command>set_policy</command> <arg choice="opt">-p <replaceable>vhostpath</replaceable></arg> <arg choice="req"><replaceable>key</replaceable></arg> <arg choice="req"><replaceable>value</replaceable></arg></cmdsynopsis></term> + <term><cmdsynopsis><command>set_policy</command> <arg choice="opt">-p <replaceable>vhostpath</replaceable></arg> <arg choice="req"><replaceable>key</replaceable></arg> <arg choice="req"><replaceable>pattern</replaceable></arg> <arg choice="req"><replaceable>definition</replaceable></arg> <arg choice="opt"><replaceable>priority</replaceable></arg> </cmdsynopsis></term> <listitem> <para> Sets a policy. @@ -918,17 +918,28 @@ </para></listitem> </varlistentry> <varlistentry> - <term>value</term> + <term>pattern</term> + <listitem><para> + The regular expression, which when matches on a given resources causes the policy to apply. + </para></listitem> + </varlistentry> + <varlistentry> + <term>definition</term> <listitem><para> The definition of the policy, as a JSON string. In most shells you are very likely to need to quote this. </para></listitem> </varlistentry> + <varlistentry> + <term>priority</term> + <listitem><para> + The priority of the policy as an integer, defaulting to 0. Higher numbers indicate greater precedence. + </para></listitem> + </varlistentry> </variablelist> <para role="example-prefix">For example:</para> - <screen role="example">rabbitmqctl set_policy federate-me '{"pattern":"^amq.",\ - "policy":{"federation-upstream-set":"all"}}'</screen> + <screen role="example">rabbitmqctl set_policy federate-me "^amq." '{"federation-upstream-set":"all"}'</screen> <para role="example"> This command sets the policy <command>federate-me</command> in the default virtual host so that built-in exchanges are federated. </para> diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl index 15fa1fd5..473f4237 100644 --- a/src/rabbit_control_main.erl +++ b/src/rabbit_control_main.erl @@ -462,11 +462,14 @@ 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) -> +action(set_policy, Node, [Key, Pattern, Defn], Opts, Inform) -> + action(set_policy, Node, [Key, Pattern, Defn, default], Opts, Inform); + +action(set_policy, Node, [Key, Pattern, Defn, Priority], Opts, Inform) -> VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)), - Inform("Setting policy ~p to ~p", [Key, Value]), + Inform("Setting policy ~p to ~p", [Key, Defn]), rpc_call(Node, rabbit_runtime_parameters, parse_set_policy, - [VHostArg, list_to_binary(Key), Value]); + [VHostArg, list_to_binary(Key), Pattern, Defn, Priority]); action(clear_policy, Node, [Key], Opts, Inform) -> VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)), diff --git a/src/rabbit_runtime_parameters.erl b/src/rabbit_runtime_parameters.erl index 51ccab89..f814e0f0 100644 --- a/src/rabbit_runtime_parameters.erl +++ b/src/rabbit_runtime_parameters.erl @@ -18,7 +18,7 @@ -include("rabbit.hrl"). --export([parse_set/4, parse_set_policy/3, set/4, set_policy/3, clear/3, +-export([parse_set/4, parse_set_policy/5, set/4, set_policy/3, clear/3, clear_policy/2, list/0, list/1, list_strict/1, list/2, list_strict/2, list_policies/0, list_policies/1, list_formatted/1, list_formatted_policies/1, lookup/3, value/3, value/4, info_keys/0]). @@ -31,8 +31,8 @@ -spec(parse_set/4 :: (rabbit_types:vhost(), binary(), binary(), string()) -> ok_or_error_string()). --spec(parse_set_policy/3 :: (rabbit_types:vhost(), binary(), string()) - -> ok_or_error_string()). +-spec(parse_set_policy/5 :: (rabbit_types:vhost(), binary(), string(), string(), + string()) -> ok_or_error_string()). -spec(set/4 :: (rabbit_types:vhost(), binary(), binary(), term()) -> ok_or_error_string()). -spec(set_policy/3 :: (rabbit_types:vhost(), binary(), term()) @@ -74,11 +74,23 @@ parse_set(VHost, Component, Key, String) -> error -> {error_string, "JSON decoding error"} end. -parse_set_policy(VHost, Key, String) -> - case rabbit_misc:json_decode(String) of +parse_set_policy(VHost, Key, Pat, Defn, default) -> + 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_policy(VHost, Key, rabbit_misc:json_to_term(JSON)); - error -> + set_policy(VHost, Key, pset(<<"pattern">>, list_to_binary(Pattern), + pset(<<"policy">>, + rabbit_misc:json_to_term(JSON), + Priority)), + error -> {error_string, "JSON decoding error"} end. |