summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmile Joubert <emile@rabbitmq.com>2012-08-23 11:28:59 +0100
committerEmile Joubert <emile@rabbitmq.com>2012-08-23 11:28:59 +0100
commit2b7a86181289078a8a3b407d8df876f111051fec (patch)
treec3ded81e5d6e023c39e9e93894a661e4c6c2e377
parent602437bd972b207ae4bc97358104f60dc4248238 (diff)
downloadrabbitmq-server-2b7a86181289078a8a3b407d8df876f111051fec.tar.gz
Match policies by regex instead of prefix
-rw-r--r--src/rabbit_parameter_validation.erl9
-rw-r--r--src/rabbit_policy.erl13
2 files changed, 17 insertions, 5 deletions
diff --git a/src/rabbit_parameter_validation.erl b/src/rabbit_parameter_validation.erl
index af940dde..0247643d 100644
--- a/src/rabbit_parameter_validation.erl
+++ b/src/rabbit_parameter_validation.erl
@@ -16,7 +16,7 @@
-module(rabbit_parameter_validation).
--export([number/2, binary/2, list/2, proplist/3]).
+-export([number/2, binary/2, list/2, regex/2, proplist/3]).
number(_Name, Term) when is_number(Term) ->
ok;
@@ -36,6 +36,13 @@ list(_Name, Term) when is_list(Term) ->
list(Name, Term) ->
{error, "~s should be list, actually was ~p", [Name, Term]}.
+regex(Name, Term) ->
+ case re:compile(Term) of
+ {ok, _} -> ok;
+ {error, Reason} -> {error, "~s should be regular expression "
+ "but is invalid: ~p", [Name, Reason]}
+ end.
+
proplist(Name, Constraints, Term) when is_list(Term) ->
{Results, Remainder}
= lists:foldl(
diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl
index 05b43a2e..3ed0734b 100644
--- a/src/rabbit_policy.erl
+++ b/src/rabbit_policy.erl
@@ -129,14 +129,19 @@ match(Name, Policies) ->
end.
matches(#resource{name = Name}, Policy) ->
- lists:prefix(binary_to_list(pget(<<"prefix">>, Policy)),
- binary_to_list(Name)).
+ case re:run(binary_to_list(Name),
+ binary_to_list(pget(<<"pattern">>, Policy)),
+ [{capture, none}]) of
+ nomatch -> false;
+ match -> true
+ end.
sort_pred(A, B) ->
- size(pget(<<"prefix">>, A)) >= size(pget(<<"prefix">>, B)).
+ pget(<<"priority">>, A) >= pget(<<"priority">>, B).
%%----------------------------------------------------------------------------
policy_validation() ->
- [{<<"prefix">>, fun rabbit_parameter_validation:binary/2, mandatory},
+ [{<<"priority">>, fun rabbit_parameter_validation:number/2, mandatory},
+ {<<"pattern">>, fun rabbit_parameter_validation:regex/2, mandatory},
{<<"policy">>, fun rabbit_parameter_validation:list/2, mandatory}].