You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2015/06/17 16:22:45 UTC

[2/2] chttpd commit: updated refs/heads/master to 1950654

Add _node_config, an endpoint to configure specific nodes from any node


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/19506543
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/19506543
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/19506543

Branch: refs/heads/master
Commit: 195065434e6c496abb7dd7b7b2fb800c4bc6a517
Parents: bd1f8ca
Author: Robert Newson <rn...@apache.org>
Authored: Wed Jun 17 14:41:50 2015 +0100
Committer: Robert Newson <rn...@apache.org>
Committed: Wed Jun 17 15:22:30 2015 +0100

----------------------------------------------------------------------
 src/chttpd.erl      |  2 +-
 src/chttpd_misc.erl | 45 +++++++++++++++++++++++++++++++--------------
 2 files changed, 32 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/19506543/src/chttpd.erl
----------------------------------------------------------------------
diff --git a/src/chttpd.erl b/src/chttpd.erl
index 5d1f4ee..19d01ed 100644
--- a/src/chttpd.erl
+++ b/src/chttpd.erl
@@ -366,7 +366,7 @@ url_handler("favicon.ico") ->   fun chttpd_misc:handle_favicon_req/1;
 url_handler("_utils") ->        fun chttpd_misc:handle_utils_dir_req/1;
 url_handler("_all_dbs") ->      fun chttpd_misc:handle_all_dbs_req/1;
 url_handler("_active_tasks") -> fun chttpd_misc:handle_task_status_req/1;
-url_handler("_config") ->       fun chttpd_misc:handle_config_req/1;
+url_handler("_node_config") ->  fun chttpd_misc:handle_node_config_req/1;
 url_handler("_reload_query_servers") -> fun chttpd_misc:handle_reload_query_servers_req/1;
 url_handler("_replicate") ->    fun chttpd_misc:handle_replicate_req/1;
 url_handler("_uuids") ->        fun chttpd_misc:handle_uuids_req/1;

http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/19506543/src/chttpd_misc.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_misc.erl b/src/chttpd_misc.erl
index 3afa30c..27b9bfd 100644
--- a/src/chttpd_misc.erl
+++ b/src/chttpd_misc.erl
@@ -14,7 +14,7 @@
 
 -export([
     handle_all_dbs_req/1,
-    handle_config_req/1,
+    handle_node_config_req/1,
     handle_favicon_req/1,
     handle_favicon_req/2,
     handle_replicate_req/1,
@@ -230,7 +230,7 @@ handle_uuids_req(Req) ->
 
 % GET /_config/
 % GET /_config
-handle_config_req(#httpd{method='GET', path_parts=[_]}=Req) ->
+handle_node_config_req(#httpd{method='GET', path_parts=[_, Node]}=Req) ->
     Grouped = lists:foldl(fun({{Section, Key}, Value}, Acc) ->
         case dict:is_key(Section, Acc) of
         true ->
@@ -238,45 +238,62 @@ handle_config_req(#httpd{method='GET', path_parts=[_]}=Req) ->
         false ->
             dict:store(Section, [{list_to_binary(Key), list_to_binary(Value)}], Acc)
         end
-    end, dict:new(), config:all()),
+    end, dict:new(), rpc_config(Node, all, [])),
     KVs = dict:fold(fun(Section, Values, Acc) ->
         [{list_to_binary(Section), {Values}} | Acc]
     end, [], Grouped),
     send_json(Req, 200, {KVs});
 % GET /_config/Section
-handle_config_req(#httpd{method='GET', path_parts=[_,Section]}=Req) ->
+handle_node_config_req(#httpd{method='GET', path_parts=[_, Node, Section]}=Req) ->
     KVs = [{list_to_binary(Key), list_to_binary(Value)}
-            || {Key, Value} <- config:get(Section)],
+            || {Key, Value} <- rpc_config(Node, get, [Section])],
     send_json(Req, 200, {KVs});
 % PUT /_config/Section/Key
 % "value"
-handle_config_req(#httpd{method='PUT', path_parts=[_, Section, Key]}=Req) ->
+handle_node_config_req(#httpd{method='PUT', path_parts=[_, Node, Section, Key]}=Req) ->
     Value = chttpd:json_body(Req),
     Persist = chttpd:header_value(Req, "X-Couch-Persist") /= "false",
-    OldValue = config:get(Section, Key, ""),
-    ok = config:set(Section, Key, ?b2l(Value), Persist),
+    OldValue = rpc_config(Node, get, [Section, Key, ""]),
+    ok = rpc_config(Node, set, [Section, Key, ?b2l(Value), Persist]),
     send_json(Req, 200, list_to_binary(OldValue));
 % GET /_config/Section/Key
-handle_config_req(#httpd{method='GET', path_parts=[_, Section, Key]}=Req) ->
-    case config:get(Section, Key, undefined) of
+handle_node_config_req(#httpd{method='GET', path_parts=[_, Node, Section, Key]}=Req) ->
+    case rpc_config(Node, get, [Section, Key, undefined]) of
     undefined ->
         throw({not_found, unknown_config_value});
     Value ->
         send_json(Req, 200, list_to_binary(Value))
     end;
 % DELETE /_config/Section/Key
-handle_config_req(#httpd{method='DELETE',path_parts=[_,Section,Key]}=Req) ->
+handle_node_config_req(#httpd{method='DELETE',path_parts=[_, Node, Section, Key]}=Req) ->
     Persist = chttpd:header_value(Req, "X-Couch-Persist") /= "false",
-    case config:get(Section, Key, undefined) of
+    case rpc_config(Node, get, [Section, Key, undefined]) of
     undefined ->
         throw({not_found, unknown_config_value});
     OldValue ->
-        config:delete(Section, Key, Persist),
+        rpc_config(Node, delete, [Section, Key, Persist]),
         send_json(Req, 200, list_to_binary(OldValue))
     end;
-handle_config_req(Req) ->
+handle_node_config_req(Req) ->
     send_method_not_allowed(Req, "GET,PUT,DELETE").
 
+rpc_config(Node0, Fun, Args) when is_binary(Node0) ->
+    Node1 = try
+                list_to_existing_atom(?b2l(Node0))
+            catch
+                error:badarg ->
+                    throw({not_found, <<"no such node: ", Node0/binary>>})
+            end,
+    rpc_config(Node1, Fun, Args);
+rpc_config(Node, Fun, Args) when is_atom(Node) ->
+    case rpc:call(Node, config, Fun,Args) of
+        {badrpc, nodedown} ->
+            Reason = ?l2b(io_lib:format("~s is down", [Node])),
+            throw({error, {nodedown, Reason}});
+        Else ->
+            Else
+    end.
+
 % Note: this resource is exposed on the backdoor interface, but it's in chttpd
 % because it's not couch trunk
 handle_system_req(Req) ->