summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2012-03-21 16:38:29 +0000
committerSimon MacMullen <simon@rabbitmq.com>2012-03-21 16:38:29 +0000
commit4749890623cda7aa133bfeb338713deec15dab7a (patch)
tree5f6ecaf3df0483ba38a66e0df54b5bf16c35ba37
parent5810f5d09bf6f5ed75165ff0b657aed2b4d2794f (diff)
downloadrabbitmq-server-4749890623cda7aa133bfeb338713deec15dab7a.tar.gz
Generic validation.
-rw-r--r--src/rabbit_cluster_config.erl40
-rw-r--r--src/rabbit_cluster_config_item.erl2
-rw-r--r--src/rabbit_control.erl10
3 files changed, 34 insertions, 18 deletions
diff --git a/src/rabbit_cluster_config.erl b/src/rabbit_cluster_config.erl
index ca2e6037..d4a84395 100644
--- a/src/rabbit_cluster_config.erl
+++ b/src/rabbit_cluster_config.erl
@@ -25,7 +25,8 @@
set(AppName, Key, Value) ->
Module = lookup_app(AppName),
Term = parse(Value),
- Module:validate(Term),
+ validate(Term),
+ Module:validate(Key, Term),
rabbit_misc:execute_mnesia_transaction(
fun () ->
ok = mnesia:write(
@@ -52,20 +53,14 @@ info_keys() -> [app_name, key, value].
%%---------------------------------------------------------------------------
-lookup_app(AppBin) ->
- case rabbit_registry:binary_to_type(AppBin) of
- {error, not_found} ->
- exit({not_found, AppBin});
- T ->
- case rabbit_registry:lookup_module(cluster_config, T) of
- {error, not_found} -> exit({not_found, T});
- {ok, Module} -> Module
- end
+lookup_app(App) ->
+ case rabbit_registry:lookup_module(cluster_config, App) of
+ {error, not_found} -> exit({application_not_found, App});
+ {ok, Module} -> Module
end.
-
-parse(Bin) ->
- case erl_scan:string(binary_to_list(Bin)) of
+parse(Src) ->
+ case erl_scan:string(Src) of
{ok, Scanned, _} ->
case erl_parse:parse_term(Scanned) of
{ok, Parsed} ->
@@ -79,3 +74,22 @@ parse(Bin) ->
format_parse_error({_Line, Mod, Err}) ->
lists:flatten(Mod:format_error(Err)).
+
+%%---------------------------------------------------------------------------
+
+%% We will want to be able to biject these to JSON. So we have some
+%% generic restrictions on what we consider acceptable.
+validate(Proplist = [T | _]) when is_tuple(T) -> validate_proplist(Proplist);
+validate(L) when is_list(L) -> [validate(I) || I <- L];
+validate(T) when is_tuple(T) -> exit({tuple, T});
+validate(true) -> ok;
+validate(false) -> ok;
+validate(A) when is_atom(A) -> exit({non_bool_atom, A});
+validate(N) when is_number(N) -> ok;
+validate(B) when is_binary(B) -> ok.
+
+validate_proplist([]) -> ok;
+validate_proplist([{K, V} | Rest]) when is_atom(K) -> validate(V),
+ validate_proplist(Rest);
+validate_proplist([{K, _V} | _Rest]) -> exit({non_atom_key, K});
+validate_proplist([H | _Rest]) -> exit({not_two_tuple, H}).
diff --git a/src/rabbit_cluster_config_item.erl b/src/rabbit_cluster_config_item.erl
index deccc1d4..4ab903ea 100644
--- a/src/rabbit_cluster_config_item.erl
+++ b/src/rabbit_cluster_config_item.erl
@@ -20,7 +20,7 @@
behaviour_info(callbacks) ->
[
- {validate, 1}
+ {validate, 2}
];
behaviour_info(_Other) ->
undefined.
diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl
index 8b3974e7..671e354d 100644
--- a/src/rabbit_control.erl
+++ b/src/rabbit_control.erl
@@ -267,13 +267,15 @@ action(list_user_permissions, Node, Args = [_Username], _Opts, Inform) ->
list_user_permissions, Args}),
rabbit_auth_backend_internal:user_perms_info_keys());
-action(set_config_item, Node, Args = [AppName, Key, Value], _Opts, Inform) ->
+action(set_config_item, Node, [AppName, Key, Value], _Opts, Inform) ->
Inform("Setting config item ~p for app ~p to ~p", [Key, AppName, Value]),
- call(Node, {rabbit_cluster_config, set, Args});
+ rpc_call(Node, rabbit_cluster_config, set, [list_to_atom(AppName),
+ list_to_atom(Key), Value]);
-action(clear_config_item, Node, Args = [AppName, Key], _Opts, Inform) ->
+action(clear_config_item, Node, [AppName, Key], _Opts, Inform) ->
Inform("Clearing config item ~p for app ~p", [Key, AppName]),
- call(Node, {rabbit_cluster_config, clear, Args});
+ rpc_call(Node, rabbit_cluster_config, clear, [list_to_atom(AppName),
+ list_to_atom(Key)]);
action(list_config_items, Node, Args = [], _Opts, Inform) ->
Inform("Listing config items", []),