./ct_report/coverage/mongoose_domain_api.COVER.html

1 %% Main module other parts of MongooseIM should use to access the domain
2 %% management.
3 -module(mongoose_domain_api).
4
5 -export([init/0,
6 stop/0,
7 get_host_type/1]).
8
9 %% domain API
10 -export([insert_domain/2,
11 delete_domain/2,
12 disable_domain/1,
13 enable_domain/1,
14 get_domain_host_type/1,
15 get_all_static/0,
16 get_domains_by_host_type/1]).
17
18 %% subdomain API
19 -export([register_subdomain/3,
20 unregister_subdomain/2,
21 get_subdomain_host_type/1,
22 get_subdomain_info/1,
23 get_all_subdomains_for_domain/1]).
24
25 %% For testing
26 -export([get_all_dynamic/0]).
27
28 -ignore_xref([get_all_static/0]).
29 -ignore_xref([get_all_dynamic/0]).
30 -ignore_xref([stop/0]).
31
32 -type domain() :: jid:lserver().
33 -type host_type() :: mongooseim:host_type().
34 -type pair() :: {domain(), host_type()}.
35 -type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern().
36
37
38 -spec init() -> ok | {error, term()}.
39 init() ->
40 80 Pairs = get_static_pairs(),
41 80 AllowedHostTypes = mongoose_config:get_opt(host_types),
42 80 mongoose_domain_core:start(Pairs, AllowedHostTypes),
43 80 mongoose_subdomain_core:start(),
44 80 mongoose_lazy_routing:start().
45
46 %% Stops gen_servers, that are started from init/0
47 %% Does not fail, even if servers are already stopped
48 -spec stop() -> ok.
49 stop() ->
50
:-(
catch mongoose_domain_core:stop(),
51
:-(
catch mongoose_subdomain_core:stop(),
52
:-(
catch mongoose_lazy_routing:stop(),
53
:-(
ok.
54
55 %% Domain should be nameprepped using `jid:nameprep'.
56 -spec insert_domain(domain(), host_type()) ->
57 ok | {error, duplicate} | {error, static} | {error, {db_error, term()}}
58 | {error, service_disabled} | {error, unknown_host_type}.
59 insert_domain(Domain, HostType) ->
60 1 case check_domain(Domain, HostType) of
61 ok ->
62
:-(
check_db(mongoose_domain_sql:insert_domain(Domain, HostType));
63 Other ->
64 1 Other
65 end.
66
67 %% Returns ok, if domain not found.
68 %% Domain should be nameprepped using `jid:nameprep'.
69 -spec delete_domain(domain(), host_type()) ->
70 ok | {error, static} | {error, {db_error, term()}}
71 | {error, service_disabled} | {error, wrong_host_type} | {error, unknown_host_type}.
72 delete_domain(Domain, HostType) ->
73
:-(
case check_domain(Domain, HostType) of
74 ok ->
75
:-(
Res = check_db(mongoose_domain_sql:delete_domain(Domain, HostType)),
76
:-(
case Res of
77 ok ->
78
:-(
mongoose_hooks:remove_domain(HostType, Domain);
79 _ ->
80
:-(
ok
81 end,
82
:-(
Res;
83 Other ->
84
:-(
Other
85 end.
86
87 -spec disable_domain(domain()) ->
88 ok | {error, not_found} | {error, static} | {error, service_disabled}
89 | {error, {db_error, term()}}.
90 disable_domain(Domain) ->
91 1 case mongoose_domain_core:is_static(Domain) of
92 true ->
93 1 {error, static};
94 false ->
95
:-(
case service_domain_db:enabled() of
96 true ->
97
:-(
check_db(mongoose_domain_sql:disable_domain(Domain));
98 false ->
99
:-(
{error, service_disabled}
100 end
101 end.
102
103 -spec enable_domain(domain()) ->
104 ok | {error, not_found} | {error, static} | {error, service_disabled}
105 | {error, {db_error, term()}}.
106 enable_domain(Domain) ->
107 1 case mongoose_domain_core:is_static(Domain) of
108 true ->
109 1 {error, static};
110 false ->
111
:-(
case service_domain_db:enabled() of
112 true ->
113
:-(
check_db(mongoose_domain_sql:enable_domain(Domain));
114 false ->
115
:-(
{error, service_disabled}
116 end
117 end.
118
119 check_db(ok) ->
120 %% Speedup the next check. %% It's async.
121
:-(
service_domain_db:force_check_for_updates(),
122
:-(
ok;
123 check_db(Result) ->
124
:-(
Result.
125
126 %% Domain should be nameprepped using `jid:nameprep'
127 -spec get_host_type(domain()) ->
128 {ok, host_type()} | {error, not_found}.
129 get_host_type(Domain) ->
130 19228 case get_domain_host_type(Domain) of
131 17178 {ok, HostType} -> {ok, HostType};
132 {error, not_found} ->
133 2050 get_subdomain_host_type(Domain)
134 end.
135
136 %% Domain should be nameprepped using `jid:nameprep'
137 -spec get_domain_host_type(domain()) ->
138 {ok, host_type()} | {error, not_found}.
139 get_domain_host_type(Domain) ->
140 34150 mongoose_domain_core:get_host_type(Domain).
141
142 %% Subdomain should be nameprepped using `jid:nameprep'
143 -spec get_subdomain_host_type(domain()) ->
144 {ok, host_type()} | {error, not_found}.
145 get_subdomain_host_type(Subdomain) ->
146 2475 mongoose_subdomain_core:get_host_type(Subdomain).
147
148 %% Subdomain should be nameprepped using `jid:nameprep'
149 -spec get_subdomain_info(domain()) ->
150 {ok, mongoose_subdomain_core:subdomain_info()} | {error, not_found}.
151 get_subdomain_info(Subdomain) ->
152 896 mongoose_subdomain_core:get_subdomain_info(Subdomain).
153
154 %% Get the list of the host_types provided during initialisation
155 %% This has complexity N, where N is the number of online domains.
156 -spec get_all_static() -> [{domain(), host_type()}].
157 get_all_static() ->
158 1 mongoose_domain_core:get_all_static().
159
160 %% Get domains, loaded from DB to this node
161 -spec get_all_dynamic() -> [{domain(), host_type()}].
162 get_all_dynamic() ->
163
:-(
mongoose_domain_core:get_all_dynamic().
164
165 %% Get the list of the host_types provided during initialisation
166 %% This has complexity N, where N is the number of online domains.
167 -spec get_domains_by_host_type(host_type()) -> [domain()].
168 get_domains_by_host_type(HostType) ->
169 27 mongoose_domain_core:get_domains_by_host_type(HostType).
170
171 check_domain(Domain, HostType) ->
172 1 Static = mongoose_domain_core:is_static(Domain),
173 1 Allowed = mongoose_domain_core:is_host_type_allowed(HostType),
174 1 HasDb = service_domain_db:enabled(),
175 1 if
176 Static ->
177 1 {error, static};
178 not Allowed ->
179
:-(
{error, unknown_host_type};
180 not HasDb ->
181
:-(
{error, service_disabled};
182 true ->
183
:-(
ok
184 end.
185
186 %% Domains should be nameprepped using `jid:nameprep'
187 -spec get_static_pairs() -> [pair()].
188 get_static_pairs() ->
189 80 [{H, H} || H <- mongoose_config:get_opt(hosts)].
190
191 -spec register_subdomain(host_type(), subdomain_pattern(),
192 mongoose_packet_handler:t()) ->
193 ok | {error, already_registered | subdomain_already_exists}.
194 register_subdomain(HostType, SubdomainPattern, PacketHandler) ->
195 415 mongoose_subdomain_core:register_subdomain(HostType, SubdomainPattern,
196 PacketHandler).
197
198 -spec unregister_subdomain(host_type(), subdomain_pattern()) -> ok.
199 unregister_subdomain(HostType, SubdomainPattern) ->
200 415 mongoose_subdomain_core:unregister_subdomain(HostType, SubdomainPattern).
201
202 -spec get_all_subdomains_for_domain(domain()) ->
203 [mongoose_subdomain_core:subdomain_info()].
204 get_all_subdomains_for_domain(Domain) ->
205 29 mongoose_subdomain_core:get_all_subdomains_for_domain(Domain).
Line Hits Source