Skip to content

Commit

Permalink
NodeName is converted to an atom automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekwegr committed Dec 15, 2022
1 parent 429d930 commit f6ab093
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
5 changes: 5 additions & 0 deletions big_tests/tests/graphql_mnesia_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ admin_mnesia_tests() ->
get_all_info_test,
install_fallback_error_test,
set_master_test,
set_master_self_test,
set_master_bad_name_test,
set_master_empty_name_test].

Expand Down Expand Up @@ -267,6 +268,10 @@ set_master_test(Config) ->
ParsedRes = get_ok_value([data, mnesia, setMaster], set_master(mim(), Config)),
?assertEqual(<<"Master node set">>, ParsedRes).

set_master_self_test(Config) ->
ParsedRes = get_ok_value([data, mnesia, setMaster], set_master(#{node => self}, Config)),
?assertEqual(<<"Master node set">>, ParsedRes).

set_master_bad_name_test(Config) ->
Res = set_master(#{node => incorrect_name}, Config),
get_coercion_err_msg(Res).
Expand Down
2 changes: 1 addition & 1 deletion priv/graphql/schemas/global/scalar_types.gql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ scalar RoomName @spectaql(options: [{ key: "example", value: "my-chat-room" }])
scalar DomainName @spectaql(options: [{ key: "example", value: "localhost" }])
"XMPP resource name (resource part of a JID)"
scalar ResourceName @spectaql(options: [{ key: "example", value: "res1" }])
"Name of the Erlang node"
"Name of the Erlang node. The value **self** is used to refer to the current node"
scalar NodeName @spectaql(options: [{key: "example", value: "mynode@localhost"}])
"String that contains at least one character"
scalar NonEmptyString @spectaql(options: [{ key: "example", value: "xyz789" }])
Expand Down
4 changes: 2 additions & 2 deletions src/ejabberd_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ commands() ->
longdesc = "If you provide as nodename \"self\", this "
"node will be set as its own master.",
module = mnesia_api, function = set_master,
args = [{nodename, string}], result = {res, restuple}},
args = [{nodename, atom}], result = {res, restuple}},
#ejabberd_commands{name = mnesia_change_nodename, tags = [mnesia],
desc = "Change the erlang node name in a backup file",
module = mnesia_api, function = mnesia_change_nodename,
args = [{oldnodename, string}, {newnodename, string},
args = [{oldnodename, atom}, {newnodename, atom},
{oldbackup, string}, {newbackup, string}],
result = {res, restuple}},
#ejabberd_commands{name = backup, tags = [mnesia],
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
-include("mongoose.hrl").

%% Allowed types for arguments are integer, string, tuple and list.
-type atype() :: integer | string | binary | {tuple, [aterm()]} | {list, aterm()}.
-type atype() :: integer | string | atom | binary | {tuple, [aterm()]} | {list, aterm()}.

%% A rtype is either an atom or a tuple with two elements.
-type rtype() :: integer | string | atom | binary | {tuple, [rterm()]}
Expand Down
11 changes: 7 additions & 4 deletions src/ejabberd_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
-include("ejabberd_commands.hrl").
-include("mongoose_logger.hrl").

-type format() :: integer | string | binary | {list, format()}.
-type format_type() :: binary() | string() | char().
-type format() :: integer | string | atom | binary | {list, format()}.
-type format_type() :: binary() | string() | char() | node().
-type cmd() :: {CallString :: string(), Args :: [string()], Desc :: string()}.

-define(ASCII_SPACE_CHARACTER, $\s).
Expand Down Expand Up @@ -345,8 +345,11 @@ format_arg(Arg, string) ->
format_arg(Arg, binary) ->
list_to_binary(format_arg(Arg, string));
format_arg(Arg, {list, Type}) ->
[format_arg(Token, Type) || Token <- string:tokens(Arg, ";")].
[format_arg(Token, Type) || Token <- string:tokens(Arg, ";")];
format_arg("self", atom) ->
node();
format_arg(Arg, atom) ->
list_to_atom(Arg).
%% @private
-spec format_arg2(Arg :: string(),
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include("../mongoose_graphql_types.hrl").

execute(_Ctx, mnesia, <<"setMaster">>, #{<<"node">> := Node}) ->
case mnesia_api:set_master(binary_to_list(Node)) of
case mnesia_api:set_master(Node) of
{ok, _} -> {ok, "Master node set"};
Error -> make_error(Error, #{node => Node})
end;
Expand All @@ -21,7 +21,7 @@ execute(_Ctx, mnesia, <<"backup">>, #{<<"path">> := Path}) ->
end;
execute(_Ctx, mnesia, <<"changeNodename">>, #{<<"fromString">> := FromString,
<<"toString">> := ToString, <<"source">> := Source, <<"target">> := Target}) ->
case mnesia_api:mnesia_change_nodename(binary_to_list(FromString), binary_to_list(ToString),
case mnesia_api:mnesia_change_nodename(FromString, ToString,
binary_to_list(Source), binary_to_list(Target)) of
{ok, _} -> {ok, "Name of the node in the backup was successfully changed"};
Error -> make_error(Error, #{fromString => FromString, toString => ToString,
Expand Down
4 changes: 3 additions & 1 deletion src/graphql/mongoose_graphql_scalar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ node_from_binary(<<>>) ->
node_from_binary(NodeName) ->
case string:lexemes(binary_to_list(NodeName), "@") of
[_Name, _Host] ->
{ok, NodeName};
{ok, binary_to_atom(NodeName)};
["self"] ->
{ok, node()};
_ ->
{error, incorrect_node_name}
end.
Expand Down
15 changes: 4 additions & 11 deletions src/mnesia_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,8 @@ load_mnesia(Path) ->
{cannot_load, String}
end.

-spec mnesia_change_nodename(string(), string(), _, _) ->
{ok, _} | {change_error(), io_lib:chars()}.
mnesia_change_nodename(FromString, ToString, Source, Target) ->
From = list_to_atom(FromString),
To = list_to_atom(ToString),
-spec mnesia_change_nodename(node(), node(), _, _) -> {ok, _} | {change_error(), io_lib:chars()}.
mnesia_change_nodename(From, To, Source, Target) ->
Switch =
fun
(Node) when Node == From ->
Expand Down Expand Up @@ -175,12 +172,8 @@ install_fallback_mnesia(Path) ->
{cannot_fallback, String}
end.

-spec set_master(Node :: atom() | string()) -> {cannot_set, io_lib:chars()} | {ok, []}.
set_master("self") ->
set_master(node());
set_master(NodeString) when is_list(NodeString) ->
set_master(list_to_atom(NodeString));
set_master(Node) when is_atom(Node) ->
-spec set_master(node()) -> {cannot_set, io_lib:chars()} | {ok, []}.
set_master(Node) ->
case mnesia:set_master_nodes([Node]) of
ok ->
{ok, ""};
Expand Down

0 comments on commit f6ab093

Please sign in to comment.