Skip to content

Commit

Permalink
more configuration work, add connect/request timeouts to providers
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Sep 21, 2024
1 parent 0996bb3 commit e5d1148
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package eu.cloudnetservice.modules.dns.config;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

Expand Down Expand Up @@ -74,6 +78,36 @@ private DnsModuleConfigEntry(
this.providerConfig = providerConfig;
}

/**
* Constructs a new builder instance for dns module configuration entries.
*
* @return a new builder instance for dns module configuration entries.
*/
@Contract(value = " -> new", pure = true)
public static @NonNull Builder builder() {
return new Builder();
}

/**
* Constructs a new builder instance for dns module configuration entries, copying the settings from the given
* configuration entry into the builder.
*
* @param configEntry the config entry of which the settings should be copied into the builder.
* @return the newly created builder instance with all values set to the values from the given config entry.
* @throws NullPointerException if the config entry is null.
*/
@Contract("_ -> new")
public static @NonNull Builder builder(@NonNull DnsModuleConfigEntry configEntry) {
return builder()
.enabled(configEntry.enabled())
.domain(configEntry.domain())
.domainNamespace(configEntry.domainNamespace())
.hostAddressV4(configEntry.hostAddressV4())
.hostAddressV6(configEntry.hostAddressV6())
.groups(configEntry.groups())
.providerConfig(configEntry.providerConfig());
}

/**
* Get if this configuration entry is enabled and should be used.
*
Expand Down Expand Up @@ -142,4 +176,155 @@ public boolean enabled() {
public @NonNull DnsModuleProviderConfig providerConfig() {
return this.providerConfig;
}

/**
* A builder for dns module configuration entries.
*
* @since 4.0
*/
public static final class Builder {

private boolean enabled = true;

private String domain;
private String domainNamespace;

private String hostAddressV4;
private String hostAddressV6;

private List<DnsModuleGroupEntry> groups = new ArrayList<>();
private DnsModuleProviderConfig providerConfig;

/**
* Sealed constructor to prevent direct instantiation. Use {@link DnsModuleConfigEntry#builder()} instead.
*/
private Builder() {
}

/**
* Sets whether the dns module entry is enabled or not.
*
* @param enabled whether the DNS module should be enabled.
* @return this builder, for chaining.
*/
@Contract("_ -> this")
public @NonNull Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}

/**
* Sets the domain for the dns module configuration entry.
*
* @param domain the domain for the DNS configuration.
* @return this builder, for chaining.
* @throws NullPointerException if the given domain is null.
*/
@Contract("_ -> this")
public @NonNull Builder domain(@NonNull String domain) {
this.domain = domain;
return this;
}

/**
* Sets the domain namespace for target records created for SRV records. Can be null to apply no namespace.
*
* @param domainNamespace the namespace of the domain.
* @return this builder, for chaining.
*/
@Contract(value = "_ -> this", pure = true)
public @NonNull Builder domainNamespace(@Nullable String domainNamespace) {
this.domainNamespace = domainNamespace;
return this;
}

/**
* Sets the host address (ipv4) to use for SRV target records (A-records). If null the bound address of the proxy is
* used instead.
*
* @param hostAddressV4 the host address (ipv4) that will be used as target for created records.
* @return this builder, for chaining.
*/
@Contract(value = "_ -> this", pure = true)
public @NonNull Builder hostAddressV4(@Nullable String hostAddressV4) {
this.hostAddressV4 = hostAddressV4;
return this;
}

/**
* Sets the host address (ipv6) to use for SRV target records (AAAA-records). If null the bound address of the proxy
* is used instead.
*
* @param hostAddressV6 the host address (ipv6) that will be used as target for created records.
* @return this builder, for chaining.
*/
@Contract(value = "_ -> this", pure = true)
public @NonNull Builder hostAddressV6(@Nullable String hostAddressV6) {
this.hostAddressV6 = hostAddressV6;
return this;
}

/**
* Sets the group entries for the dns module configuration entry.
*
* @param groups the group entries to set.
* @return this builder, for chaining.
* @throws NullPointerException if the given groups list is null.
*/
@Contract("_ -> this")
public @NonNull Builder groups(@NonNull List<DnsModuleGroupEntry> groups) {
this.groups = new ArrayList<>(groups);
return this;
}

/**
* Modifies the group entries for the dns module configuration entry.
*
* @param modifier the modifier action to edit the list of groups.
* @return this builder, for chaining.
* @throws NullPointerException if the given modifier action is null.
*/
@Contract("_ -> this")
public @NonNull Builder modifyGroups(@NonNull Consumer<List<DnsModuleGroupEntry>> modifier) {
modifier.accept(this.groups);
return this;
}

/**
* Sets the provider configuration for the DNS module.
*
* @param providerConfig the provider configuration.
* @return this builder, for chaining.
* @throws NullPointerException if the given provider configuration is null.
*/
@Contract("_ -> this")
public @NonNull Builder providerConfig(@NonNull DnsModuleProviderConfig providerConfig) {
this.providerConfig = providerConfig;
return this;
}

/**
* Constructs a new DNS module configuration entry from this builder. The builder can be re-used after this
* operation as further changes will not reflect into the constructed config entry.
*
* @return the newly created DNS module configuration entry.
* @throws NullPointerException if no domain, domain namespace or provider config was provided.
*/
@Contract(" -> new")
public @NonNull DnsModuleConfigEntry build() {
Preconditions.checkNotNull(this.domain, "domain not provided");
Preconditions.checkNotNull(this.domainNamespace, "domain namespace not provided");
Preconditions.checkNotNull(this.providerConfig, "provider configuration not provided");

return new DnsModuleConfigEntry(
this.enabled,
this.domain,
this.domainNamespace,
this.hostAddressV4,
this.hostAddressV6,
List.copyOf(this.groups),
this.providerConfig
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ private Builder() {
*
* @param hostAddressV4 the host address (ipv4) that will be used as target for created records.
* @return this builder, for chaining.
* @throws IllegalArgumentException if the given host address is invalid or not an ipv4 address.
*/
@Contract("_ -> this")
public @NonNull Builder hostAddressV4(@Nullable String hostAddressV4) {
Expand All @@ -188,7 +187,6 @@ private Builder() {
*
* @param hostAddressV6 the host address (ipv6) that will be used as target for created records.
* @return this builder, for chaining.
* @throws IllegalArgumentException if the given host address is invalid or not an ipv6 address.
*/
@Contract("_ -> this")
public @NonNull Builder hostAddressV6(@Nullable String hostAddressV6) {
Expand Down Expand Up @@ -224,7 +222,7 @@ private Builder() {

/**
* Constructs a new dns group configuration entry from this builder. The builder can be re-used after this operation
* as further changes will not reflect into the builder.
* as further changes will not reflect into the constructed group entry.
*
* @return the newly created dns group configuration based on this builder.
* @throws NullPointerException if not target group was specified.
Expand Down
Loading

0 comments on commit e5d1148

Please sign in to comment.