Skip to content

Commit

Permalink
PROD-22: Serverless functions (2600hz#5911)
Browse files Browse the repository at this point in the history
Allow developers to upload Javascript functions to KAZOO to be run on
the local infrastructure instead of incurring the cost of an HTTP
request to the developer's infrastructure via Pivot.

run the functions via the callflow action

includes adding 'aggregate' databases as storage plan options, and a
filter for which databases to consider.

add examples of user-defined Javascript functions

Updates couchbeam to include the Show API functionality

 start to address compaints

fix module names and tested

remove kapi functions schemas

remove more functions-related stuff

remove functions
  • Loading branch information
zzzming authored and jamesaimonetti committed Sep 12, 2019
1 parent 0ce2a30 commit 171efb6
Show file tree
Hide file tree
Showing 49 changed files with 2,004 additions and 128 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ core/kazoo_proper/priv/mp3.mp3
/doc/engineering/.org/proper.pdf
/doc/engineering/.org/proper.tex
/todo.org
*.tar
19 changes: 19 additions & 0 deletions applications/callflow/doc/function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Function

### About Function

Runs the function in the local infrastructure to create a new callflow to branch to.

#### Schema

Validator for the function callflow data object



Key | Description | Type | Default | Required | Support Level
--- | ----------- | ---- | ------- | -------- | -------------
`id` | The Function's doc ID | `string()` | | `false` |
`skip_module` | When set to true this callflow action is skipped, advancing to the wildcard branch (if any) | `boolean()` | | `false` |



17 changes: 17 additions & 0 deletions applications/callflow/doc/ref/function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Function

### About Function

#### Schema

Validator for the function callflow data object



Key | Description | Type | Default | Required | Support Level
--- | ----------- | ---- | ------- | -------- | -------------
`id` | The Function's doc ID | `string()` | | `false` |
`skip_module` | When set to true this callflow action is skipped, advancing to the wildcard branch (if any) | `boolean()` | | `false` |



3 changes: 2 additions & 1 deletion applications/callflow/src/callflow.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
kazoo_documents,kazoo_edr,kazoo_endpoint,
kazoo_globals,kazoo_media,kazoo_number_manager,
kazoo_schemas,kazoo_stdlib,kazoo_token_buckets,
kazoo_voicemail,kazoo_web,kernel,lager,stdlib]},
kazoo_translator,kazoo_voicemail,kazoo_web,
kernel,lager,stdlib]},
{description,"Callflow - traversing through the tree..."},
{env,[{is_kazoo_app,true}]},
{mod,{callflow_app,[]}},
Expand Down
2 changes: 1 addition & 1 deletion applications/callflow/src/callflow_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ start(_Type, _Args) ->

-spec prep_stop(any()) -> 'ok'.
prep_stop(_State) ->
kz_nodes:unbind_for_pool_state('kz_amqp_sup', whereis('callflow_sup')),
_ = kz_nodes:unbind_for_pool_state('kz_amqp_sup', whereis('callflow_sup')),
'ok'.

%%------------------------------------------------------------------------------
Expand Down
86 changes: 86 additions & 0 deletions applications/callflow/src/module/cf_function.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
%%%-----------------------------------------------------------------------------
%%% @copyright (C) 2012-2019, 2600Hz
%%% @doc Accept customized `dialplan'.
%%%
%%% <h4>Data options:</h4>
%%% <dl>
%%% <dt>`function_ref'</dt>
%%% <dd> a couchDB document Id reference to retrieve Javascript function.</dd>
%%%
%%% <dt>`language_pack'</dt>
%%% <dd> the language and version are required for the function runtime.</dd>
%%%
%%% <dt>`skip_module'</dt>
%%% <dd>`boolean()'.</dd>
%%%
%%% <dt>`debug'</dt>
%%% <dd>`boolean()'</dd>
%%% </dl>
%%%
%%% @author Ming Luo
%%% This Source Code Form is subject to the terms of the Mozilla Public
%%% License, v. 2.0. If a copy of the MPL was not distributed with this
%%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module(cf_function).

-behaviour(gen_cf_action).

-include("callflow.hrl").

-export([handle/2
,run_function/2
]).

%%------------------------------------------------------------------------------
%% @doc Entry point for this module, attempts to call an endpoint as defined
%% in the Data payload. Returns continue if fails to connect or
%% stop when successful.
%%
%% Expected data payload:
%% id: string(), the function ID for the account
%% @end
%%------------------------------------------------------------------------------
-spec handle(kz_json:object(), kapps_call:call()) -> 'ok'.
handle(Data, Call) ->
%% add language_pack support for other language and versions.
FunctionId = kz_json:get_ne_binary_value(<<"id">>, Data),
case run_function(FunctionId, Call) of
{'ok', Flow} ->
maybe_branch(Flow, Call);
{'error', _E} ->
lager:info("error for ~s: ~p", [FunctionId, _E]),
cf_exe:continue(Call)
end.

-spec run_function(kz_term:ne_binary(), kapps_call:call()) -> {'ok', kz_json:object()} |
kz_datamgr:data_error().
run_function(FunctionId, Call) ->
BaseParams = kzt_kazoo:req_params(Call),
UserParams = kzt_translator:get_user_vars(Call),
Params = kz_json:set_values(BaseParams, UserParams),

QueryString = kz_http_util:json_to_querystring(Params),
kz_datamgr:show(?KZ_FUNCTIONS_DB, <<"functions/wh">>, FunctionId, options(QueryString)).

-spec options(iodata()) -> [{'query_string', binary()}].
options([]) -> [];
options(<<>>) -> [];
options(QueryString) ->
[{'query_string', kz_term:to_binary(QueryString)}].

-spec maybe_branch(kz_json:object(), kapps_call:call()) -> 'ok'.
maybe_branch(FlowJObj, Call) ->
case kzd_callflows:validate_flow(
kzd_callflows:set_flow(kzd_callflows:new(), FlowJObj)
)
of
{'error', Errors} ->
lager:info("error validating flow: ~p", [Errors]),
cf_exe:continue(Call);
{'ok', ValidCallflow} ->
lager:info("branching callflow to ~p", [ValidCallflow]),
cf_exe:branch(kzd_callflows:flow(ValidCallflow), Call)
end.
Loading

0 comments on commit 171efb6

Please sign in to comment.