forked from 2600hz/kazoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PROD-22: Serverless functions (2600hz#5911)
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
1 parent
0ce2a30
commit 171efb6
Showing
49 changed files
with
2,004 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.