Merge pull request #13555 from ieQu1/ds-rest-404
fix(mgmt): Return 404 for /ds/ API endpoints when DS is disabled
This commit is contained in:
commit
6058b50c91
|
@ -87,7 +87,8 @@ schema("/ds/sites") ->
|
||||||
tags => ?TAGS,
|
tags => ?TAGS,
|
||||||
responses =>
|
responses =>
|
||||||
#{
|
#{
|
||||||
200 => mk(array(binary()), #{desc => <<"List sites">>})
|
200 => mk(array(binary()), #{desc => <<"List sites">>}),
|
||||||
|
404 => disabled_schema()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -115,7 +116,8 @@ schema("/ds/storages") ->
|
||||||
tags => ?TAGS,
|
tags => ?TAGS,
|
||||||
responses =>
|
responses =>
|
||||||
#{
|
#{
|
||||||
200 => mk(array(atom()), #{desc => <<"List durable storages">>})
|
200 => mk(array(atom()), #{desc => <<"List durable storages">>}),
|
||||||
|
404 => disabled_schema()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -130,7 +132,8 @@ schema("/ds/storages/:ds") ->
|
||||||
responses =>
|
responses =>
|
||||||
#{
|
#{
|
||||||
200 => mk(ref(db), #{desc => <<"Get information about a durable storage">>}),
|
200 => mk(ref(db), #{desc => <<"Get information about a durable storage">>}),
|
||||||
400 => not_found(<<"Durable storage">>)
|
400 => not_found(<<"Durable storage">>),
|
||||||
|
404 => disabled_schema()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -148,7 +151,8 @@ schema("/ds/storages/:ds/replicas") ->
|
||||||
200 => mk(array(binary()), #{
|
200 => mk(array(binary()), #{
|
||||||
desc => <<"List sites that contain replicas of the durable storage">>
|
desc => <<"List sites that contain replicas of the durable storage">>
|
||||||
}),
|
}),
|
||||||
400 => not_found(<<"Durable storage">>)
|
400 => not_found(<<"Durable storage">>),
|
||||||
|
404 => disabled_schema()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
put =>
|
put =>
|
||||||
|
@ -159,7 +163,8 @@ schema("/ds/storages/:ds/replicas") ->
|
||||||
responses =>
|
responses =>
|
||||||
#{
|
#{
|
||||||
202 => mk(array(binary()), #{}),
|
202 => mk(array(binary()), #{}),
|
||||||
400 => bad_request()
|
400 => bad_request(),
|
||||||
|
404 => disabled_schema()
|
||||||
},
|
},
|
||||||
'requestBody' => mk(array(binary()), #{desc => <<"New list of sites">>})
|
'requestBody' => mk(array(binary()), #{desc => <<"New list of sites">>})
|
||||||
}
|
}
|
||||||
|
@ -296,10 +301,15 @@ fields(db_site) ->
|
||||||
%%================================================================================
|
%%================================================================================
|
||||||
|
|
||||||
list_sites(get, _Params) ->
|
list_sites(get, _Params) ->
|
||||||
{200, emqx_ds_replication_layer_meta:sites()}.
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
|
{200, emqx_ds_replication_layer_meta:sites()};
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
|
end.
|
||||||
|
|
||||||
get_site(get, #{bindings := #{site := Site}}) ->
|
get_site(get, #{bindings := #{site := Site}}) ->
|
||||||
case lists:member(Site, emqx_ds_replication_layer_meta:sites()) of
|
case is_enabled() andalso lists:member(Site, emqx_ds_replication_layer_meta:sites()) of
|
||||||
false ->
|
false ->
|
||||||
?NOT_FOUND(<<"Site not found: ", Site/binary>>);
|
?NOT_FOUND(<<"Site not found: ", Site/binary>>);
|
||||||
true ->
|
true ->
|
||||||
|
@ -314,33 +324,60 @@ get_site(get, #{bindings := #{site := Site}}) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
list_dbs(get, _Params) ->
|
list_dbs(get, _Params) ->
|
||||||
?OK(dbs()).
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
|
?OK(dbs());
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
|
end.
|
||||||
|
|
||||||
get_db(get, #{bindings := #{ds := DB}}) ->
|
get_db(get, #{bindings := #{ds := DB}}) ->
|
||||||
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
?OK(#{
|
?OK(#{
|
||||||
name => DB,
|
name => DB,
|
||||||
shards => list_shards(DB)
|
shards => list_shards(DB)
|
||||||
}).
|
});
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
|
end.
|
||||||
|
|
||||||
db_replicas(get, #{bindings := #{ds := DB}}) ->
|
db_replicas(get, #{bindings := #{ds := DB}}) ->
|
||||||
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
Replicas = emqx_ds_replication_layer_meta:db_sites(DB),
|
Replicas = emqx_ds_replication_layer_meta:db_sites(DB),
|
||||||
?OK(Replicas);
|
?OK(Replicas);
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
|
end;
|
||||||
db_replicas(put, #{bindings := #{ds := DB}, body := Sites}) ->
|
db_replicas(put, #{bindings := #{ds := DB}, body := Sites}) ->
|
||||||
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
case update_db_sites(DB, Sites, rest) of
|
case update_db_sites(DB, Sites, rest) of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
{202, <<"OK">>};
|
{202, <<"OK">>};
|
||||||
{error, Description} ->
|
{error, Description} ->
|
||||||
?BAD_REQUEST(400, Description)
|
?BAD_REQUEST(400, Description)
|
||||||
|
end;
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
end.
|
end.
|
||||||
|
|
||||||
db_replica(put, #{bindings := #{ds := DB, site := Site}}) ->
|
db_replica(put, #{bindings := #{ds := DB, site := Site}}) ->
|
||||||
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
case join(DB, Site, rest) of
|
case join(DB, Site, rest) of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
{202, <<"OK">>};
|
{202, <<"OK">>};
|
||||||
{error, Description} ->
|
{error, Description} ->
|
||||||
?BAD_REQUEST(400, Description)
|
?BAD_REQUEST(400, Description)
|
||||||
end;
|
end;
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
|
end;
|
||||||
db_replica(delete, #{bindings := #{ds := DB, site := Site}}) ->
|
db_replica(delete, #{bindings := #{ds := DB, site := Site}}) ->
|
||||||
|
case is_enabled() of
|
||||||
|
true ->
|
||||||
case leave(DB, Site, rest) of
|
case leave(DB, Site, rest) of
|
||||||
{ok, Sites} when is_list(Sites) ->
|
{ok, Sites} when is_list(Sites) ->
|
||||||
{202, <<"OK">>};
|
{202, <<"OK">>};
|
||||||
|
@ -348,6 +385,9 @@ db_replica(delete, #{bindings := #{ds := DB, site := Site}}) ->
|
||||||
?NOT_FOUND(<<"Site is not part of replica set">>);
|
?NOT_FOUND(<<"Site is not part of replica set">>);
|
||||||
{error, Description} ->
|
{error, Description} ->
|
||||||
?BAD_REQUEST(400, Description)
|
?BAD_REQUEST(400, Description)
|
||||||
|
end;
|
||||||
|
false ->
|
||||||
|
err_disabled()
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec update_db_sites(emqx_ds:db(), [emqx_ds_replication_layer_meta:site()], rest | cli) ->
|
-spec update_db_sites(emqx_ds:db(), [emqx_ds_replication_layer_meta:site()], rest | cli) ->
|
||||||
|
@ -391,6 +431,9 @@ forget(Site, Via) ->
|
||||||
%% site_info(Site) ->
|
%% site_info(Site) ->
|
||||||
%% #{}.
|
%% #{}.
|
||||||
|
|
||||||
|
disabled_schema() ->
|
||||||
|
emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Durable storage is disabled">>).
|
||||||
|
|
||||||
not_found(What) ->
|
not_found(What) ->
|
||||||
emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<What/binary, " not found">>).
|
emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<What/binary, " not found">>).
|
||||||
|
|
||||||
|
@ -492,4 +535,10 @@ meta_result_to_binary({error, Err}) ->
|
||||||
IOList = io_lib:format("Error: ~p", [Err]),
|
IOList = io_lib:format("Error: ~p", [Err]),
|
||||||
{error, iolist_to_binary(IOList)}.
|
{error, iolist_to_binary(IOList)}.
|
||||||
|
|
||||||
|
is_enabled() ->
|
||||||
|
emqx_persistent_message:is_persistence_enabled().
|
||||||
|
|
||||||
|
err_disabled() ->
|
||||||
|
?NOT_FOUND(<<"Durable storage is disabled">>).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
Loading…
Reference in New Issue