Permalink
Browse files

Bump to 0.4.2

  • Loading branch information...
2 parents d9454b2 + 69c349b commit 64e97bc0bdbde5ca09922f08b40d1c95f7294606 @Licenser Licenser committed Jan 24, 2013
@@ -1,7 +1,7 @@
{application, wiggle,
[
{description, ""},
- {vsn, "0.4.1"},
+ {vsn, "0.4.2"},
{registered, []},
{applications, [
kernel,
@@ -15,6 +15,7 @@ start(_StartType, _StartArgs) ->
{ok, Acceptors} = application:get_env(wiggle, acceptors),
Dispatch = [{'_', [{[<<"api">>, '_', <<"users">>, '...'], wiggle_user_handler, []},
+ {[<<"api">>, '_', <<"sessions">>, '...'], wiggle_session_handler, []},
{[<<"api">>, '_', <<"groups">>, '...'], wiggle_group_handler, []},
{[<<"api">>, '_', <<"cloud">>, '...'], wiggle_cloud_handler, []},
{[<<"api">>, '_', <<"hypervisors">>, '...'], wiggle_hypervisor_handler, []},
@@ -14,6 +14,7 @@
resource_exists/2,
forbidden/2,
options/2,
+ service_available/2,
is_authorized/2]).
-export([to_json/2,
@@ -27,18 +28,33 @@
forbidden/2,
init/3,
is_authorized/2,
+ service_available/2,
options/2,
resource_exists/2,
rest_init/2]).
--record(state, {path, method, version, token, content, reply}).
+-record(state, {path, method, version, token, content, reply, obj, body}).
init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_http_rest}.
rest_init(Req, _) ->
wiggle_handler:initial_state(Req, <<"cloud">>).
+
+service_available(Req, State = #state{path = [<<"connection">>]}) ->
+ {true, Req, State};
+
+service_available(Req, State) ->
+ case {libsniffle:servers(), libsnarl:servers()} of
+ {[], _} ->
+ {false, Req, State};
+ {_, []} ->
+ {false, Req, State};
+ _ ->
+ {true, Req, State}
+ end.
+
options(Req, State) ->
Methods = allowed_methods(Req, State, State#state.path),
{ok, Req1} = cowboy_http_req:set_resp_header(
@@ -59,15 +75,24 @@ content_types_accepted(Req, State) ->
allowed_methods(Req, State) ->
{['HEAD', 'OPTIONS' | allowed_methods(State#state.version, State#state.token, State#state.path)], Req, State}.
+allowed_methods(_Version, _Token, [<<"connection">>]) ->
+ ['GET'];
+
allowed_methods(_Version, _Token, []) ->
['GET'].
resource_exists(Req, State = #state{path = []}) ->
+ {true, Req, State};
+
+resource_exists(Req, State = #state{path = [<<"connection">>]}) ->
{true, Req, State}.
is_authorized(Req, State = #state{method = 'OPTIONS'}) ->
{true, Req, State};
+is_authorized(Req, State = #state{path = [<<"connection">>]}) ->
+ {true, Req, State};
+
is_authorized(Req, State = #state{token = undefined}) ->
{{false, <<"X-Snarl-Token">>}, Req, State};
@@ -77,11 +102,14 @@ is_authorized(Req, State) ->
forbidden(Req, State = #state{method = 'OPTIONS'}) ->
{false, Req, State};
+forbidden(Req, State = #state{path = [<<"connection">>]}) ->
+ {false, Req, State};
+
forbidden(Req, State = #state{token = undefined}) ->
{true, Req, State};
forbidden(Req, State = #state{path = []}) ->
- {allowed(State#state.token, [<<"cloud">>]), Req, State};
+ {allowed(State#state.token, [<<"cloud">>, <<"cloud">>, <<"status">>]), Req, State};
forbidden(Req, State) ->
{true, Req, State}.
@@ -94,6 +122,13 @@ to_json(Req, State) ->
{Reply, Req1, State1} = handle_request(Req, State),
{jsx:encode(Reply), Req1, State1}.
+handle_request(Req, State = #state{path = [<<"connection">>]}) ->
+ Res = jsxd:thread([{set, <<"sniffle">>, length(libsniffle:servers())},
+ {set, <<"snarl">>, length(libsnarl:servers())},
+ {set, <<"howl">>, length(libhowl:servers())}],
+ []),
+ {Res, Req, State};
+
handle_request(Req, State = #state{path = []}) ->
case libsniffle:cloud_status() of
{ok, {Metrics, Warnings}} ->
@@ -115,8 +150,12 @@ handle_request(Req, State = #state{path = []}) ->
_ ->
[{howl, <<"not connected">>} | Vers1]
end,
+ {ok, Users} = libsnarl:user_list(),
+ {ok, Vms} = libsniffle:vm_list(),
{[{versions, [{wiggle, ?VERSION} | Vers2]},
- {metrics, Metrics},
+ {metrics, [{<<"users">>, length(Users)},
+ {<<"vms">>, length(Vms)} |
+ Metrics]},
{warnings, Warnings}], Req, State};
_ ->
{[{warnings, [{cloud, <<"down!">>}]}], Req, State}
@@ -10,8 +10,10 @@
content_types_accepted/2,
allowed_methods/2,
resource_exists/2,
+ delete_resource/2,
forbidden/2,
options/2,
+ service_available/2,
is_authorized/2]).
-export([to_json/2,
@@ -28,16 +30,27 @@
init/3,
is_authorized/2,
options/2,
+ service_available/2,
resource_exists/2,
rest_init/2]).
--record(state, {path, method, version, token, content, reply}).
+-record(state, {path, method, version, token, content, reply, obj, body}).
init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_http_rest}.
rest_init(Req, _) ->
wiggle_handler:initial_state(Req, <<"datasets">>).
+service_available(Req, State) ->
+ case {libsniffle:servers(), libsnarl:servers()} of
+ {[], _} ->
+ {false, Req, State};
+ {_, []} ->
+ {false, Req, State};
+ _ ->
+ {true, Req, State}
+ end.
+
options(Req, State) ->
Methods = allowed_methods(Req, State, State#state.path),
{ok, Req1} = cowboy_http_req:set_resp_header(
@@ -62,17 +75,17 @@ allowed_methods(_Version, _Token, []) ->
['GET'];
allowed_methods(_Version, _Token, [_Dataset]) ->
- ['GET'].
+ ['GET', 'DELETE'].
resource_exists(Req, State = #state{path = []}) ->
{true, Req, State};
resource_exists(Req, State = #state{path = [Dataset]}) ->
case libsniffle:dataset_get(Dataset) of
- not_found ->
+ {ok, not_found} ->
{false, Req, State};
- {ok, _} ->
- {true, Req, State}
+ {ok, Obj} ->
+ {true, Req, State#state{obj = Obj}}
end.
is_authorized(Req, State = #state{method = 'OPTIONS'}) ->
@@ -91,11 +104,14 @@ forbidden(Req, State = #state{token = undefined}) ->
{true, Req, State};
forbidden(Req, State = #state{path = []}) ->
- {allowed(State#state.token, [<<"datasets">>]), Req, State};
+ {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 = 'DELETE', path = [Dataset]}) ->
+ {allowed(State#state.token, [<<"datasets">>, Dataset, <<"delete">>]), Req, State};
+
forbidden(Req, State) ->
{true, Req, State}.
@@ -109,13 +125,11 @@ to_json(Req, State) ->
handle_request(Req, State = #state{token = Token, path = []}) ->
{ok, Permissions} = libsnarl:user_cache({token, Token}),
- {ok, Res} = libsniffle:dataset_list([{must, 'allowed', [<<"datast">>, {<<"res">>, <<"name">>}, <<"get">>], Permissions}]),
+ {ok, Res} = libsniffle:dataset_list([{must, 'allowed', [<<"datasets">>, {<<"res">>, <<"dataset">>}, <<"get">>], Permissions}]),
{lists:map(fun ({E, _}) -> E end, Res), Req, State};
-handle_request(Req, State = #state{path = [Dataset]}) ->
- {ok, Res} = libsniffle:dataset_get(Dataset),
- {Res, Req, State}.
-
+handle_request(Req, State = #state{path = [_Dataset], obj = Obj}) ->
+ {Obj, Req, State}.
%%--------------------------------------------------------------------
%% PUT
@@ -133,10 +147,18 @@ from_json(Req, State) ->
{Reply, Req2, State1}.
handle_write(Req, State, _Body) ->
- {fase, Req, State}.
+ {false, Req, State}.
+
+%%--------------------------------------------------------------------
+%% DELETE
+%%--------------------------------------------------------------------
+
+delete_resource(Req, State = #state{path = [Dataset]}) ->
+ ok = libsniffle:dataset_delete(Dataset),
+ {true, Req, State}.
%%--------------------------------------------------------------------
-%% DEETE
+%% Internal Functions
%%--------------------------------------------------------------------
allowed(Token, Perm) ->
@@ -15,7 +15,10 @@
delete_resource/2,
resource_exists/2,
forbidden/2,
+ service_available/2,
options/2,
+ create_path/2,
+ post_is_create/2,
is_authorized/2]).
-export([to_json/2,
@@ -30,18 +33,32 @@
forbidden/2,
init/3,
is_authorized/2,
+ service_available/2,
options/2,
+ create_path/2,
+ post_is_create/2,
resource_exists/2,
rest_init/2]).
--record(state, {path, method, version, token, content, reply}).
+-record(state, {path, method, version, token, content, reply, obj, body}).
init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_http_rest}.
rest_init(Req, _) ->
wiggle_handler:initial_state(Req, <<"groups">>).
+post_is_create(Req, State) ->
+ {true, Req, State}.
+
+service_available(Req, State) ->
+ case libsnarl:servers() of
+ [] ->
+ {false, Req, State};
+ _ ->
+ {true, Req, State}
+ end.
+
options(Req, State) ->
Methods = allowed_methods(Req, State, State#state.path),
{ok, Req1} = cowboy_http_req:set_resp_header(
@@ -64,7 +81,7 @@ 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, [_Group]) ->
['GET', 'PUT', 'DELETE'];
@@ -79,10 +96,10 @@ resource_exists(Req, State = #state{path = [Group, <<"permissions">> | Permissio
case {erlangify_permission(Permission), libsnarl:group_get(Group)} of
{_, not_found} ->
{false, Req, State};
- {[], {ok, _}} ->
- {true, Req, State};
- {P, {ok, GroupObj}} ->
- {lists:member(P, jsxd:get(<<"permissions">>, [], GroupObj)), Req, State}
+ {[], {ok, Obj}} ->
+ {true, Req, State#state{obj=Obj}};
+ {P, {ok, Obj}} ->
+ {lists:member(P, jsxd:get(<<"permissions">>, [], Obj)), Req, State#state{obj=Obj}}
end;
resource_exists(Req, State = #state{path = []}) ->
@@ -92,8 +109,8 @@ resource_exists(Req, State = #state{path = [Group | _]}) ->
case libsnarl:group_get(Group) of
{ok, not_found} ->
{false, Req, State};
- {ok, _} ->
- {true, Req, State}
+ {ok, Obj} ->
+ {true, Req, State#state{obj=Obj}}
end.
is_authorized(Req, State = #state{path = [_, <<"sessions">>]}) ->
@@ -117,8 +134,11 @@ forbidden(Req, State = #state{method = 'OPTIONS'}) ->
forbidden(Req, State = #state{token = undefined}) ->
{true, Req, State};
-forbidden(Req, State = #state{path = []}) ->
- {allowed(State#state.token, [<<"groups">>]), Req, State};
+forbidden(Req, State = #state{method = 'GET', path = []}) ->
+ {allowed(State#state.token, [<<"cloud">>, <<"groups">>, <<"list">>]), Req, State};
+
+forbidden(Req, State = #state{method = 'POST', path = []}) ->
+ {allowed(State#state.token, [<<"cloud">>, <<"groups">>, <<"create">>]), Req, State};
forbidden(Req, State = #state{method = 'GET', path = [Group]}) ->
{allowed(State#state.token, [<<"groups">>, Group, <<"get">>]), Req, State};
@@ -153,27 +173,38 @@ to_json(Req, State) ->
{Reply, Req1, State1} = handle_request(Req, State),
{jsx:encode(Reply), Req1, State1}.
-handle_request(Req, State = #state{token = _Token, path = []}) ->
+handle_request(Req, State = #state{path = []}) ->
% {ok, Permissions} = libsnarl:user_cache({token, Token}),
{ok, Res} = libsnarl:group_list(), %{must, 'allowed', [<<"vm">>, {<<"res">>, <<"uuid">>}, <<"get">>], Permissions}),
{Res, Req, State};
-handle_request(Req, State = #state{path = [Group]}) ->
- {ok, GroupObj} = libsnarl:group_get(Group),
+handle_request(Req, State = #state{path = [_Group], obj = GroupObj}) ->
GroupObj1 = jsxd:update(<<"permissions">>,
fun (Permissions) ->
lists:map(fun jsonify_permissions/1, Permissions)
end, [], GroupObj),
{GroupObj1, Req, State};
-handle_request(Req, State = #state{path = [Group, <<"permissions">>]}) ->
- {ok, GroupObj} = libsnarl:group_get(Group),
+handle_request(Req, State = #state{path = [_Group, <<"permissions">>], obj = GroupObj}) ->
{lists:map(fun jsonify_permissions/1, jsxd:get(<<"permissions">>, [], GroupObj)), Req, State}.
%%--------------------------------------------------------------------
%% PUT
%%--------------------------------------------------------------------
+create_path(Req, State = #state{path = [], version = Version}) ->
+ {ok, Body, Req1} = cowboy_http_req:body(Req),
+ {Decoded, Req2} = case Body of
+ <<>> ->
+ {[], Req1};
+ _ ->
+ D = jsx:decode(Body),
+ {D, Req1}
+ end,
+ {ok, Group} = jsxd:get(<<"name">>, Decoded),
+ {ok, UUID} = libsnarl:group_add(Group),
+ {<<"/api/", Version/binary, "/groups/", UUID/binary>>, Req2, State}.
+
from_json(Req, State) ->
{ok, Body, Req1} = cowboy_http_req:body(Req),
{Reply, Req2, State1} = case Body of
@@ -185,6 +216,10 @@ from_json(Req, State) ->
end,
{Reply, Req2, State1}.
+%% TODO : This is a icky case it is called after post.
+handle_write(Req, State = #state{method = 'POST', path = []}, _) ->
+ {true, Req, State};
+
handle_write(Req, State = #state{path = [Group]}, _Body) ->
ok = libsnarl:group_add(Group),
{true, Req, State};
Oops, something went wrong.

0 comments on commit 64e97bc

Please sign in to comment.