Skip to content

Commit 0c2755f

Browse files
authored
Merge pull request #4597 from esl/tomasz/mim-2527-all-domains
Add allDomains GraphQL query
2 parents 8a90671 + dbf2d6a commit 0c2755f

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

big_tests/tests/graphql_domain_SUITE.erl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ domain_tests() ->
4141
wrong_host_type_error_formatting,
4242
invalid_domain_name_error,
4343
disable_domain,
44+
get_all_domains_with_disabled,
4445
enable_domain,
4546
get_domains_by_host_type,
47+
get_all_domains,
4648
get_domain_details,
4749
delete_domain,
4850
request_delete_domain,
@@ -60,6 +62,7 @@ domain_admin_tests() ->
6062
domain_admin_disable_domain_no_permission,
6163
domain_admin_enable_domain_no_permission,
6264
domain_admin_get_domains_by_host_type_no_permission,
65+
domain_admin_get_all_domains_no_permission,
6366
domain_admin_get_domain_details_no_permission,
6467
domain_admin_delete_domain_no_permission,
6568
domain_admin_set_domain_password_no_permission,
@@ -94,9 +97,15 @@ end_per_group(domain_admin_tests, _Config) ->
9497
end_per_group(_GroupName, _Config) ->
9598
graphql_helper:clean().
9699

100+
init_per_testcase(get_all_domains_with_disabled, Config) ->
101+
disable_domain(?EXAMPLE_DOMAIN, Config),
102+
escalus:init_per_testcase(get_all_domains_with_disabled, Config);
97103
init_per_testcase(CaseName, Config) ->
98104
escalus:init_per_testcase(CaseName, Config).
99105

106+
end_per_testcase(get_all_domains_with_disabled, Config) ->
107+
enable_domain(?EXAMPLE_DOMAIN, Config),
108+
escalus:end_per_testcase(get_all_domains_with_disabled, Config);
100109
end_per_testcase(CaseName, Config) ->
101110
escalus:end_per_testcase(CaseName, Config).
102111

@@ -178,6 +187,15 @@ disable_domain(Config) ->
178187
{ok, Domain} = rpc(mim(), mongoose_domain_sql, select_domain, [?EXAMPLE_DOMAIN]),
179188
?assertEqual(#{host_type => ?HOST_TYPE, status => disabled}, Domain).
180189

190+
get_all_domains_with_disabled(Config) ->
191+
Result = execute_command(<<"domain">>, <<"allDomains">>, #{}, Config),
192+
ParsedResult = get_ok_value([data, domain, allDomains], Result),
193+
Expected = [
194+
#{<<"domain">> => ?EXAMPLE_DOMAIN, <<"hostType">> => ?HOST_TYPE, <<"status">> => <<"DISABLED">>},
195+
#{<<"domain">> => ?SECOND_EXAMPLE_DOMAIN, <<"hostType">> => ?HOST_TYPE, <<"status">> => <<"ENABLED">>}
196+
],
197+
lists:foreach(fun(E) -> ?assert(lists:member(E, ParsedResult)) end, Expected).
198+
181199
enable_domain(Config) ->
182200
Result = enable_domain(?EXAMPLE_DOMAIN, Config),
183201
ParsedResult = get_ok_value([data, domain, enableDomain], Result),
@@ -189,6 +207,15 @@ get_domains_by_host_type(Config) ->
189207
?assertEqual(lists:sort([?EXAMPLE_DOMAIN, ?SECOND_EXAMPLE_DOMAIN]),
190208
lists:sort(ParsedResult)).
191209

210+
get_all_domains(Config) ->
211+
Result = execute_command(<<"domain">>, <<"allDomains">>, #{}, Config),
212+
ParsedResult = get_ok_value([data, domain, allDomains], Result),
213+
Expected = [
214+
#{<<"domain">> => ?EXAMPLE_DOMAIN, <<"hostType">> => ?HOST_TYPE, <<"status">> => <<"ENABLED">>},
215+
#{<<"domain">> => ?SECOND_EXAMPLE_DOMAIN, <<"hostType">> => ?HOST_TYPE, <<"status">> => <<"ENABLED">>}
216+
],
217+
lists:foreach(fun(E) -> ?assert(lists:member(E, ParsedResult)) end, Expected).
218+
192219
get_domain_details(Config) ->
193220
Result = get_domain_details(?EXAMPLE_DOMAIN, Config),
194221
ParsedResult = get_ok_value([data, domain, domainDetails], Result),
@@ -276,6 +303,9 @@ domain_admin_get_domains_by_host_type_no_permission(Config) ->
276303
get_unauthorized(get_domains_by_host_type(?HOST_TYPE, Config)),
277304
get_unauthorized(get_domains_by_host_type(domain_helper:host_type(), Config)).
278305

306+
domain_admin_get_all_domains_no_permission(Config) ->
307+
get_unauthorized(execute_command(<<"domain">>, <<"allDomains">>, #{}, Config)).
308+
279309
domain_admin_get_domain_details_no_permission(Config) ->
280310
get_unauthorized(get_domain_details(?DOMAIN_ADMIN_EXAMPLE_DOMAIN, Config)),
281311
get_unauthorized(get_domain_details(?EXAMPLE_DOMAIN, Config)).

big_tests/tests/service_domain_db_SUITE.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ db_cases() -> [
9696
db_can_check_domain_password,
9797
db_cannot_check_password_for_unknown_domain,
9898
db_deleting_domain_deletes_domain_admin,
99+
db_get_all_domains,
99100
sql_select_from,
100101
db_could_sync_between_nodes,
101102
db_gaps_are_getting_filled_automatically
@@ -391,6 +392,22 @@ db_get_all_static(_) ->
391392
{<<"example.cfg">>, <<"type1">>}] =
392393
lists:sort(get_all_static(mim())).
393394

395+
db_get_all_domains(_) ->
396+
Domain1 = random_domain_name(),
397+
Domain2 = random_domain_name(),
398+
{ok, _} = insert_domain(mim(), Domain1, <<"type1">>),
399+
{ok, _} = insert_domain(mim(), Domain2, <<"type1">>),
400+
disable_domain(mim(), Domain1),
401+
sync(),
402+
403+
AllDomains = get_all_domains(mim()),
404+
405+
%% Verify Domain1 is disabled and Domain2 is enabled
406+
[Domain1Map] = [D || D <- AllDomains, maps:get(domain, D) == Domain1],
407+
?assertMatch(#{domain := Domain1, host_type := <<"type1">>, status := disabled}, Domain1Map),
408+
[Domain2Map] = [D || D <- AllDomains, maps:get(domain, D) == Domain2],
409+
?assertMatch(#{domain := Domain2, host_type := <<"type1">>, status := enabled}, Domain2Map).
410+
394411
db_get_all_dynamic(_) ->
395412
Domain1 = random_domain_name(),
396413
Domain2 = random_domain_name(),
@@ -1147,6 +1164,9 @@ get_all_static(Node) ->
11471164
get_all_dynamic(Node) ->
11481165
rpc(Node, mongoose_domain_api, get_all_dynamic, []).
11491166

1167+
get_all_domains(Node) ->
1168+
rpc(Node, mongoose_domain_api, get_all_domains, []).
1169+
11501170
disable_domain(Node, Domain) ->
11511171
rpc(Node, mongoose_domain_api, disable_domain, [Domain]).
11521172

priv/graphql/schemas/admin/domain.gql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ type DomainAdminQuery @use(services: ["service_domain_db"]) @protected{
22
"Get all enabled domains by hostType. Only for global admin"
33
domainsByHostType(hostType: String!): [DomainName!]
44
@protected(type: GLOBAL) @use
5+
"Get all domains with details. Returns a list of Domain objects. Only for global admin"
6+
allDomains: [Domain!]
7+
@protected(type: GLOBAL) @use
58
"Get information about the domain"
69
domainDetails(domain: DomainName!): Domain
710
@protected(type: DOMAIN, args: ["domain"]) @use

src/domain/mongoose_domain_api.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
%% domain API
2323
-export([get_domain_host_type/1,
2424
get_all_static/0,
25-
get_domains_by_host_type/1]).
25+
get_domains_by_host_type/1,
26+
get_all_domains/0]).
2627

2728
%% domain admin API
2829
-export([check_domain_password/2]).
@@ -226,6 +227,10 @@ get_all_dynamic() ->
226227
get_domains_by_host_type(HostType) ->
227228
mongoose_domain_core:get_domains_by_host_type(HostType).
228229

230+
-spec get_all_domains() -> [domain_info()].
231+
get_all_domains() ->
232+
mongoose_domain_sql:select_all_domains().
233+
229234
-type password() :: binary().
230235

231236
-spec check_domain_password(domain(), password()) -> ok | {error, wrong_password | not_found}.

src/domain/mongoose_domain_sql.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
delete_domain_admin/1]).
1313

1414
-export([select_domain/1,
15+
select_all_domains/0,
1516
get_minmax_event_id/0,
1617
count_events_between_ids/2,
1718
get_event_ids_between/2,
@@ -85,6 +86,8 @@ start(_) ->
8586
"domain_settings.status = ", Enabled/binary, ") "
8687
" WHERE domain_events.id >= ? AND domain_events.id <= ? "
8788
" ORDER BY domain_events.id ">>),
89+
prepare(domain_select_all, domain_settings, [],
90+
<<"SELECT domain, host_type, status FROM domain_settings order by domain">>),
8891
%% Admins
8992
prepare(domain_insert_admin, domain_admins, [domain, pass_details],
9093
<<"INSERT INTO domain_admins (domain, pass_details) VALUES (?, ?)">>),
@@ -139,6 +142,13 @@ select_domain(Domain) ->
139142
{ok, row_to_map(Row)}
140143
end.
141144

145+
select_all_domains() ->
146+
Pool = get_db_pool(),
147+
{selected, Rows} = execute_successfully(Pool, domain_select_all, []),
148+
[#{domain => Domain,
149+
host_type => HostType,
150+
status => int_to_status(mongoose_rdbms:result_to_integer(Status))} || {Domain, HostType, Status} <- Rows].
151+
142152
delete_domain(Domain, HostType) ->
143153
transaction(fun(Pool) ->
144154
case select_domain(Domain) of

src/graphql/admin/mongoose_graphql_domain_admin_query.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ execute(_Ctx, admin, <<"domainsByHostType">>, #{<<"hostType">> := HostType}) ->
1717
Error ->
1818
format_result(Error, #{hostType => HostType})
1919
end;
20+
execute(_Ctx, admin, <<"allDomains">>, _Args) ->
21+
Domains = mongoose_domain_api:get_all_domains(),
22+
{ok, [ {ok, D} || D <- Domains ]};
2023
execute(_Ctx, admin, <<"domainDetails">>, #{<<"domain">> := Domain}) ->
2124
format_result(mongoose_domain_api:get_domain_details(Domain), #{domain => Domain}).

0 commit comments

Comments
 (0)