From: Christian Hofstaedtler Date: Tue, 8 Apr 2014 12:57:59 +0000 (+0200) Subject: API: merge domain manipulation from create and update X-Git-Tag: rec-3.6.0-rc1~72^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bb9fd22320009a58584d1b2516df8cf73c92d96a;p=thirdparty%2Fpdns.git API: merge domain manipulation from create and update Remove code duplication. --- diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 23580774bc..7d9d4b441c 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -370,6 +370,20 @@ void productServerStatisticsFetch(map& out) static void patchZone(HttpRequest* req, HttpResponse* resp); +static void updateDomainSettingsFromDocument(const DomainInfo& di, const string& zonename, Document& document) { + string master; + const Value &masters = document["masters"]; + if (masters.IsArray()) { + for (SizeType i = 0; i < masters.Size(); ++i) { + master += masters[i].GetString(); + master += " "; + } + } + + di.backend->setKind(zonename, DomainInfo::stringToKind(stringFromJson(document, "kind"))); + di.backend->setMaster(zonename, master); +} + static void apiServerZones(HttpRequest* req, HttpResponse* resp) { UeberBackend B; if (req->method == "POST" && !::arg().mustDo("experimental-api-readonly")) { @@ -386,25 +400,17 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) { zonename.resize(zonename.size()-1); } - string kind = stringFromJson(document, "kind"); - bool exists = B.getDomainInfo(zonename, di); if(exists) throw ApiException("Domain '"+zonename+"' already exists"); + // validate 'kind' is set + stringFromJson(document, "kind"); + const Value &nameservers = document["nameservers"]; if (!nameservers.IsArray() || nameservers.Size() == 0) throw ApiException("Need at least one nameserver"); - string master; - const Value &masters = document["masters"]; - if (masters.IsArray()) { - for (SizeType i = 0; i < masters.Size(); ++i) { - master += masters[i].GetString(); - master += " "; - } - } - // no going back after this if(!B.createDomain(zonename)) throw ApiException("Creating domain '"+zonename+"' failed"); @@ -444,8 +450,7 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) { } di.backend->commitTransaction(); - di.backend->setKind(zonename, DomainInfo::stringToKind(kind)); - di.backend->setMaster(zonename, master); + updateDomainSettingsFromDocument(di, zonename, document); fillZone(zonename, resp); return; @@ -500,17 +505,8 @@ static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) { Document document; req->json(document); - string master; - const Value &masters = document["masters"]; - if (masters.IsArray()) { - for(SizeType i = 0; i < masters.Size(); ++i) { - master += masters[i].GetString(); - master += " "; - } - } + updateDomainSettingsFromDocument(di, zonename, document); - di.backend->setKind(zonename, DomainInfo::stringToKind(stringFromJson(document, "kind"))); - di.backend->setMaster(zonename, master); fillZone(zonename, resp); return; }