Skip to content

Commit

Permalink
Kazoo 6007 master (2600hz#5406)
Browse files Browse the repository at this point in the history
* KAZOO-6007: preserve null on provisioner payloads but remove from db docs (2600hz#5401)

KAZOO-6007: support options on merge_recursive

KAZOO-6007: more merge options and tests

demonstrate the diff between merge and merge_recursive

log the merge strategy used when a counterexample is found

test setting 'null' on objects

test merging nulls

note: this is a failing test case atm

needs arity-3 if you want options respected

document handling of null

* KAZOO-6007: cleanup

* KAZOO-6007: let kzd_devices cleanse

* KAZOO-6007: update the test

* KAZOO-6007: add the e
  • Loading branch information
k-anderson authored and lazedo committed Jan 10, 2019
1 parent 0027c29 commit d522e6d
Show file tree
Hide file tree
Showing 9 changed files with 657 additions and 235 deletions.
54 changes: 30 additions & 24 deletions applications/crossbar/src/modules_v2/cb_devices_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ validate(Context) ->
validate_devices(Context, ?HTTP_GET) ->
load_device_summary(Context);
validate_devices(Context, ?HTTP_PUT) ->
validate_request('undefined', Context).
validate_device('undefined', Context).

-spec validate(cb_context:context(), path_token()) -> cb_context:context().
validate(Context, PathToken) ->
Expand All @@ -186,7 +186,7 @@ validate_device(Context, ?STATUS_PATH_TOKEN, ?HTTP_GET) ->
validate_device(Context, DeviceId, ?HTTP_GET) ->
load_device(DeviceId, Context);
validate_device(Context, DeviceId, ?HTTP_POST) ->
validate_request(DeviceId, Context);
validate_device(DeviceId, load_device(DeviceId, Context));
validate_device(Context, DeviceId, ?HTTP_PATCH) ->
validate_patch(Context, DeviceId);
validate_device(Context, DeviceId, ?HTTP_PUT) ->
Expand All @@ -195,7 +195,7 @@ validate_device(Context, DeviceId, ?HTTP_DELETE) ->
load_device(DeviceId, Context).

validate_patch(Context, DeviceId) ->
crossbar_doc:patch_and_validate(DeviceId, Context, fun validate_request/2).
crossbar_doc:patch_and_validate(DeviceId, Context, fun validate_device/2).

-spec validate(cb_context:context(), path_token(), path_token()) -> cb_context:context().
validate(Context, DeviceId, ?CHECK_SYNC_PATH_TOKEN) ->
Expand Down Expand Up @@ -268,8 +268,8 @@ load_users_device_summary(Context, UserId) ->
%% @doc
%% @end
%%------------------------------------------------------------------------------
-spec validate_request(kz_term:api_binary(), cb_context:context()) -> cb_context:context().
validate_request(DeviceId, Context) ->
-spec validate_device(kz_term:api_binary(), cb_context:context()) -> cb_context:context().
validate_device(DeviceId, Context) ->
Routines = [fun prepare_mac_address/2
,fun prepare_outbound_flags/2
,fun prepare_device_realm/2
Expand Down Expand Up @@ -489,13 +489,20 @@ prepare_provisioner_fields(_DeviceId, Context) ->
Keys = [<<"combo_keys">>
,<<"feature_keys">>
],
ReqData = cb_context:req_data(Context),
JObj = case cb_context:req_value(Context, <<"provision">>) of
'undefined' -> cb_context:req_data(Context);
'undefined' -> ReqData;
Provision ->
NewData = prune_null_provisioner_fields(Keys, Provision),
kz_json:set_value(<<"provision">>, NewData, cb_context:req_data(Context))
kz_json:set_value(<<"provision">>, NewData, ReqData)
end,
cb_context:set_req_data(Context, JObj).
Setters = [{fun cb_context:set_req_data/2, JObj}
,{fun cb_context:store/3
,'unfiltered_req_data'
,ReqData
}
],
cb_context:setters(Context, Setters).

-spec prune_null_provisioner_fields(kz_json:paths(), kz_json:object()) -> kz_json:object().
prune_null_provisioner_fields([], JObj) -> JObj;
Expand Down Expand Up @@ -648,16 +655,11 @@ get_mac_addresses(DbName) ->
[provisioner_util:cleanse_mac_address(MAC) || MAC <- MACs].

-spec get_current_mac_address(kz_term:api_binary(), cb_context:context()) -> kz_term:api_binary().
get_current_mac_address(DeviceId, Context) ->
Context1 = crossbar_doc:load(DeviceId, Context, ?TYPE_CHECK_OPTION(kzd_devices:type())),
case cb_context:resp_status(Context1) of
'success' ->
JObj = cb_context:doc(Context1),
provisioner_util:cleanse_mac_address(
kzd_devices:mac_address(JObj)
);
_Else -> 'undefined'
end.
get_current_mac_address('undefined', _Context) ->
'undefined';
get_current_mac_address(_DeviceId, Context) ->
JObj = cb_context:fetch(Context, 'db_doc', kz_json:new()),
kzd_devices:mac_address(JObj).

%%------------------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -885,11 +887,17 @@ maybe_update_provision(_DeviceId, Context) ->

-spec update_provision(cb_context:context()) -> 'ok'.
update_provision(Context) ->
_ = provisioner_util:provision_device(cb_context:doc(Context)
,cb_context:fetch(Context, 'db_doc')
JObj = cb_context:doc(Context),
Props = kz_json:to_proplist(
kz_doc:private_fields(JObj)
),
ReqData = cb_context:fetch(Context, 'unfiltered_req_data', JObj),
NewDevice = kz_json:set_values(Props, ReqData),
OldDevice = cb_context:fetch(Context, 'db_doc'),
_ = provisioner_util:provision_device(NewDevice
,OldDevice
,#{'req_verb' => cb_context:req_verb(Context)
,'auth_token' => cb_context:auth_token(Context)
,'new_mac_address' => cb_context:req_value(Context, <<"mac_address">>)
}
),
sync_sip_data(Context, 'false').
Expand Down Expand Up @@ -1000,9 +1008,7 @@ remove_aggregate(DeviceId, Context) ->
%%------------------------------------------------------------------------------
-spec maybe_delete_provision(kz_term:api_binary(), cb_context:context()) -> 'ok'.
maybe_delete_provision(_DeviceId, Context) ->
MacAddress = provisioner_util:cleanse_mac_address(
kzd_devices:mac_address(cb_context:doc(Context))
),
MacAddress = kzd_devices:mac_address(cb_context:doc(Context)),
case kz_term:is_not_empty(MacAddress) of
'false' -> Context;
'true' ->
Expand Down
8 changes: 6 additions & 2 deletions core/kazoo_documents/src/kzd_devices.erl
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,15 @@ mac_address(Doc) ->

-spec mac_address(doc(), Default) -> binary() | Default.
mac_address(Doc, Default) ->
kz_json:get_binary_value([<<"mac_address">>], Doc, Default).
provisioner_util:cleanse_mac_address(
kz_json:get_binary_value([<<"mac_address">>], Doc, Default)
).

-spec set_mac_address(doc(), binary()) -> doc().
set_mac_address(Doc, MacAddress) ->
kz_json:set_value([<<"mac_address">>], MacAddress, Doc).
CleansedMacAddress =
provisioner_util:cleanse_mac_address(MacAddress),
kz_json:set_value([<<"mac_address">>], CleansedMacAddress, Doc).

-spec media(doc()) -> kz_term:api_object().
media(Doc) ->
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_documents/test/kzd_device_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test_auxiliary_functions() ->
,?_assertEqual(<<"[email protected]">>, kzd_devices:presence_id(Device1))
,?_assertEqual(<<"contact">>, kzd_devices:sip_invite_format(Device1))
,?_assertEqual(<<"Test Device 1">>, kzd_devices:name(Device1))
,?_assertEqual(<<"00:15:65:27:C9:8E">>, kzd_devices:mac_address(Device1))
,?_assertEqual(<<"00156527c98e">>, kzd_devices:mac_address(Device1))
,?_assertEqual(<<"fr-fr">>, kzd_devices:language(Device1))
,?_assertEqual(<<"sip_device">>, kzd_devices:device_type(Device1))
,?_assertEqual(<<"user0000000000000000000000000001">>, kzd_devices:owner_id(Device1))
Expand Down Expand Up @@ -243,7 +243,7 @@ test_device_param_setting() ->
,{<<"[email protected]">>, fun kzd_devices:set_presence_id/2, fun kzd_devices:presence_id/1}
,{<<"fax">>, fun kzd_devices:set_sip_invite_format/2, fun kzd_devices:sip_invite_format/1}
,{<<"Rick Sanchez">>, fun kzd_devices:set_name/2, fun kzd_devices:name/1}
,{<<"00:14:65:26:C9:8Z">>, fun kzd_devices:set_mac_address/2, fun kzd_devices:mac_address/1}
,{<<"00156527c98e">>, fun kzd_devices:set_mac_address/2, fun kzd_devices:mac_address/1}
,{<<"us-en">>, fun kzd_devices:set_language/2, fun kzd_devices:language/1}
,{<<"fax_machine">>, fun kzd_devices:set_device_type/2, fun kzd_devices:device_type/1}
,{<<"user0000000000000000000000000002">>, fun kzd_devices:set_owner_id/2, fun kzd_devices:owner_id/1}
Expand Down
Loading

0 comments on commit d522e6d

Please sign in to comment.