Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.3] Add maintenance module for kapps_config interactions #6441

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions core/kazoo_apps/src/kapps_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ get_binary(Category, Key, Default, Node) ->
kz_term:api_object().
get_json(Category, Key) ->
V = get(Category, Key),
as_json_value(V, undefined).
as_json_value(V, 'undefined').

-spec as_json_value(any(), kz_term:api_object()) -> kz_term:api_object().
as_json_value(undefined, Default) -> Default;
as_json_value('undefined', Default) -> Default;
as_json_value(V, Default) ->
case kz_json:is_json_object(V) of
'true' -> V;
Expand Down Expand Up @@ -373,18 +373,17 @@ get_ne_binary_or_ne_binaries(Category, Key, Default) ->
-spec get_ne_binary_or_ne_binaries(config_category(), config_key(), Default, config_node()) -> kz_term:ne_binary() | kz_term:ne_binaries() | Default.
get_ne_binary_or_ne_binaries(Category, Key, Default, Node) ->
ValueOrValues = get(Category, Key, Default, Node),
case kz_term:is_empty(ValueOrValues) of
'true' -> Default;
'false' ->
case ValueOrValues of
Value=?NE_BINARY -> Value;
Values when is_list(Values) ->
[kz_term:to_binary(Value)
|| Value <- Values,
kz_term:is_not_empty(Value)
]
end
end.
value_or_values(ValueOrValues, Default).

value_or_values('undefined', Default) -> Default;
value_or_values(<<>>, Default) -> Default;
value_or_values([], Default) -> Default;
value_or_values(<<Value/binary>>, _Default) -> Value;
value_or_values([_|_]=Values, _Default) ->
[kz_term:to_binary(Value)
|| Value <- Values,
kz_term:is_not_empty(Value)
].

-spec get_ne_binary(config_category(), config_key()) -> kz_term:api_ne_binary().
get_ne_binary(Category, Key) ->
Expand Down Expand Up @@ -413,15 +412,7 @@ get_ne_binaries(Category, Key, Default) ->
-spec get_ne_binaries(config_category(), config_key(), Default, config_node()) -> kz_term:ne_binaries() | Default.
get_ne_binaries(Category, Key, Default, Node) ->
Values = get(Category, Key, Default, Node),
case kz_term:is_empty(Values) of
'true' -> Default;
'false' ->
[kz_term:to_binary(Value)
|| Value <- Values,
kz_term:is_not_empty(Value)
]
end.

value_or_values(Values, Default).

%%------------------------------------------------------------------------------
%% @doc Get a configuration key for a given category but only if its configured
Expand Down
71 changes: 71 additions & 0 deletions core/kazoo_apps/src/kapps_config_maintenance.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%%%-----------------------------------------------------------------------------
%%% @copyright (C) 2012-2020, 2600Hz
%%% @doc
%%% @author James Aimonetti
%%% @end
%%%-----------------------------------------------------------------------------
-module(kapps_config_maintenance).

-export([get/2, get_pp/2
,get_category/1, get_category_pp/1
,get_json/2, get_json_pp/2
]).

-spec get(binary(), binary()) -> 'ok'.
get(Category, Key) ->
get_pp(Category, Key, 'false').

-spec get_pp(binary(), binary()) -> 'ok'.
get_pp(Category, Key) ->
get_pp(Category, Key, 'true').

get_pp(Category, Key, PP) ->
pp(kapps_config:get(Category, Key), PP).

-spec get_json(binary(), binary()) -> 'ok'.
get_json(Category, Key) ->
get_json_pp(Category, Key, 'false').

-spec get_json_pp(binary(), binary()) -> 'ok'.
get_json_pp(Category, Key) ->
get_json_pp(Category, Key, 'true').

-spec get_json_pp(binary(), binary(), boolean()) -> 'ok'.
get_json_pp(Category, Key, PP) ->
pp(kapps_config:get_json(Category, Key), PP).

-spec get_category(binary()) -> 'ok'.
get_category(Category) ->
get_category_pp(Category, 'false').

-spec get_category_pp(binary()) -> 'ok'.
get_category_pp(Category) ->
get_category_pp(Category, 'true').

-spec get_category_pp(binary(), boolean()) -> 'ok'.
get_category_pp(Category, PP) ->
pp(kapps_config:get_category(Category), PP).

pp({'ok', Thing}, PP) ->
pp(Thing, PP);
pp({'error', _E}, _PP) ->
io:format("error: ~p~n", [_E]);
pp(Thing, PP) ->
case kz_json:is_json_object(Thing) of
'true' -> pp_json(Thing, PP);
'false' -> pp_thing(Thing, PP)
end.

pp_json(JObj, 'true') ->
io:format("~s~n", [kz_json:encode(JObj, ['pretty'])]);
pp_json(JObj, 'false') ->
io:format("~s~n", [kz_json:encode(JObj)]).

pp_thing(<<Value/binary>>, _PP) ->
io:format("~s~n", [Value]);
pp_thing([], _PP) ->
io:format("[]~n");
pp_thing([<<_/binary>>|_]=Values, 'true') ->
io:format("~s~n", [kz_binary:join(Values)]);
pp_thing(Thing, _PP) ->
io:format("~p~n", [Thing]).
35 changes: 23 additions & 12 deletions core/kazoo_data/src/kazoo_data_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
,trace_pid/1
,stop_trace/1
]).
-export([open_document/2
,open_document_cached/2
-export([open_document/2, open_document/3
,open_document_cached/2, open_document_cached/3
]).

-export([load_doc_from_file/2]).
Expand Down Expand Up @@ -83,20 +83,31 @@ stop_trace(Ref) ->
io:format("trace stopped, see log at ~s~n", [Filename]).


-spec open_document(kz_term:ne_binary(), kz_term:ne_binary()) -> no_return.
-spec open_document(kz_term:ne_binary(), kz_term:ne_binary()) -> 'no_return'.
open_document(Db, Id) ->
print(kz_datamgr:open_doc(Db, Id)).
open_document(Db, Id, 'false').

-spec open_document_cached(kz_term:ne_binary(), kz_term:ne_binary()) -> no_return.
open_document_cached(Db, Id) ->
print(kz_datamgr:open_cache_doc(Db, Id)).
-spec open_document(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary() | boolean()) -> 'no_return'.
open_document(Db, Id, PP) ->
print(kz_datamgr:open_doc(Db, Id), kz_term:is_true(PP)).

print({ok, JSON}) ->
io:format("~s\n", [kz_json:encode(JSON)]),
no_return;
print({error, R}) ->
-spec open_document_cached(kz_term:ne_binary(), kz_term:ne_binary()) -> 'no_return'.
open_document_cached(Db, Id) ->
open_document_cached(Db, Id, 'false').

-spec open_document_cached(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary() | boolean()) -> 'no_return'.
open_document_cached(Db, Id, PP) ->
print(kz_datamgr:open_cache_doc(Db, Id), kz_term:is_true(PP)).

print({'ok', JObj}, 'true') ->
io:format("~s\n", [kz_json:encode(JObj, ['pretty'])]),
'no_return';
print({'ok', JObj}, 'false') ->
io:format("~s\n", [kz_json:encode(JObj)]),
'no_return';
print({'error', R}, _PP) ->
io:format("ERROR: ~p\n", [R]),
no_return.
'no_return'.

-spec load_doc_from_file(kz_term:ne_binary(), kz_term:ne_binary()) ->
{'ok', kz_json:object()} |
Expand Down