Skip to content

Commit

Permalink
cb_channels: get system-wide channels list (2600hz#2462)
Browse files Browse the repository at this point in the history
* cb_channels: get system-wide channels list
  • Loading branch information
onnet authored and lazedo committed Aug 19, 2016
1 parent e2695fa commit 07c7dac
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
16 changes: 15 additions & 1 deletion applications/crossbar/doc/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@

The Channels API allows queries to find active channels for an account, a user, or a device. Given a call-id for a channel, a limited set of commands are allowed to be executed against that channel (such as hangup, transfer, or play media).

#### Fetch active channels
#### Fetch active channels system wide.

For superduper admin only.
Be sure to set system_config->crossbar.channels->system_wide_channels_list flag to 'true'.

> GET /v2/channels
```shell
curl -v -X GET \
-H "Content-Type: application/json" \
-H "X-Auth-Token: {AUTH_TOKEN}" \
http://{SERVER}:8000/v2/channels
```

#### Fetch active channels for an account

> GET /v2/accounts/{ACCOUNT_ID}/channels
Expand Down
13 changes: 13 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7837,6 +7837,19 @@
"required": true,
"type": "object"
},
"system_config.crossbar.channels": {
"description": "Schema for crossbar.channels system_config",
"properties": {
"system_wide_channels_list": {
"default": false,
"description": "crossbar.channels system wide channels list",
"name": "system_wide_channels_list",
"type": "boolean"
}
},
"required": true,
"type": "object"
},
"system_config.crossbar.contact_list": {
"description": "Schema for crossbar.contact_list system_config",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-03/schema#",
"_id": "system_config.crossbar.channels",
"description": "Schema for crossbar.channels system_config",
"properties": {
"system_wide_channels_list": {
"default": false,
"description": "crossbar.channels system wide channels list",
"name": "system_wide_channels_list",
"type": "boolean"
}
},
"required": true,
"type": "object"
}
31 changes: 24 additions & 7 deletions applications/crossbar/src/modules/cb_channels.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

-include("crossbar.hrl").

-define(MOD_CONFIG_CAT, <<(?CONFIG_CAT)/binary, ".channels">>).

-type endpoints_return() :: {kz_json:objects(), cb_context:context()}.

%%%===================================================================
Expand Down Expand Up @@ -229,6 +231,9 @@ summary(Context) ->
[{<<"channels">>, []}, {<<"accounts">>, [_AccountId]} | _] ->
lager:debug("getting account summary"),
account_summary(Context);
[{<<"channels">>,[]}] ->
lager:debug("getting system-wide summary"),
account_summary(Context);
_Nouns ->
lager:debug("unexpected nouns: ~p", [_Nouns]),
crossbar_util:response_faulty_request(Context)
Expand Down Expand Up @@ -318,13 +323,14 @@ get_channels(Context, Devices, PublisherFun) ->
=/= 'undefined'
],

Req = [{<<"Realm">>, Realm}
,{<<"Usernames">>, lists:usort(Usernames)} % unique list of usernames
,{<<"Account-ID">>, cb_context:account_id(Context)}
,{<<"Active-Only">>, 'false'}
,{<<"Msg-ID">>, cb_context:req_id(Context)}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
],
Req = props:filter_undefined(
[{<<"Realm">>, Realm}
,{<<"Usernames">>, lists:usort(Usernames)} % unique list of usernames
,{<<"Account-ID">>, get_account_id(Context)}
,{<<"Active-Only">>, 'false'}
,{<<"Msg-ID">>, cb_context:req_id(Context)}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]),

case kz_amqp_worker:call_collect(Req
,PublisherFun
Expand Down Expand Up @@ -508,3 +514,14 @@ maybe_intercept(Context, CallId, TargetType, TargetId) ->
lager:debug("attempting to move ~s to ~s(~s)", [CallId, TargetId, TargetType]),
kz_amqp_worker:cast(API, fun kapi_metaflow:publish_req/1),
crossbar_util:response_202(<<"intercept initiated">>, Context).

-spec get_account_id(cb_context:context()) -> ne_binary().
get_account_id(Context) ->
case cb_context:account_id(Context) of
'undefined' ->
case kapps_config:get_is_true(?MOD_CONFIG_CAT, <<"system_wide_channels_list">>, 'false') of
'true' -> <<"all">>;
'false' -> 'undefined'
end;
AccountId -> AccountId
end.
8 changes: 8 additions & 0 deletions applications/ecallmgr/src/ecallmgr_fs_channels.erl
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,14 @@ find_by_user_realm(Username, Realm) ->
-spec find_account_channels(ne_binary()) ->
{'ok', kz_json:objects()} |
{'error', 'not_found'}.
find_account_channels(<<"all">>) ->
case ets:match_object(?CHANNELS_TBL, #channel{_='_'}) of
[] -> {'error', 'not_found'};
Channels ->
{'ok', [ecallmgr_fs_channel:to_json(Channel)
|| Channel <- Channels
]}
end;
find_account_channels(AccountId) ->
case ets:match_object(?CHANNELS_TBL, #channel{account_id=AccountId, _='_'}) of
[] -> {'error', 'not_found'};
Expand Down

0 comments on commit 07c7dac

Please sign in to comment.