/
admin_partitions.erl
70 lines (59 loc) · 2.33 KB
/
admin_partitions.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(admin_partitions).
-export([routes/0,
init/1,
content_types_provided/2,
to_json/2,
is_authorized/2,
service_available/2,
forbidden/2
]).
%% riak_control and webmachine dependencies
-include_lib("riak_control/include/riak_control.hrl").
-include_lib("webmachine/include/webmachine.hrl").
%% mappings to the various content types supported for this resource
-define(CONTENT_TYPES,[{"application/json",to_json}]).
%% the different vnode types we care about
-define(VNODE_TYPES,[riak_kv,riak_pipe,riak_search]).
%% defines the webmachine routes this module handles
routes () ->
[{admin_routes:partitions_route(),?MODULE,[]}].
%% entry-point for the resource from webmachine
init ([]) ->
{ok,_V,Partitions}=riak_control_session:get_partitions(),
{ok,Partitions}.
%% validate origin
forbidden(RD, C) ->
{riak_control_security:is_null_origin(RD), RD, C}.
%% redirect to SSL port if using HTTP
service_available (RD,C) ->
riak_control_security:scheme_is_available(RD,C).
%% validate username and password
is_authorized (RD,C) ->
riak_control_security:enforce_auth(RD,C).
%% return the list of available content types for webmachine
content_types_provided (Req,C) ->
{?CONTENT_TYPES,Req,C}.
%% valid | invalid | joining | leaving | exiting
to_json (Req,C=Partitions) ->
{ok,_V,Nodes}=riak_control_session:get_nodes(),
Details=[{struct,riak_control_formatting:node_ring_details(P,Nodes)} || P <- Partitions],
{mochijson2:encode({struct,[{partitions,Details}]}), Req,C}.