Permalink
Browse files

merge.

  • Loading branch information...
2 parents 41e8694 + 18195e6 commit ee7179056c477e170c085b9b6b3659037daa7cc7 @Licenser Licenser committed Mar 29, 2013
@@ -1,7 +1,7 @@
{application, wiggle,
[
{description, ""},
- {vsn, "0.4.5"},
+ {vsn, "0.4.6"},
{registered, []},
{applications, [
kernel,
@@ -19,6 +19,8 @@ start(_StartType, _StartArgs) ->
{[<<"api">>, '_', <<"groups">>, '...'], wiggle_group_handler, []},
{[<<"api">>, '_', <<"cloud">>, '...'], wiggle_cloud_handler, []},
{[<<"api">>, '_', <<"hypervisors">>, '...'], wiggle_hypervisor_handler, []},
+ {[<<"api">>, '_', <<"dtrace">>, '_', <<"stream">>], wiggle_dtrace_stream, []},
+ {[<<"api">>, '_', <<"dtrace">>, '...'], wiggle_dtrace_handler, []},
{[<<"api">>, '_', <<"vms">>, '_', <<"console">>], wiggle_console_handler, []},
{[<<"api">>, '_', <<"vms">>, '_', <<"vnc">>], wiggle_vnc_handler, []},
{[<<"api">>, '_', <<"vms">>, '...'], wiggle_vm_handler, []},
@@ -27,8 +29,7 @@ start(_StartType, _StartArgs) ->
{[<<"api">>, '_', <<"packages">>, '...'], wiggle_package_handler, []}]}],
{ok, _} = cowboy:start_listener(http, Acceptors,
cowboy_tcp_transport, [{port, Port}],
- cowboy_http_protocol, [{dispatch, Dispatch}]
- ),
+ cowboy_http_protocol, [{dispatch, Dispatch}]),
wiggle_sup:start_link().
@@ -18,10 +18,14 @@
is_authorized/2]).
-export([to_json/2,
- from_json/2]).
+ from_json/2,
+ to_msgpack/2,
+ from_msgpack/2]).
-ignore_xref([to_json/2,
from_json/2,
+ from_msgpack/2,
+ to_msgpack/2,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
@@ -66,7 +70,8 @@ options(Req, State) ->
content_types_provided(Req, State) ->
{[
- {<<"application/json">>, to_json}
+ {<<"application/json">>, to_json},
+ {<<"application/x-msgpack">>, to_msgpack}
], Req, State}.
content_types_accepted(Req, State) ->
@@ -122,6 +127,10 @@ to_json(Req, State) ->
{Reply, Req1, State1} = handle_request(Req, State),
{jsx:encode(Reply), Req1, State1}.
+to_msgpack(Req, State) ->
+ {Reply, Req1, State1} = handle_request(Req, State),
+ {msgpack:pack(Reply, [jsx]), Req1, State1}.
+
handle_request(Req, State = #state{path = [<<"connection">>]}) ->
Res = jsxd:thread([{set, <<"sniffle">>, length(libsniffle:servers())},
{set, <<"snarl">>, length(libsnarl:servers())},
@@ -169,6 +178,9 @@ handle_request(Req, State = #state{path = []}) ->
from_json(Req, State) ->
{false, Req, State}.
+from_msgpack(Req, State) ->
+ {false, Req, State}.
+
%%--------------------------------------------------------------------
%% DEETE
%%--------------------------------------------------------------------
@@ -13,21 +13,28 @@
delete_resource/2,
forbidden/2,
options/2,
+ post_is_create/2,
+ create_path/2,
service_available/2,
is_authorized/2]).
-export([to_json/2,
- from_json/2]).
-
+ from_json/2,
+ to_msgpack/2,
+ from_msgpack/2]).
-ignore_xref([to_json/2,
from_json/2,
+ from_msgpack/2,
+ to_msgpack/2,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
delete_resource/2,
forbidden/2,
init/3,
+ post_is_create/2,
+ create_path/2,
is_authorized/2,
options/2,
service_available/2,
@@ -60,9 +67,13 @@ options(Req, State) ->
['HEAD', 'OPTIONS' | Methods]), ", "), Req),
{ok, Req1, State}.
+post_is_create(Req, State) ->
+ {true, Req, State}.
+
content_types_provided(Req, State) ->
{[
- {<<"application/json">>, to_json}
+ {<<"application/json">>, to_json},
+ {<<"application/x-msgpack">>, to_msgpack}
], Req, State}.
content_types_accepted(Req, State) ->
@@ -72,10 +83,10 @@ allowed_methods(Req, State) ->
{['HEAD', 'OPTIONS' | allowed_methods(State#state.version, State#state.token, State#state.path)], Req, State}.
allowed_methods(_Version, _Token, []) ->
- ['GET'];
+ ['GET', 'POST'];
allowed_methods(_Version, _Token, [_Dataset]) ->
- ['GET', 'DELETE'];
+ ['GET', 'DELETE', 'PUT'];
allowed_methods(_Version, _Token, [_Dataset, <<"metadata">>|_]) ->
['PUT', 'DELETE'].
@@ -106,12 +117,19 @@ forbidden(Req, State = #state{method = 'OPTIONS'}) ->
forbidden(Req, State = #state{token = undefined}) ->
{true, Req, State};
+forbidden(Req, State = #state{method = 'POST', path = []}) ->
+ {allowed(State#state.token, [<<"cloud">>, <<"datasets">>, <<"create">>]), Req, State};
+
forbidden(Req, State = #state{path = []}) ->
{allowed(State#state.token, [<<"cloud">>, <<"datasets">>, <<"list">>]), Req, State};
+
forbidden(Req, State = #state{method = 'GET', path = [Dataset]}) ->
{allowed(State#state.token, [<<"datasets">>, Dataset, <<"get">>]), Req, State};
+forbidden(Req, State = #state{method = 'PUT', path = [Dataset]}) ->
+ {allowed(State#state.token, [<<"datasets">>, Dataset, <<"edit">>]), Req, State};
+
forbidden(Req, State = #state{method = 'DELETE', path = [Dataset]}) ->
{allowed(State#state.token, [<<"datasets">>, Dataset, <<"delete">>]), Req, State};
@@ -132,6 +150,10 @@ to_json(Req, State) ->
{Reply, Req1, State1} = handle_request(Req, State),
{jsx:encode(Reply), Req1, State1}.
+to_msgpack(Req, State) ->
+ {Reply, Req1, State1} = handle_request(Req, State),
+ {msgpack:pack(Reply, [jsx]), Req1, State1}.
+
handle_request(Req, State = #state{token = Token, path = []}) ->
{ok, Permissions} = libsnarl:user_cache({token, Token}),
{ok, Res} = libsniffle:dataset_list([{must, 'allowed', [<<"datasets">>, {<<"res">>, <<"dataset">>}, <<"get">>], Permissions}]),
@@ -144,6 +166,12 @@ handle_request(Req, State = #state{path = [_Dataset], obj = Obj}) ->
%% PUT
%%--------------------------------------------------------------------
+create_path(Req, State = #state{path = [], version = Version}) ->
+ {ok, Decoded, Req1} = wiggle_handler:decode(Req),
+ {ok, URL} = jsxd:get(<<"url">>, Decoded),
+ {ok, UUID} = libsniffle:dataset_import(URL),
+ {<<"/api/", Version/binary, "/datasets/", UUID/binary>>, Req1, State#state{body = Decoded}}.
+
from_json(Req, State) ->
{ok, Body, Req1} = cowboy_http_req:body(Req),
{Reply, Req2, State1} = case Body of
@@ -155,10 +183,28 @@ from_json(Req, State) ->
end,
{Reply, Req2, State1}.
+from_msgpack(Req, State) ->
+ {ok, Body, Req1} = cowboy_http_req:body(Req),
+ {Reply, Req2, State1} = case Body of
+ <<>> ->
+ handle_write(Req1, State, []);
+ _ ->
+ Decoded = msgpack:unpack(Body, [jsx]),
+ handle_write(Req1, State, Decoded)
+ end,
+ {Reply, Req2, State1}.
+
handle_write(Req, State = #state{path = [Dataset, <<"metadata">> | Path]}, [{K, V}]) ->
libsniffle:dataset_set(Dataset, [<<"metadata">> | Path] ++ [K], jsxd:from_list(V)),
{true, Req, State};
+handle_write(Req, State = #state{path = [Dataset]}, [{K, V}]) ->
+ libsniffle:dataset_set(Dataset, [K], jsxd:from_list(V)),
+ {true, Req, State};
+
+handle_write(Req, State = #state{path = []}, _Body) ->
+ {true, Req, State};
+
handle_write(Req, State, _Body) ->
{false, Req, State}.
Oops, something went wrong.

0 comments on commit ee71790

Please sign in to comment.