aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cdapbroker.app.src2
-rw-r--r--src/resource_handler.erl61
-rw-r--r--src/resource_handler_tests.erl34
3 files changed, 66 insertions, 31 deletions
diff --git a/src/cdapbroker.app.src b/src/cdapbroker.app.src
index 611d873..eb39868 100644
--- a/src/cdapbroker.app.src
+++ b/src/cdapbroker.app.src
@@ -1,6 +1,6 @@
{application, cdapbroker,
[{description, "Interface between Consul and CDAP in DCAE"},
- {vsn, "4.0.6"},
+ {vsn, "4.0.7"},
{registered, []},
{mod, { cdapbroker_app, []}},
{applications,
diff --git a/src/resource_handler.erl b/src/resource_handler.erl
index 798f457..1a0760d 100644
--- a/src/resource_handler.erl
+++ b/src/resource_handler.erl
@@ -7,8 +7,6 @@
-export([get/3, put/3, delete/3, post/3]).
-export([cross_domains/3]).
--export([appname_to_field_vals/2]).
-
%%for keeping state
%%The application record is defined in application.hrl
%%In Mnesia, the first element is the type of record and the second element is the key
@@ -31,6 +29,8 @@
%lazy shorthand to write info audit records. man I miss defines in python. c ftw.
-define(AUDI(Req, Bts, XER, Rcode), audit(info, Req, [{bts, Bts}, {xer,XER}, {rcode, RCode}, {mod, mod()}])).
+%need this for the ?MODULE:FUNCTION to work with meck unit tests
+-export([delete_app_helper/4, appname_to_field_vals/2]).
%%%
%%Helper functions
@@ -332,7 +332,35 @@ handle_put(Req, State, XER, Appname, ReqBody, RequestUrl) ->
end
end.
+handle_post_multidelete_app(Req, State, XER, ReqBody) ->
+ %handler method for the multi delete post ala AWS
+ case
+ try
+ B = maps:get(<<"appnames">>, jiffy:decode(ReqBody, [return_maps])),
+ true = erlang:is_list(B),
+ B
+ catch _:_ ->
+ invalid
+ end
+ of
+ invalid -> {400, "Invalid PUT Body", State};
+ IDs ->
+ case IDs of
+ [] -> {200, "EMPTY PUT BODY", State};
+ _ ->
+ %<<"*">> ->
+ %this block deleted all apps, but decided this backdoor wasn't very RESTy
+ %% {atomic, Apps} = mnesia:transaction(fun() -> mnesia:match_object(application, {application, '_', '_', '_', '_', '_', '_', '_', '_', '_'}, read) end),
+ % AppsToDelete = lists:map(fun(X) -> {application, Appname, _,_,_,_,_,_,_,_} = X, Appname end, Apps),
+ Returns = lists:map(fun(X) -> ?MODULE:delete_app_helper(X, State, XER, Req) end, IDs),
+ RL = lists:map(fun({RC, _, _}) -> RC end, Returns),
+ {200, jiffy:encode(RL), State}
+ end
+ end.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% HTTP API CALLBACKS %%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%
init(_Route, _Req, State) ->
{ok, State}.
terminate(_Reason, _Route, _Req, _State) ->
@@ -340,8 +368,7 @@ terminate(_Reason, _Route, _Req, _State) ->
%%%FOR Cors support
%%%Note! only matches on host. Does not handle ports. See: https://github.com/s1n4/leptus/issues/55
cross_domains(_Route, _Req, State) ->
- {['_'], State}.
-
+ {['_'], State}.
%%%GET Methods
get("/", Req, State) ->
%The broker's "info" endpoint; returns some possibly useful information
@@ -429,33 +456,11 @@ put("/application/:appname/reconfigure", Req, State) ->
{RCode, RBody, RState} = handle_reconfigure_put(Req, State, XER, Appname, ReqBody),
?AUDI(Req, Bts, XER, Rcode),
{RCode, RBody, RState}.
-
%%%POST methods
post("/application/delete", Req, State) ->
%This follows the AWS S3 Multi Key Delete: http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
- %Except I added an additional special value called "*"
{Bts, XER} = init_api_call(Req),
- {RCode, RBody, RState} = case try
- B = maps:get(<<"appnames">>, jiffy:decode(leptus_req:body_raw(Req), [return_maps])),
- true = erlang:is_list(B),
- B
- catch _:_ ->
- invalid
- end
- of
- invalid -> {400, "Invalid PUT Body", State};
- IDs ->
- case IDs of
- [] -> {200, "EMPTY PUT BODY", State};
- _ ->
- %<<"*">> ->
- %this block deleted all apps, but decided this backdoor wasn't very RESTy
- %% {atomic, Apps} = mnesia:transaction(fun() -> mnesia:match_object(application, {application, '_', '_', '_', '_', '_', '_', '_', '_', '_'}, read) end),
- % AppsToDelete = lists:map(fun(X) -> {application, Appname, _,_,_,_,_,_,_,_} = X, Appname end, Apps),
- Returns = lists:map(fun(X) -> delete_app_helper(X, State, XER, Req) end, IDs),
- RL = lists:map(fun({RC, _, _}) -> RC end, Returns),
- {200, jiffy:encode(RL), State}
- end
- end,
+ ReqBody = leptus_req:body_raw(Req),
+ {RCode, RBody, RState} = handle_post_multidelete_app(Req, State, XER, ReqBody),
?AUDI(Req, Bts, XER, Rcode),
{RCode, RBody, RState}.
diff --git a/src/resource_handler_tests.erl b/src/resource_handler_tests.erl
index 75e5302..111666b 100644
--- a/src/resource_handler_tests.erl
+++ b/src/resource_handler_tests.erl
@@ -25,10 +25,11 @@
parse_put_body/1,
parse_reconfiguration_put_body/1,
handle_reconfigure_put/5,
- handle_put/6
+ handle_put/6,
+ handle_post_multidelete_app/4
]).
-parse_put_body_test() ->
+put_test() ->
Valid = {[
{<<"cdap_application_type">>, <<"program-flowlet">>},
{<<"namespace">>, <<"ns">>},
@@ -160,4 +161,33 @@ reconfiguration_put_test() ->
meck:unload(resource_handler).
+delete_test() ->
+ EmptyD = dict:new(),
+
+ %test failure: appnames missing
+ Invalid1 = jiffy:encode({[{<<"ids">>, [<<"hwtest">>]}]}),
+ ?assert(handle_post_multidelete_app(notused, EmptyD, notused, Invalid1) == {400,"Invalid PUT Body", EmptyD}),
+
+ %test invalid: not a list
+ Invalid2 = jiffy:encode({[{<<"appnames">>, <<"hwtest">>}]}),
+ ?assert(handle_post_multidelete_app(notused, EmptyD, notused, Invalid2) == {400,"Invalid PUT Body", EmptyD}),
+
+ %mock out delete_app_helper(X, State, XER, Req)
+ meck:new(resource_handler, [passthrough]),
+ meck:expect(resource_handler,
+ delete_app_helper,
+ fun(Appname, State, _XER, _Req) ->
+ case Appname of
+ <<"noexist">> -> {404, "Tried to delete an application that was not registered", State};
+ <<"exist">> -> {200, "", State}
+ end
+ end),
+ %est empty
+ Empty = jiffy:encode({[{<<"appnames">>, []}]}),
+ ?assert(handle_post_multidelete_app(notused, EmptyD, notused, Empty) == {200, "EMPTY PUT BODY", EmptyD}),
+
+ %test one app that is registered (in the mock...) and one app that is not regustered
+ Valid = jiffy:encode({[{<<"appnames">>, [<<"exist">>, <<"noexist">>]}]}),
+ ?assert(handle_post_multidelete_app(notused, EmptyD, notused, Valid) == {200, <<"[200,404]">>, EmptyD}),
+ meck:unload(resource_handler).