Add dagger map for injecting DnsWriter implementations

This is one of several CLs in a sequence for allowing per-TLD DNS
implementations.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129445641
This commit is contained in:
Greg Shikhman 2016-08-05 08:45:02 -07:00 committed by Justine Tunney
parent 770fd35e20
commit a620d06239
24 changed files with 173 additions and 215 deletions

View file

@ -36,6 +36,7 @@ java_library(
"//third_party/java/objectify:objectify-v4_1",
"//third_party/java/servlet/servlet_api",
"//java/google/registry/config",
"//java/google/registry/dns/writer",
"//java/google/registry/model",
"//java/google/registry/request",
"//java/google/registry/util",

View file

@ -30,7 +30,7 @@ import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import google.registry.dns.DnsConstants.TargetType;
import google.registry.model.dns.DnsWriterZone;
import google.registry.dns.writer.DnsWriterZone;
import google.registry.request.Parameter;
import google.registry.request.RequestParameters;
import java.util.Set;

View file

@ -0,0 +1,42 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.dns;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.collect.ImmutableMap;
import google.registry.dns.writer.DnsWriter;
import google.registry.model.registry.Registry;
import java.util.Map;
import javax.inject.Inject;
/** Proxy for retrieving {@link DnsWriter} implementations. */
public final class DnsWriterProxy {
private final ImmutableMap<String, DnsWriter> dnsWriters;
@Inject
DnsWriterProxy(Map<String, DnsWriter> dnsWriters) {
this.dnsWriters = ImmutableMap.copyOf(dnsWriters);
}
/** Return the {@link DnsWriter} for the given tld. */
public DnsWriter getForTld(String tld) {
String clazz = Registry.get(tld).getDnsWriter();
DnsWriter dnsWriter = dnsWriters.get(clazz);
checkState(dnsWriter != null, "Could not load DnsWriter %s for TLD %s", clazz, tld);
return dnsWriter;
}
}

View file

@ -20,7 +20,7 @@ import static google.registry.util.CollectionUtils.nullToEmpty;
import com.google.common.net.InternetDomainName;
import google.registry.config.ConfigModule.Config;
import google.registry.model.dns.DnsWriter;
import google.registry.dns.writer.DnsWriter;
import google.registry.request.Action;
import google.registry.request.HttpException.ServiceUnavailableException;
import google.registry.request.Parameter;
@ -30,7 +30,6 @@ import google.registry.util.FormattingLogger;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import javax.inject.Provider;
import org.joda.time.Duration;
/** Task that sends domain and host updates to the DNS server. */
@ -44,7 +43,7 @@ public final class PublishDnsUpdatesAction implements Runnable, Callable<Void> {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@Inject DnsQueue dnsQueue;
@Inject Provider<DnsWriter> writerProvider;
@Inject DnsWriterProxy dnsWriterProxy;
@Inject @Config("dnsWriteLockTimeout") Duration timeout;
@Inject @Parameter(RequestParameters.PARAM_TLD) String tld;
@Inject @Parameter(DOMAINS_PARAM) Set<String> domains;
@ -73,7 +72,7 @@ public final class PublishDnsUpdatesAction implements Runnable, Callable<Void> {
/** Steps through the domain and host refreshes contained in the parameters and processes them. */
private void processBatch() {
try (DnsWriter writer = writerProvider.get()) {
try (DnsWriter writer = dnsWriterProxy.getForTld(tld)) {
for (String domain : nullToEmpty(domains)) {
if (!DomainNameUtils.isUnder(
InternetDomainName.from(domain), InternetDomainName.from(tld))) {

View file

@ -1,18 +0,0 @@
package(
default_visibility = ["//java/google/registry:registry_project"],
)
licenses(["notice"]) # Apache 2.0
java_library(
name = "writer",
srcs = glob(["*.java"]),
deps = [
"//java/com/google/common/base",
"//third_party/java/dagger",
"//third_party/java/jsr305_annotations",
"//third_party/java/jsr330_inject",
"//java/google/registry/model",
],
)

View file

@ -1,49 +0,0 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.dns.writer;
import com.google.common.base.Joiner;
import google.registry.model.dns.DnsWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
/**
* {@link DnsWriter} that doesn't actually update records in a DNS server.
*
* <p>All this class does is write its displeasure to the logs.
*/
public final class VoidDnsWriter implements DnsWriter {
private static final Logger logger = Logger.getLogger(VoidDnsWriter.class.getName());
private final Set<String> names = new HashSet<>();
@Override
public void publishDomain(String domainName) {
names.add(domainName);
}
@Override
public void publishHost(String hostName) {
names.add(hostName);
}
@Override
public void close() {
logger.warning("Ignoring DNS zone updates! No DnsWriterFactory implementation specified!\n"
+ Joiner.on('\n').join(names));
}
}

View file

@ -1,29 +0,0 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.dns.writer;
import dagger.Module;
import dagger.Provides;
import google.registry.model.dns.DnsWriter;
/** Dagger module that disables DNS updates. */
@Module
public final class VoidDnsWriterModule {
@Provides
static DnsWriter provideDnsWriter() {
return new VoidDnsWriter();
}
}

View file

@ -21,6 +21,7 @@ java_library(
"//third_party/java/dagger",
"//third_party/java/joda_time",
"//java/google/registry/config",
"//java/google/registry/dns/writer",
"//java/google/registry/model",
"//java/google/registry/util",
],

View file

@ -22,7 +22,10 @@ import com.google.api.services.dns.DnsScopes;
import com.google.common.base.Function;
import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import google.registry.config.ConfigModule.Config;
import google.registry.dns.writer.DnsWriter;
import java.util.Set;
/** Dagger module for Google Cloud DNS service connection objects. */
@ -39,4 +42,11 @@ public final class CloudDnsModule {
.setApplicationName(projectId)
.build();
}
@Provides
@IntoMap
@StringKey(CloudDnsWriter.NAME)
static DnsWriter provideCloudDnsWriter(CloudDnsWriter writer) {
return writer;
}
}

View file

@ -32,8 +32,8 @@ import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.net.InternetDomainName;
import com.google.common.util.concurrent.RateLimiter;
import google.registry.config.ConfigModule.Config;
import google.registry.model.dns.DnsWriter;
import google.registry.model.dns.DnsWriterZone;
import google.registry.dns.writer.DnsWriter;
import google.registry.dns.writer.DnsWriterZone;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.host.HostResource;
@ -59,6 +59,12 @@ import org.joda.time.Duration;
*/
class CloudDnsWriter implements DnsWriter {
/**
* The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change
* the value on affected Registry objects to prevent runtime failures.
*/
public static final String NAME = "CloudDnsWriter";
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
// This is the default max QPS for Cloud DNS. It can be increased by contacting the team

View file

@ -21,6 +21,7 @@ java_library(
"//third_party/java/jsr305_annotations",
"//third_party/java/jsr330_inject",
"//java/google/registry/config",
"//java/google/registry/dns/writer",
"//java/google/registry/model",
"//java/google/registry/util",
],

View file

@ -25,7 +25,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InternetDomainName;
import google.registry.config.ConfigModule.Config;
import google.registry.model.dns.DnsWriter;
import google.registry.dns.writer.DnsWriter;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.host.HostResource;
@ -75,6 +75,12 @@ import org.xbill.DNS.Update;
*/
public class DnsUpdateWriter implements DnsWriter {
/**
* The name of the pricing engine, as used in {@code Registry.dnsWriter}. Remember to change
* the value on affected Registry objects to prevent runtime failures.
*/
public static final String NAME = "DnsUpdateWriter";
private final Duration dnsTimeToLive;
private final DnsMessageTransport transport;
private final Clock clock;

View file

@ -14,21 +14,26 @@
package google.registry.dns.writer.dnsupdate;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import google.registry.model.dns.DnsWriter;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import google.registry.dns.writer.DnsWriter;
import javax.net.SocketFactory;
/** Dagger module that provides a DnsUpdateWriter. */
@Module
public abstract class DnsUpdateWriterModule {
@Binds
abstract DnsWriter provideDnsWriter(DnsUpdateWriter dnsWriter);
@Provides
static SocketFactory provideSocketFactory() {
return SocketFactory.getDefault();
}
@Provides
@IntoMap
@StringKey(DnsUpdateWriter.NAME)
static DnsWriter provideDnsUpdateWriter(DnsUpdateWriter writer) {
return writer;
}
}