%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

%% Endpoints

{mapping, "cluster_formation.etcd.endpoints", "rabbit.cluster_formation.peer_discovery_etcd.endpoints", [
    {datatype, {enum, [none]}}
]}.

{mapping, "cluster_formation.etcd.endpoints.$index", "rabbit.cluster_formation.peer_discovery_etcd.endpoints", [
    {datatype, [string, ip]}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.endpoints",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.endpoints", Conf, undefined) of
        none      -> [];
        _ ->
            Endpoints = cuttlefish_variable:filter_by_prefix("cluster_formation.etcd.endpoints", Conf),
            [V || {_, V} <- Endpoints]
    end
end}.

%% Legacy: etcd host

{mapping, "cluster_formation.etcd.host", "rabbit.cluster_formation.peer_discovery_etcd.etcd_host", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_host",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.host", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% Legacy: etcd port

{mapping, "cluster_formation.etcd.port", "rabbit.cluster_formation.peer_discovery_etcd.etcd_port", [
    {datatype, {integer, non_negative}}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_port",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.port", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% Legacy: etcd scheme. The key remains for backwards compatibility, it will not be used.

{mapping, "cluster_formation.etcd.scheme", "rabbit.cluster_formation.peer_discovery_etcd.etcd_scheme", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_scheme",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.scheme", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% key prefix appended after /rabbitmq/{discovery,locks} (a mandatory prefix we enforce as of #22)

{mapping, "cluster_formation.etcd.key_prefix", "rabbit.cluster_formation.peer_discovery_etcd.etcd_prefix", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_prefix",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.key_prefix", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% cluster name

{mapping, "cluster_formation.etcd.cluster_name", "rabbit.cluster_formation.peer_discovery_etcd.cluster_name", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.cluster_name",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.cluster_name", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% node key ttl

{mapping, "cluster_formation.etcd.node_ttl", "rabbit.cluster_formation.peer_discovery_etcd.etcd_node_ttl", [
    {datatype, {integer, non_negative}}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_node_ttl",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.node_ttl", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% lock acquisition timeout

{mapping, "cluster_formation.etcd.lock_wait_time", "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time", [
    {datatype, {integer, non_negative}}
]}.

{mapping, "cluster_formation.etcd.lock_timeout", "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time", [
    {datatype, {integer, non_negative}}
]}.

%% an alias for lock acquisition timeout to be consistent with the etcd backend

{translation, "rabbit.cluster_formation.peer_discovery_etcd.lock_wait_time",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.lock_timeout", Conf, undefined) of
        undefined ->
            case cuttlefish:conf_get("cluster_formation.etcd.lock_wait_time", Conf, undefined) of
                    undefined -> cuttlefish:unset();
                    Value     -> Value
                end;
        Value -> Value
    end
end}.

%% authentication

{mapping, "cluster_formation.etcd.username", "rabbit.cluster_formation.peer_discovery_etcd.etcd_username", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_username",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.username", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

{mapping, "cluster_formation.etcd.password", "rabbit.cluster_formation.peer_discovery_etcd.etcd_password", [
    {datatype, [tagged_binary, binary]}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.etcd_password",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.etcd.password", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%%
%% TLS client options
%%

{include_partial, {rabbit, "ssl_options"},
    [{prefix, "cluster_formation.etcd.ssl_options"},
     {app_prefix, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options"},
     {disable_with, none},
     {exclude, ["fail_if_no_peer_cert", "dh", "dhfile"]}]}.

%% `key.*` is kept inline so the no-match branch stays the atom
%% `undefined`, matching the historical (and buggy) translation shape.

{mapping, "cluster_formation.etcd.ssl_options.key.RSAPrivateKey",
    "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key",
    [{datatype, string}]}.

{mapping, "cluster_formation.etcd.ssl_options.key.DSAPrivateKey",
    "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key",
    [{datatype, string}]}.

{mapping, "cluster_formation.etcd.ssl_options.key.PrivateKeyInfo",
    "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key",
    [{datatype, string}]}.

{translation, "rabbit.cluster_formation.peer_discovery_etcd.ssl_options.key",
fun(Conf) ->
    case cuttlefish_variable:filter_by_prefix(
           "cluster_formation.etcd.ssl_options.key", Conf) of
        [{[_,_,Key], Val}|_] -> {list_to_atom(Key), list_to_binary(Val)};
        _ -> undefined
    end
end}.
