From e289929f21a4060fd7920f5f70106ecf0ab723e5 Mon Sep 17 00:00:00 2001 From: Kristijan Mitrovic Date: Wed, 20 May 2020 16:35:40 +0200 Subject: [PATCH] Better DNS listing APIs usage --- .../cloud/dns/godaddy/GoDaddyDnsDriver.java | 24 +++++++++++-------- .../cloud/dns/route53/Route53DnsDriver.java | 22 +++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/bubble-server/src/main/java/bubble/cloud/dns/godaddy/GoDaddyDnsDriver.java b/bubble-server/src/main/java/bubble/cloud/dns/godaddy/GoDaddyDnsDriver.java index ba0e8abd..2610143e 100644 --- a/bubble-server/src/main/java/bubble/cloud/dns/godaddy/GoDaddyDnsDriver.java +++ b/bubble-server/src/main/java/bubble/cloud/dns/godaddy/GoDaddyDnsDriver.java @@ -32,6 +32,7 @@ import static org.cobbzilla.util.http.HttpMethods.PATCH; import static org.cobbzilla.util.http.HttpMethods.PUT; import static org.cobbzilla.util.json.JsonUtil.COMPACT_MAPPER; import static org.cobbzilla.util.json.JsonUtil.json; +import static org.cobbzilla.wizard.resources.ResourceUtil.invalidEx; public class GoDaddyDnsDriver extends DnsDriverBase { @@ -165,18 +166,21 @@ public class GoDaddyDnsDriver extends DnsDriverBase { if (domain == null) return emptyList(); // iterate over all records, return matches - String url = config.getBaseUri()+domain.getName()+"/records"; - if (matcher != null) { - if (matcher.hasType()) { - url += "/" + matcher.getType().name(); - } - if (matcher.hasFqdn()) { - String fqdn = matcher.getFqdn(); - fqdn = domain.dropDomainSuffix(fqdn); - url += "/" + fqdn; + final var url = new StringBuilder(config.getBaseUri()).append(domain.getName()).append("/records"); + if (matcher != null && (matcher.hasType() || matcher.hasFqdn())) { + if (!matcher.hasType() || !matcher.hasPattern()) { + // as per GoDaddy's docs both type and fqdn must be set here + // https://developer.godaddy.com/doc/endpoint/domains#/v1/recordGet + throw invalidEx("err.request.invalid", "Both type and pattern are required"); } + + url.append("/").append(matcher.getType().name()); + + var fqdn = matcher.getPattern(); + fqdn = domain.dropDomainSuffix(fqdn); + url.append("/").append(fqdn); } - return readRecords(domain, url, matcher); + return readRecords(domain, url.toString(), matcher); } public Collection readRecords(BubbleDomain domain, String url, DnsRecordMatch matcher) { diff --git a/bubble-server/src/main/java/bubble/cloud/dns/route53/Route53DnsDriver.java b/bubble-server/src/main/java/bubble/cloud/dns/route53/Route53DnsDriver.java index 2ff760ad..6b989d13 100644 --- a/bubble-server/src/main/java/bubble/cloud/dns/route53/Route53DnsDriver.java +++ b/bubble-server/src/main/java/bubble/cloud/dns/route53/Route53DnsDriver.java @@ -18,10 +18,7 @@ import org.cobbzilla.util.dns.DnsRecord; import org.cobbzilla.util.dns.DnsRecordMatch; import org.cobbzilla.util.dns.DnsType; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import static java.util.Collections.emptyList; @@ -57,15 +54,20 @@ public class Route53DnsDriver extends DnsDriverBase { @Getter(lazy=true) private final Map cachedZoneLookups = new ExpirationMap<>(); private HostedZone getHostedZone(BubbleDomain domain) { return getCachedZoneLookups().computeIfAbsent(domain.getName(), key -> { + final var keyDot = key + "."; + + final Optional found; try { - final ListHostedZonesResult zones = getRoute53client().listHostedZones(new ListHostedZonesRequest().withMaxItems(MAX_ITEMS)); - for (HostedZone z : zones.getHostedZones()) { - if (z.getName().equalsIgnoreCase(key + ".")) return z; - } - return die("HostedZone with name '"+key+".' not found"); + found = getRoute53client().listHostedZones(new ListHostedZonesRequest().withMaxItems(MAX_ITEMS)) + .getHostedZones() + .stream() + .filter(z -> z.getName().equalsIgnoreCase(keyDot)) + .findFirst(); } catch (Exception e) { - return die("getHostedZone: "+e); + return die("getHostedZone: " + e); } + + return found.isPresent() ? found.get() : die("getHostedZone: HostedZone not found with name: " + keyDot); }); }