Permalink
Browse files

WHISTLE-935: tweak logs and fix for inbound only

  • Loading branch information...
1 parent 3114691 commit 644b6e18d544172889e3b0eaf07f061ad6bf64ef @k-anderson k-anderson committed Oct 9, 2012
@@ -25,17 +25,16 @@
-spec maybe_authorize_channel/2 :: (wh_proplist(), atom()) -> boolean().
maybe_authorize_channel(Props, Node) ->
-
CallId = props:get_value(<<"Unique-ID">>, Props),
- is_authz_enabled(Props, CallId, Node).
+ is_authz_enabled(Props, CallId, Node).
-spec is_authz_enabled/3 :: (wh_proplist(), ne_binary(), atom()) -> boolean().
is_authz_enabled(Props, CallId, Node) ->
-
case wh_util:is_true(ecallmgr_config:get(<<"authz_enabled">>, false)) of
true ->
is_global_resource(Props, CallId, Node);
- false -> lager:debug("config ecallmgr.authz is disabled", []),
+ false ->
+ lager:debug("config ecallmgr.authz is disabled", []),
allow_call(Props, CallId, Node)
end.
@@ -124,7 +123,8 @@ authorize_account(Props, CallId, Node) ->
-spec authorize_reseller/3 :: (wh_proplist(), ne_binary(), atom()) -> boolean().
authorize_reseller(Props, CallId, Node) ->
ResellerId = props:get_value(?GET_CCV(<<"Reseller-ID">>), Props),
- case authorize(ResellerId, Props) of
+ AccountId = props:get_value(?GET_CCV(<<"Account-ID">>), Props),
+ case AccountId =:= ResellerId orelse authorize(ResellerId, Props) of
{error, account_limited} ->
lager:debug("reseller has no remaining resources", []),
maybe_deny_call(Props, CallId, Node);
@@ -286,6 +286,7 @@ authz_req(AccountId, Props) ->
,{<<"Request">>, ecallmgr_util:get_sip_request(Props)}
,{<<"Call-ID">>, props:get_value(<<"Unique-ID">>, Props)}
,{<<"Account-ID">>, AccountId}
+ ,{<<"Call-Direction">>, props:get_value(<<"Call-Direction">>, Props)}
,{<<"Custom-Channel-Vars">>, wh_json:from_list(ecallmgr_util:custom_channel_vars(Props))}
,{<<"Usage">>, ecallmgr_fs_nodes:account_summary(AccountId)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
@@ -36,6 +36,7 @@
-define(AUTHZ_REQ_HEADERS, [<<"To">>, <<"From">>, <<"Call-ID">>
,<<"Caller-ID-Name">>, <<"Caller-ID-Number">>
,<<"Account-ID">>, <<"Request">>, <<"Usage">>
+ ,<<"Call-Direction">>
]).
-define(OPTIONAL_AUTHZ_REQ_HEADERS, [<<"Custom-Channel-Vars">>, <<"Switch-Hostname">>]).
-define(AUTHZ_REQ_VALUES, [{<<"Event-Category">>, ?EVENT_CATEGORY}
@@ -16,6 +16,8 @@ handle_req(JObj, Props) ->
true = wapi_authz:req_v(JObj),
wh_util:put_callid(JObj),
+ io:format("~p~n", [JObj]),
+
AccountId = wh_json:get_value(<<"Account-ID">>, JObj),
Limits = j5_util:get_limits(AccountId),
@@ -125,10 +127,24 @@ resource_consumption_at_limit(#limits{resource_consuming_calls=Resources}, JObj)
-spec trunks_at_limit/2 :: (#limits{}, wh_json:json_object()) -> boolean().
trunks_at_limit(Limits, JObj) ->
- InboundResources = wh_json:get_integer_value([<<"Usage">>, <<"Inbound-Flat-Rate">>], JObj, 0),
- RemainingInbound = consume_inbound_limits(Limits, InboundResources),
- OutboundResources = wh_json:get_integer_value([<<"Usage">>, <<"Outbound-Flat-Rate">>], JObj, 0),
- consume_twoway_limits(Limits, RemainingInbound + OutboundResources).
+ RemainingInbound = consume_inbound_limits(Limits, get_inbound_resources(JObj)),
+ consume_twoway_limits(Limits, RemainingInbound + get_outbound_resources(JObj)).
+
+-spec get_inbound_resources/1 :: (wh_json:json_object()) -> integer().
+get_inbound_resources(JObj) ->
+ CurrentUsage = wh_json:get_integer_value([<<"Usage">>, <<"Inbound-Flat-Rate">>], JObj, 0),
+ case wh_json:get_value(<<"Call-Direction">>, JObj) of
+ <<"inbound">> -> CurrentUsage + 1;
+ _Else -> CurrentUsage
+ end.
+
+-spec get_outbound_resources/1 :: (wh_json:json_object()) -> integer().
+get_outbound_resources(JObj) ->
+ CurrentUsage = wh_json:get_integer_value([<<"Usage">>, <<"Outbound-Flat-Rate">>], JObj, 0),
+ case wh_json:get_value(<<"Call-Direction">>, JObj) of
+ <<"outbound">> -> CurrentUsage + 1;
+ _Else -> CurrentUsage
+ end.
-spec credit_is_available/2 :: (#limits{}, wh_json:json_object()) -> boolean().
credit_is_available(Limits, JObj) ->
@@ -172,37 +188,40 @@ postpay_is_available(#limits{allow_postpay=true, max_postpay_amount=MaxPostpay
consume_inbound_limits(#limits{inbound_trunks=-1}, _) ->
lager:debug("account has unlimited inbound trunks", []),
0;
-consume_inbound_limits(#limits{inbound_trunks=0}, Resources) ->
- Resources;
consume_inbound_limits(_, 0) ->
- lager:debug("not using any inbound only trunks yet", []),
+ lager:debug("account is not consuming any inbound resources", []),
0;
+consume_inbound_limits(#limits{inbound_trunks=0}, Resources) ->
+ lager:debug("account has no inbound only trunks", []),
+ Resources;
consume_inbound_limits(#limits{inbound_trunks=Trunks}, Resources) ->
- case Trunks - Resources of
- Count when Count >= 0 ->
- lager:debug("already using ~p of ~p inbound only trunks", [Resources, Trunks]),
- 0;
- Count ->
- RemainingInbound = abs(Count),
- lager:debug("already using all ~p inbound only trunks, with ~p unaccounted for inbound channels", [Trunks, RemainingInbound]),
- RemainingInbound
+ lager:debug("account has ~p inbound only trunks and is using ~p inbound resources", [Trunks, Resources]),
+ case Resources - Trunks of
+ Count when Count > 0 ->
+ lager:debug("remaining ~p channels unaccounted for by inbound only trunks", [Count]),
+ Count;
+ _Else ->
+ lager:debug("all inbound channels are accounted for by inbound only trunks", []),
+ 0
end.
-spec consume_twoway_limits/2 :: (#limits{}, wh_json:json_object()) -> boolean().
consume_twoway_limits(#limits{twoway_trunks=-1}, _) ->
lager:debug("account has unlimited twoway trunks", []),
false;
-consume_twoway_limits(#limits{twoway_trunks=0}, _) -> true;
consume_twoway_limits(_, 0) ->
- lager:debug("not using any twoway trunks yet", []),
+ lager:debug("account is not consuming any two-way resources", []),
false;
+consume_twoway_limits(#limits{twoway_trunks=0}, _) ->
+ lager:debug("account has no two-way trunks", []),
+ true;
consume_twoway_limits(#limits{twoway_trunks=Trunks}, Resources) ->
- case Resources >= Trunks of
- true ->
- lager:debug("already using all ~p twoway trunks", [Trunks]),
+ case Resources - Trunks of
+ Count when Count > 0 ->
+ lager:debug("remaining ~p two-way channels are unaccounted for by trunks", [Count]),
true;
- false ->
- lager:debug("already using ~p of ~p twoway trunks", [Resources, Trunks]),
+ _Else ->
+ lager:debug("all remaining channels are accounted for by two-way trunks", []),
false
end.

0 comments on commit 644b6e1

Please sign in to comment.