Skip to content

Commit

Permalink
Refactoring and always deal with users the same.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeiklejohn committed Dec 19, 2012
1 parent cc56e4a commit ab4d318
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
40 changes: 30 additions & 10 deletions src/riak_cs_control_formatting.erl
Expand Up @@ -12,25 +12,36 @@
-module(riak_cs_control_formatting). -module(riak_cs_control_formatting).
-author('Christopher Meiklejohn <cmeiklejohn@basho.com>'). -author('Christopher Meiklejohn <cmeiklejohn@basho.com>').


-export([strip_root_node/1, -export([format_incoming_user/1,
format_users/1,
format_user/1, format_user/1,
iso8601/1]). iso8601/1]).


%% @spec strip_root_node(list()) -> list() %% @doc Format an incoming user for an S3 request.
%% @doc Remove root node from user structure. %% @spec format_incoming_user(list()) -> list()
strip_root_node(Attributes) -> format_incoming_user(Attributes) ->
{struct, [{<<"user">>, DecodedAttributes}]} = mochijson2:decode(Attributes), {struct, [{<<"user">>, DecodedAttributes}]} = mochijson2:decode(Attributes),
mochijson2:encode(DecodedAttributes). mochijson2:encode(DecodedAttributes).



%% @doc Format a user, by ensuring that all keys are atomized.
%% @spec format_user({term(), list()}) -> {term(), list()} %% @spec format_user(term()) -> term()
%% @doc Format a user object, by adding a admin key derived from the format_user({struct, _} = Attributes) ->
%% running environment. {struct, AttributeList} = atomize(Attributes),
format_user({struct, Attributes}) -> {struct, append_admin_status(AttributeList)};
format_user(Attributes) ->
Attributes.

%% @doc Given a proplist of attributes, add admin status.
%% @spec append_admin_status(proplist()) -> proplist()
append_admin_status(Attributes) ->
RiakCsAdminKey = riak_cs_control_configuration:cs_configuration(cs_admin_key), RiakCsAdminKey = riak_cs_control_configuration:cs_configuration(cs_admin_key),
BinaryAdminKey = list_to_binary(RiakCsAdminKey),
KeyId = proplists:get_value(key_id, Attributes), KeyId = proplists:get_value(key_id, Attributes),
{struct, Attributes ++ {admin, KeyId =:= RiakCsAdminKey}}. lists:append(Attributes, [{admin, KeyId =:= BinaryAdminKey}]).


%% @spec format_users(list()) -> list()
%% @doc Format an entire list of users.
format_users(Users) -> [format_user(User) || User <- Users].


%% @spec iso8601({{term(), term(), term()}, %% @spec iso8601({{term(), term(), term()},
%% {term(), term(), term()}}) -> binary() %% {term(), term(), term()}}) -> binary()
Expand All @@ -39,3 +50,12 @@ iso8601({{Y,M,D},{H,I,S}}) ->
iolist_to_binary( iolist_to_binary(
io_lib:format("~4..0b~2..0b~2..0bT~2..0b~2..0b~2..0bZ", io_lib:format("~4..0b~2..0b~2..0bT~2..0b~2..0b~2..0bZ",
[Y, M, D, H, I, S])). [Y, M, D, H, I, S])).

%% @doc Given a struct/proplist that we've received via JSON,
%% recursively turn the keys into atoms from binaries.
atomize({struct,L}) ->
{struct, [{binary_to_atom(I, utf8), atomize(J)} || {I, J} <- L]};
atomize(L) when is_list(L) ->
[atomize(I) || I <- L];
atomize(X) ->
X.
21 changes: 11 additions & 10 deletions src/riak_cs_control_session.erl
Expand Up @@ -62,19 +62,23 @@ init([]) ->


handle_call(get_users, _From, State) -> handle_call(get_users, _From, State) ->
Response = handle_request({multipart_get, "users"}), Response = handle_request({multipart_get, "users"}),
{reply, {ok, Response}, State}; Users = riak_cs_control_formatting:format_users(Response),
{reply, {ok, Users}, State};


handle_call({get_user, KeyId}, _From, State) -> handle_call({get_user, KeyId}, _From, State) ->
Response = handle_request({get, "user/" ++ KeyId}), Response = handle_request({get, "user/" ++ KeyId}),
{reply, {ok, Response}, State}; User = riak_cs_control_formatting:format_user(Response),
{reply, {ok, User}, State};


handle_call({put_user, Attributes}, _From, State) -> handle_call({put_user, Attributes}, _From, State) ->
Response = handle_request({put, "user", Attributes}), Response = handle_request({put, "user", Attributes}),
{reply, {ok, Response}, State}; User = riak_cs_control_formatting:format_user(Response),
{reply, {ok, User}, State};


handle_call({put_user, KeyId, Attributes}, _From, State) -> handle_call({put_user, KeyId, Attributes}, _From, State) ->
Response = handle_request({put, "user/" ++ KeyId, Attributes}), Response = handle_request({put, "user/" ++ KeyId, Attributes}),
{reply, {ok, Response}, State}; User = riak_cs_control_formatting:format_user(Response),
{reply, {ok, User}, State};


handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
{reply, ok, State}. {reply, ok, State}.
Expand Down Expand Up @@ -133,8 +137,7 @@ handle_request({multipart_get, Url}) ->
case get_request(Url) of case get_request(Url) of
{ok, Content} -> {ok, Content} ->
riak_cs_control_multipart:parse_multipart_response(Content); riak_cs_control_multipart:parse_multipart_response(Content);
{error, Reason} -> _ ->
lager:info("Multipart request failed: ~s", [Reason]),
[] []
end; end;


Expand All @@ -145,8 +148,7 @@ handle_request({get, Url}) ->
{ok, Content} -> {ok, Content} ->
Body = proplists:get_value(content, Content), Body = proplists:get_value(content, Content),
mochijson2:decode(Body); mochijson2:decode(Body);
{error, Reason} -> _ ->
lager:info("Get request failed: ~s", [Reason]),
empty_response() empty_response()
end; end;


Expand All @@ -156,7 +158,6 @@ handle_request({put, Url, Body}) ->
case put_request(Url, Body) of case put_request(Url, Body) of
{ok, {_Headers, Body}} -> {ok, {_Headers, Body}} ->
mochijson2:decode(Body); mochijson2:decode(Body);
{error, Reason} -> _ ->
lager:info("Put request failed: ~s", [Reason]),
empty_response() empty_response()
end. end.
2 changes: 1 addition & 1 deletion src/riak_cs_control_wm_user.erl
Expand Up @@ -90,7 +90,7 @@ from_json(ReqData, Context) ->
case maybe_retrieve_user(Context, KeyId) of case maybe_retrieve_user(Context, KeyId) of
{true, NewContext} -> {true, NewContext} ->
Attributes = wrq:req_body(ReqData), Attributes = wrq:req_body(ReqData),
NewAttributes = riak_cs_control_formatting:strip_root_node(Attributes), NewAttributes = riak_cs_control_formatting:format_incoming_user(Attributes),
case riak_cs_control_session:put_user(KeyId, NewAttributes) of case riak_cs_control_session:put_user(KeyId, NewAttributes) of
{ok, _Response} -> {ok, _Response} ->
Resource = "/users/" ++ KeyId, Resource = "/users/" ++ KeyId,
Expand Down
4 changes: 2 additions & 2 deletions src/riak_cs_control_wm_users.erl
Expand Up @@ -56,7 +56,7 @@ post_is_create(ReqData, Context) ->
%% @doc Extract key out of response from riak-cs. %% @doc Extract key out of response from riak-cs.
extract_key_id(User) -> extract_key_id(User) ->
{struct, UserDetails} = User, {struct, UserDetails} = User,
binary_to_list(proplists:get_value(list_to_binary("key_id"), UserDetails)). binary_to_list(proplists:get_value(key_id, UserDetails)).


%% @doc Attempt to create the user if possible, and generate the path %% @doc Attempt to create the user if possible, and generate the path
%% using the key_id of the new user. %% using the key_id of the new user.
Expand Down Expand Up @@ -107,7 +107,7 @@ maybe_create_user(ReqData, Context) ->
case Context#context.user of case Context#context.user of
undefined -> undefined ->
Attributes = wrq:req_body(ReqData), Attributes = wrq:req_body(ReqData),
NewAttributes = riak_cs_control_formatting:strip_root_node(Attributes), NewAttributes = riak_cs_control_formatting:format_incoming_user(Attributes),
case riak_cs_control_session:put_user(NewAttributes) of case riak_cs_control_session:put_user(NewAttributes) of
{ok, Response} -> {ok, Response} ->
{true, Context#context{user=Response}}; {true, Context#context{user=Response}};
Expand Down

0 comments on commit ab4d318

Please sign in to comment.