Skip to content

Commit

Permalink
Merge branch 'WHISTLE-1016' into v1.53
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed May 8, 2012
2 parents f3bb0b3 + cd5a827 commit c9022b4
Show file tree
Hide file tree
Showing 15 changed files with 1,537 additions and 874 deletions.
61 changes: 61 additions & 0 deletions lib/whistle-1.0.0/src/wh_util.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-module(wh_util).

-export([format_account_id/1, format_account_id/2]).
-export([is_in_account_hierarchy/2, is_in_account_hierarchy/3]).
-export([is_system_admin/1]).
-export([get_account_realm/1, get_account_realm/2]).
-export([is_account_enabled/1]).

Expand Down Expand Up @@ -90,6 +92,65 @@ format_account_id(AccountId, encoded) when is_binary(AccountId) ->
format_account_id(AccountId, raw) ->
AccountId.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% Determine if the given account id/db exists in the hierarchy of
%% the provided account id/db. Optionally consider the account in
%% its own hierarchy.
%% @end
%%--------------------------------------------------------------------
-spec is_in_account_hierarchy/2 :: ('undefined' | ne_binary(), 'undefined' | ne_binary()) -> boolean().
-spec is_in_account_hierarchy/3 :: ('undefined' | ne_binary(), 'undefined' | ne_binary(), boolean()) -> boolean().

is_in_account_hierarchy(CheckFor, InAccount) ->
is_in_account_hierarchy(CheckFor, InAccount, false).

is_in_account_hierarchy(undefined, _, _) ->
false;
is_in_account_hierarchy(_, undefined, _) ->
false;
is_in_account_hierarchy(CheckFor, InAccount, IncludeSelf) ->
CheckId = wh_util:format_account_id(CheckFor, raw),
AccountId = wh_util:format_account_id(InAccount, raw),
AccountDb = wh_util:format_account_id(InAccount, encoded),
case (IncludeSelf andalso AccountId =:= CheckId) orelse crossbar_util:open_doc(AccountDb, AccountId) of
true ->
lager:debug("account ~s is the same as the account to fetch the hierarchy from", [CheckId]),
true;
{ok, JObj} ->
Tree = wh_json:get_value(<<"pvt_tree">>, JObj, []),
case lists:member(CheckId, Tree) of
true ->
lager:debug("account ~s is in the account hierarchy of ~s", [CheckId, AccountId]),
true;
false ->
lager:debug("account ~s was not found in the account hierarchy of ~s", [CheckId, AccountId]),
false
end;
{error, _R} ->
lager:debug("failed to get the ancestory of the account ~s: ~p", [AccountId, _R]),
false
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec is_system_admin/1 :: ('undefined' | ne_binary()) -> boolean().
is_system_admin(undefined) -> false;
is_system_admin(Account) ->
AccountId = wh_util:format_account_id(Account, raw),
AccountDb = wh_util:format_account_id(Account, encoded),
case couch_mgr:open_doc(AccountDb, AccountId) of
{ok, JObj} -> wh_json:is_true(<<"pvt_superduper_admin">>, JObj);
{error, _R} ->
lager:debug("unable to open account definition for ~s: ~p", [Account, _R]),
false
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down
11 changes: 10 additions & 1 deletion lib/whistle_number_manager-1.0.0/include/wh_number_manager.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
-define(WNM_CONFIG_CAT, <<"number_manager">>).

-define(WNM_NUMBER_STATUS, [<<"discovery">>, <<"available">>, <<"reserved">>, <<"released">>
,<<"in_service">>, <<"disconnected">>, <<"cancelled">>]).
,<<"port_in">> ,<<"in_service">>, <<"disconnected">>, <<"port_out">>
]).
-define(WNM_AVALIABLE_STATES, [<<"discovery">>, <<"available">>]).

-define(WNM_DEAFULT_CARRIER_MODULES, [<<"wnm_local">>]).
Expand All @@ -19,6 +20,14 @@

-define(WNM_DEAFULT_TOLLFREE_RE, "^(\\+?1)?(8[1-4,9]\\d{8}|80[1-9]\\d{7}|85[1-4,6-9]\\d{7}|86[1-5,7-9]\\d{7}|87[1-6,8-9]\\d{7}|88[1-7,9]\\d{7}|([1-7,9]\\d{9}))$").

-type transition_return() :: {'ok', wh_json:json_object()} |
{'error', 'invalid_state_transition'} |
{'error', 'unauthorized'} |
{'error', 'carrier_fault'} |
{'error', 'unknown_carrier'} |
{'error', 'no_change'} |
{'error', 'no_change_required'}.

%%% NUMBER STATES
%%% discovery - The number was discovered via a carrier lookup but has not been reserved or purchased.
%%% Numbers in this state should be cleared out on a cleanup period (every 2 hours or so)
Expand Down
75 changes: 28 additions & 47 deletions lib/whistle_number_manager-1.0.0/src/carriers/wnm_bandwidth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
-module(wnm_bandwidth).

-export([find_numbers/2]).
-export([acquire_number/3]).
-export([release_number/2]).
-export([acquire_number/2]).
-export([disconnect_number/2]).

-include("../../include/wh_number_manager.hrl").
-include_lib("whistle_number_manager/include/wh_number_manager.hrl").
-include_lib("xmerl/include/xmerl.hrl").

-define(WNM_BW_CONFIG_CAT, <<(?WNM_CONFIG_CAT)/binary, ".bandwidth">>).
Expand Down Expand Up @@ -81,59 +81,40 @@ find_numbers(Search, Quanity) ->
%% Acquire a given number from the carrier
%% @end
%%--------------------------------------------------------------------
-spec acquire_number/3 :: (ne_binary(), ne_binary(), wh_json:json_object()) -> {ok, ne_binary(), wh_json:json_object()} |
{error, ne_binary()}.
acquire_number(_, <<"available">>, JObj) ->
{ok, <<"in_service">>, JObj};
acquire_number(_, <<"discovery">>, JObj) ->
-spec acquire_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()} |
{error, ne_binary()}.
acquire_number(_, JObj) ->
case whapps_config:get_is_true(?WNM_BW_CONFIG_CAT, <<"enable_provisioning">>, <<"true">>) of
true ->
order_number(JObj);
false ->
{ok, <<"in_service">>, JObj}
end;
acquire_number(_, <<"claim">>, JObj) ->
{ok, <<"in_service">>, JObj};
acquire_number(_, _, _) ->
{error, unavailable}.
false -> {ok, JObj}; %%{error, number_provisioning_disabled};
true ->
Id = wh_json:get_string_value(<<"number_id">>, JObj),
Endpoint = whapps_config:get_binary(?WNM_BW_CONFIG_CAT, <<"endpoint">>, <<"">>),
OrderNamePrefix = whapps_config:get_binary(?WNM_BW_CONFIG_CAT, <<"order_name_prefix">>, <<"Whistle">>),
OrderName = list_to_binary([OrderNamePrefix, "-", wh_util:to_binary(wh_util:current_tstamp())]),
AcquireFor = wh_json:get_ne_value(<<"authorizing_account">>, JObj, <<"no_authorizing_account">>),
Props = [{'orderName', [wh_util:to_list(OrderName)]}
,{'extRefID', [wh_util:to_list(AcquireFor)]}
,{'numberIDs', [{'id', [Id]}]}
,{'subscriber', [wh_util:to_list(AcquireFor)]}
,{'endPoints', [{'host', [wh_util:to_list(Endpoint)]}]}
],
case make_numbers_request('basicNumberOrder', Props) of
{error, _}=E -> E;
{ok, Xml} ->
Response = xmerl_xpath:string("/numberOrderResponse/numberOrder", Xml),
{ok, number_order_response_to_json(Response)}
end
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Release a number from the routing table
%% @end
%%--------------------------------------------------------------------
-spec release_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()}.
release_number(_, JObj) ->
-spec disconnect_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()}.
disconnect_number(_, JObj) ->
{ok, JObj}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Order a number given the discovery json object
%% @end
%%--------------------------------------------------------------------
-spec order_number/1 :: (wh_json:json_object()) -> {ok, ne_binary(), wh_json:json_object()} |
{error, ne_binary()}.
order_number(JObj) ->
Id = wh_json:get_string_value(<<"number_id">>, JObj),
Endpoint = whapps_config:get_binary(?WNM_BW_CONFIG_CAT, <<"endpoint">>, <<"">>),
OrderNamePrefix = whapps_config:get_binary(?WNM_BW_CONFIG_CAT, <<"order_name_prefix">>, <<"Whistle">>),
OrderName = list_to_binary([OrderNamePrefix, "-", wh_util:to_binary(wh_util:current_tstamp())]),
AcquireFor = wh_json:get_ne_value(<<"acquire_for">>, JObj, <<"no_subscriber">>),
Props = [{'orderName', [wh_util:to_list(OrderName)]}
,{'extRefID', [wh_util:to_list(AcquireFor)]}
,{'numberIDs', [{'id', [Id]}]}
,{'subscriber', [wh_util:to_list(AcquireFor)]}
,{'endPoints', [{'host', [wh_util:to_list(Endpoint)]}]}
],
case make_numbers_request('basicNumberOrder', Props) of
{error, _}=E ->
E;
{ok, Xml} ->
Response = xmerl_xpath:string("/numberOrderResponse/numberOrder", Xml),
{ok, <<"in_service">>, number_order_response_to_json(Response)}
end.

%%--------------------------------------------------------------------
%% @private
Expand Down
21 changes: 8 additions & 13 deletions lib/whistle_number_manager-1.0.0/src/carriers/wnm_local.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
-module(wnm_local).

-export([find_numbers/2]).
-export([acquire_number/3]).
-export([release_number/2]).
-export([acquire_number/2]).
-export([disconnect_number/2]).

-include("../../include/wh_number_manager.hrl").
-include_lib("whistle_number_manager/include/wh_number_manager.hrl").

%%--------------------------------------------------------------------
%% @public
Expand Down Expand Up @@ -50,21 +50,16 @@ find_numbers(Number, Quanity) ->
%% Acquire a given number from the carrier
%% @end
%%--------------------------------------------------------------------
-spec acquire_number/3 :: (ne_binary(), ne_binary(), wh_json:json_object()) -> {ok, ne_binary(), wh_json:json_object()}
| {error, ne_binary()}.
acquire_number(_, <<"available">>, JObj) ->
{ok, <<"in_service">>, JObj};
acquire_number(_, <<"claim">>, JObj) ->
{ok, <<"in_service">>, JObj};
acquire_number(_, _, _) ->
{error, unavailable}.
-spec acquire_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()}.
acquire_number(_, JObj) ->
{ok, JObj}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Release a number from the routing table
%% @end
%%--------------------------------------------------------------------
-spec release_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()}.
release_number(_, JObj) ->
-spec disconnect_number/2 :: (ne_binary(), wh_json:json_object()) -> {ok, wh_json:json_object()}.
disconnect_number(_, JObj) ->
{ok, JObj}.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ save(JObj, PriorJObj, Number, <<"reserved">>) ->
EmptyJObj = wh_json:new(),
Cnam = wh_json:get_value(<<"cnam">>, JObj, EmptyJObj),
case Cnam =/= EmptyJObj andalso Cnam =/= wh_json:get_value(<<"cnam">>, PriorJObj) of
false ->
lager:debug("cnam is unchanged or empty on a reserved number"),
{ok, JObj};
false -> {ok, JObj};
true ->
lager:debug("cnam information has been updated"),
Notify = [{<<"Account-ID">>, wh_json:get_value(<<"pvt_reserved_for">>, JObj)}
Expand All @@ -49,9 +47,7 @@ save(JObj, PriorJObj, Number, <<"in_service">>) ->
EmptyJObj = wh_json:new(),
Cnam = wh_json:get_value(<<"cnam">>, JObj, EmptyJObj),
case Cnam =/= EmptyJObj andalso Cnam =/= wh_json:get_value(<<"cnam">>, PriorJObj) of
false ->
lager:debug("cnam is unchanged or empty on a number in service"),
{ok, JObj};
false -> {ok, JObj};
true ->
lager:debug("cnam information has been updated"),
Notify = [{<<"Account-ID">>, wh_json:get_value(<<"pvt_assigned_to">>, JObj)}
Expand All @@ -65,9 +61,7 @@ save(JObj, PriorJObj, Number, <<"in_service">>) ->
wapi_notifications:publish_cnam_request(Notify),
{ok, JObj}
end;
save(JObj, _, _, State) ->
lager:debug("ignoring cnam in number with state ~s", [State]),
{ok, JObj}.
save(JObj, _, _, _) -> {ok, JObj}.

%%--------------------------------------------------------------------
%% @public
Expand Down
75 changes: 52 additions & 23 deletions lib/whistle_number_manager-1.0.0/src/providers/wnm_dash_e911.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,40 @@
%%--------------------------------------------------------------------
-spec save/4 :: (wh_json:json_object(), wh_json:json_object(), ne_binary(), ne_binary())
-> {ok, wh_json:json_object()} | {error, binary()}.
save(JObj, _, _, <<"reserved">>) ->
{ok, JObj};
save(JObj, _, _, <<"discovery">>) ->
{ok, JObj};
save(JObj, _, Number, <<"port_in">>) ->
case wh_json:get_value(<<"dash_e911">>, JObj) of
undefined -> {ok, JObj};
Address ->
lager:debug("porting number '~s' has e911 address, updating dash", [Number]),
update_e911(Number, Address, JObj)
end;
save(JObj, PriorJObj, Number, <<"reserved">>) ->
EmptyJObj = wh_json:new(),
Address = wh_json:get_value(<<"dash_e911">>, JObj, EmptyJObj),
case Address =/= wh_json:get_value(<<"dash_e911">>, PriorJObj, EmptyJObj) of
false -> {ok, JObj};
true when Address =:= EmptyJObj ->
lager:debug("e911 address was changed and is now empty"),
remove_number(Number),
{ok, JObj};
true ->
lager:debug("e911 address for '~s' was changed, updating dash", [Number]),
update_e911(Number, Address, JObj)
end;
save(JObj, PriorJObj, Number, <<"in_service">>) ->
EmptyJObj = wh_json:new(),
Address = wh_json:get_value(<<"dash_e911">>, JObj, EmptyJObj),
case Address =/= wh_json:get_value(<<"dash_e911">>, PriorJObj) of
case Address =/= wh_json:get_value(<<"dash_e911">>, PriorJObj, EmptyJObj) of
false -> {ok, JObj};
true when Address =:= EmptyJObj ->
lager:debug("e911 address was changed and is now empty"),
remove_number(Number),
{ok, JObj};
true ->
lager:debug("e911 address for '~s' was changed, updating dash", [Number]),
Location = json_address_to_xml_location(Address),
CallerName = wh_json:get_ne_value(<<"caller_name">>, Address, <<"Valued Customer">>),
case add_location(Number, Location, CallerName) of
{error, R}=E ->
lager:debug("error provisioning dash e911 address: ~p", [R]),
E;
{provisioned, E911} ->
lager:debug("provisioned dash e911 address"),
{ok, wh_json:set_value(<<"dash_e911">>, E911, JObj)};
{geocoded, E911} ->
lager:debug("added location to dash e911, attempting to provision new location"),
case provision_location(wh_json:get_value(<<"location_id">>, E911)) of
undefined ->
lager:debug("provisioning attempt moved location to status: undefined"),
{ok, wh_json:set_value(<<"dash_e911">>, E911, JObj)};
Status ->
lager:debug("provisioning attempt moved location to status: ~s", [Status]),
{ok, wh_json:set_value(<<"dash_e911">>, wh_json:set_value(<<"status">>, Status, E911), JObj)}
end
end
update_e911(Number, Address, JObj)
end;
save(JObj, _, Number, _) ->
remove_number(Number),
Expand All @@ -90,6 +89,36 @@ delete(JObj, _, Number, _) ->
remove_number(Number),
{ok, JObj}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec update_e911/3 :: (ne_binary(), wh_json:json_object(), wh_json:json_object()) -> {'ok', wh_json:json_object()} |
{'error', term()}.
update_e911(Number, Address, JObj) ->
Location = json_address_to_xml_location(Address),
CallerName = wh_json:get_ne_value(<<"caller_name">>, Address, <<"Valued Customer">>),
case add_location(Number, Location, CallerName) of
{error, R}=E ->
lager:debug("error provisioning dash e911 address: ~p", [R]),
E;
{provisioned, E911} ->
lager:debug("provisioned dash e911 address"),
{ok, wh_json:set_value(<<"dash_e911">>, E911, JObj)};
{geocoded, E911} ->
lager:debug("added location to dash e911, attempting to provision new location"),
case provision_location(wh_json:get_value(<<"location_id">>, E911)) of
undefined ->
lager:debug("provisioning attempt moved location to status: undefined"),
{ok, wh_json:set_value(<<"dash_e911">>, E911, JObj)};
Status ->
lager:debug("provisioning attempt moved location to status: ~s", [Status]),
{ok, wh_json:set_value(<<"dash_e911">>, wh_json:set_value(<<"status">>, Status, E911), JObj)}
end
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down
Loading

0 comments on commit c9022b4

Please sign in to comment.