From 241dc035653e1ab2d4e7e38936b408c384ebf4cc Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Fri, 18 Aug 2023 11:05:05 +0300 Subject: [PATCH] ws-auth.cc: Split apiServerAutoprimaries to GET and POST variants Enables us to specify method routes for this later. --- pdns/ws-auth.cc | 74 ++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 75c240679b..2e2e370470 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -1729,41 +1729,53 @@ static void apiServerAutoprimaryDetail(HttpRequest* req, HttpResponse* resp) { } } -static void apiServerAutoprimaries(HttpRequest* req, HttpResponse* resp) { - UeberBackend B; +static void apiServerAutoprimariesGET(HttpRequest* /* req */, HttpResponse* resp) { + UeberBackend B; // NOLINT(readability-identifier-length) - if (req->method == "GET") { - std::vector primaries; - if (!B.autoPrimariesList(primaries)) - throw HttpInternalServerErrorException("Unable to retrieve autoprimaries"); - Json::array doc; - for (const auto& primary: primaries) { - Json::object obj = { - { "ip", primary.ip }, - { "nameserver", primary.nameserver }, - { "account", primary.account } - }; - doc.push_back(obj); - } - resp->setJsonBody(doc); - } else if (req->method == "POST") { - auto document = req->json(); - AutoPrimary primary(stringFromJson(document, "ip"), stringFromJson(document, "nameserver"), ""); + std::vector primaries; + if (!B.autoPrimariesList(primaries)) { + throw HttpInternalServerErrorException("Unable to retrieve autoprimaries"); + } + Json::array doc; + for (const auto& primary: primaries) { + const Json::object obj = { + { "ip", primary.ip }, + { "nameserver", primary.nameserver }, + { "account", primary.account } + }; + doc.push_back(obj); + } + resp->setJsonBody(doc); +} - if (document["account"].is_string()) { - primary.account = document["account"].string_value(); - } +static void apiServerAutoprimariesPOST(HttpRequest* req, HttpResponse* resp) { + UeberBackend B; // NOLINT(readability-identifier-length) - if (primary.ip=="" or primary.nameserver=="") { - throw ApiException("ip and nameserver fields must be filled"); - } - if (!B.autoPrimaryAdd(primary)) - throw HttpInternalServerErrorException("Cannot find backend with autoprimary feature"); - resp->body = ""; - resp->status = 201; - } else { - throw HttpMethodNotAllowedException(); + const auto& document = req->json(); + + AutoPrimary primary(stringFromJson(document, "ip"), stringFromJson(document, "nameserver"), ""); + + if (document["account"].is_string()) { + primary.account = document["account"].string_value(); + } + + if (primary.ip.empty() or primary.nameserver.empty()) { + throw ApiException("ip and nameserver fields must be filled"); + } + if (!B.autoPrimaryAdd(primary)) { + throw HttpInternalServerErrorException("Cannot find backend with autoprimary feature"); } + resp->body = ""; + resp->status = 201; +} + +static void apiServerAutoprimaries(HttpRequest* req, HttpResponse* resp) { + if (req->method == "GET") + apiServerAutoprimariesGET(req, resp); + else if (req->method == "POST") + apiServerAutoprimariesPOST(req, resp); + else + throw HttpMethodNotAllowedException(); } // create new zone -- 2.47.2