diff options
| author | Emile Joubert <emile@rabbitmq.com> | 2012-08-23 11:28:59 +0100 |
|---|---|---|
| committer | Emile Joubert <emile@rabbitmq.com> | 2012-08-23 11:28:59 +0100 |
| commit | 4c29d8ce2bb3c7901e14e777fa1299b1362d107f (patch) | |
| tree | c3ded81e5d6e023c39e9e93894a661e4c6c2e377 | |
| parent | de787513d717b673e03e182db1c9a669babefe8a (diff) | |
| download | rabbitmq-server-git-4c29d8ce2bb3c7901e14e777fa1299b1362d107f.tar.gz | |
Match policies by regex instead of prefix
| -rw-r--r-- | src/rabbit_parameter_validation.erl | 9 | ||||
| -rw-r--r-- | src/rabbit_policy.erl | 13 |
2 files changed, 17 insertions, 5 deletions
diff --git a/src/rabbit_parameter_validation.erl b/src/rabbit_parameter_validation.erl index af940dde97..0247643d45 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 05b43a2ee1..3ed0734bfa 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}]. |
