Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #238 from heroku/ypaq-control-rod-to-disable-legac…
Browse files Browse the repository at this point in the history
…y-api

Add control rod to disable legacy api
  • Loading branch information
cyx authored Mar 21, 2018
2 parents 22d3fef + 7f955d0 commit ca6bd40
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
40 changes: 32 additions & 8 deletions bin/control_rods
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,42 @@ main(["enable_firehose"]) ->
halt(0);
main(["disable_api"]) ->
Node = connect(),
io:format("Disabling API (v2 & v3)...~n"),
Res = rpc:call(Node, logplex_api, set_status, [disabled]),
io:format("Disabling v3 API...~n"),
Res = rpc:call(Node, logplex_api_v3, set_status, [disabled]),
io:format("Node: ~p~n", [Res]),
disconnect(),
halt(0);
main(["enable_api"]) ->
Node = connect(),
io:format("Enabling API (v2 & v3)...~n"),
Res = rpc:call(Node, logplex_api, set_status, [normal]),
io:format("Enabling v3 API ...~n"),
Res = rpc:call(Node, logplex_api_v3, set_status, [normal]),
io:format("Node: ~p~n", [Res]),
disconnect(),
halt(0);
main(["set_api_read_only"]) ->
Node = connect(),
io:format("Setting API (v2 & v3) in read-only mode...~n"),
io:format("Setting v3 API in read-only mode...~n"),
Res = rpc:call(Node, logplex_api_v3, set_status, [read_only]),
io:format("Node: ~p~n", [Res]),
disconnect(),
halt(0);
main(["disable_legacy_api"]) ->
Node = connect(),
io:format("Disabling legacy API ...~n"),
Res = rpc:call(Node, logplex_api, set_status, [disabled]),
io:format("Node: ~p~n", [Res]),
disconnect(),
halt(0);
main(["enable_legacy_api"]) ->
Node = connect(),
io:format("Enabling legacy API ...~n"),
Res = rpc:call(Node, logplex_api, set_status, [normal]),
io:format("Node: ~p~n", [Res]),
disconnect(),
halt(0);
main(["set_legacy_api_read_only"]) ->
Node = connect(),
io:format("Setting legacy API in read-only mode...~n"),
Res = rpc:call(Node, logplex_api, set_status, [read_only]),
io:format("Node: ~p~n", [Res]),
disconnect(),
Expand All @@ -115,9 +136,12 @@ main(_) ->
"allow_logs_ress\t\t\tAllow logs ingress.~n~n"
"disable_firehose\t\tDisable firehose.~n"
"enable_firehose\t\t\tEnable firehose.~n~n"
"disable_api\t\t\tDisable API (v2 & v3).~n"
"enable_api\t\t\tEnable API (v2 & v3).~n"
"set_api_read_only\t\tSet API (v2 & v3) in read-only mode.~n"
"disable_api\t\t\tDisable v3 API.~n"
"enable_api\t\t\tEnable v3 API.~n"
"set_api_read_only\t\tSet v3 API in read-only mode.~n"
"disable_legacy_api\t\t\tDisable legacy API.~n"
"enable_legacy_api\t\t\tEnable legacy API.~n"
"set_legacy_api_read_only\t\tSet legacy API in read-only mode.~n"
),
halt(0).

Expand Down
4 changes: 2 additions & 2 deletions src/logplex_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ valid_uri(Req) ->
%% Checks whether the API state
-spec status() -> 'normal' | 'read_only' | 'disabled'.
status() ->
case logplex_app:config(api_status, normal) of
case logplex_app:config(legacy_api_status, normal) of
Status when Status == normal;
Status == disabled;
Status == read_only ->
Expand All @@ -743,7 +743,7 @@ set_status(Term) ->
"canary operations allowed.~n");
disabled -> io:format("API entirely disabled~n")
end,
logplex_app:set_config(api_status, Term),
logplex_app:set_config(legacy_api_status, Term),
Old.

-spec status_io(pos_integer()) -> binary().
Expand Down
13 changes: 12 additions & 1 deletion src/logplex_api_v3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

%% setup functions
-export([setup_metrics/0,
child_spec/0
child_spec/0,
set_status/1
]).

%% rest helpers
Expand Down Expand Up @@ -72,6 +73,16 @@ tokens_path() ->
channel_logs_path() ->
{?CHANNEL_LOGS_PATH, logplex_api_v3_channel_logs, [{route, ?CHANNEL_LOGS_PATH}]}.

-spec set_status(normal | disabled | read_only) -> ok.
set_status(Status) ->
case Status of
normal -> io:format("Fully Enabling API~n");
read_only -> io:format("API in read-only mode: only GET requests~n");
disabled -> io:format("API entirely disabled~n")
end,
logplex_app:set_config(api_status, Status),
ok.

%% ----------------------------------------------------------------------------
%% REST helpers
%%
Expand Down
3 changes: 3 additions & 0 deletions src/logplex_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ cache_os_envvars() ->
,{api_status, ["API_STATUS"],
optional,
atom}
,{legacy_api_status, ["LEGACY_API_STATUS"],
optional,
atom}
]),
ok.

Expand Down
12 changes: 6 additions & 6 deletions test/logplex_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ end_per_suite(_Config) ->
application:stop(logplex).

init_per_group(read_only, Config) ->
InitialStatus = application:get_env(logplex, api_status),
InitialStatus = application:get_env(logplex, legacy_api_status),
logplex_api:set_status(read_only),
read_only = logplex_api:status(),
[{initial_api_status, InitialStatus} | Config];
init_per_group(disabled, Config) ->
InitialStatus = application:get_env(logplex, api_status),
InitialStatus = application:get_env(logplex, legacy_api_status),
logplex_api:set_status(disabled),
disabled = logplex_api:status(),
[{initial_api_status, InitialStatus} | Config];
Expand All @@ -48,13 +48,13 @@ init_per_group(_, Config) ->

end_per_group(read_only, Config) ->
case ?config(initial_api_status, Config) of
undefined -> application:unset_env(logplex, api_status);
{ok,Val} -> application:set_env(logplex, api_status, Val)
undefined -> application:unset_env(logplex, legacy_api_status);
{ok,Val} -> application:set_env(logplex, legacy_api_status, Val)
end;
end_per_group(disabled, Config) ->
case ?config(initial_api_status, Config) of
undefined -> application:unset_env(logplex, api_status);
{ok,Val} -> application:set_env(logplex, api_status, Val)
undefined -> application:unset_env(logplex, legacy_api_status);
{ok,Val} -> application:set_env(logplex, legacy_api_status, Val)
end;
end_per_group(_, _Config) ->
ok.
Expand Down
4 changes: 2 additions & 2 deletions test/logplex_api_v3_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ init_per_testcase(Testcase , Config)
Testcase == drains_service_unavailable;
Testcase == tokens_service_unavailable;
Testcase == channel_logs_service_unavailable ->
logplex_app:set_config(api_status, disabled),
logplex_api_v3:set_status(disabled),
Config;
init_per_testcase(Testcase , Config)
when Testcase == channel_not_authorized;
Expand Down Expand Up @@ -117,7 +117,7 @@ end_per_testcase(Testcase, Config)
Testcase == drains_service_unavailable;
Testcase == tokens_service_unavailable;
Testcase == channel_logs_service_unavailable ->
logplex_app:set_config(api_status, normal),
logplex_api_v3:set_status(normal),
Config;
end_per_testcase(cannot_add_more_drains, Config) ->
OldLimit = ?config(old_max_drains_per_channel, Config),
Expand Down

0 comments on commit ca6bd40

Please sign in to comment.