]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ws-auth.cc: Split apiServerAutoprimaries to GET and POST variants
authorAki Tuomi <cmouse@cmouse.fi>
Fri, 18 Aug 2023 08:05:05 +0000 (11:05 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Fri, 15 Dec 2023 07:59:57 +0000 (09:59 +0200)
Enables us to specify method routes for this later.

pdns/ws-auth.cc

index 75c240679b397a0540c2be452f905b47fdbb3d6a..2e2e370470aebe366549b71bf81818a93f159711 100644 (file)
@@ -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<AutoPrimary> 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<AutoPrimary> 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