Add 'DNS Zone Settings' implementation

This commit is contained in:
2025-07-29 11:06:07 +02:00
parent 50b8efbb46
commit e49e975388
11 changed files with 882 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Dns.Internals;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Extensions for <see href="https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/">DNS Zone Settings</see>.
/// </summary>
public static class DnsZoneSettingsExtensions
{
/// <summary>
/// Update DNS settings for a zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<DnsZoneSettings>> UpdateDnsZoneSettings(this ICloudflareClient client, UpdateDnsZoneSettingsRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
if (request.ZoneMode.HasValue && !Enum.IsDefined(typeof(DnsZoneMode), request.ZoneMode))
throw new ArgumentOutOfRangeException(nameof(request.ZoneMode), request.ZoneMode, "Value must be one of the ZoneMode enum values.");
if (request.Nameservers != null && !Enum.IsDefined(typeof(DnsZoneNameserversType), request.Nameservers.Type))
throw new ArgumentOutOfRangeException($"{nameof(request.Nameservers)}.{nameof(request.Nameservers.Type)}", request.Nameservers.Type, "Value must be one of the NameserverType enum values.");
if (request.NameserverTtl.HasValue && (request.NameserverTtl < 30 || 86400 < request.NameserverTtl))
throw new ArgumentOutOfRangeException(nameof(request.NameserverTtl), request.NameserverTtl, "Value must be between 30 and 86400.");
if (request.SOA != null)
{
string paramNameBase = $"{nameof(request.SOA)}";
if (request.SOA.Expire < 86400 || 2419200 < request.SOA.Expire)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Expire)}", request.SOA.Expire, "Value must be between 86400 and 2419200.");
if (request.SOA.MinimumTtl < 60 || 86400 < request.SOA.MinimumTtl)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.MinimumTtl)}", request.SOA.MinimumTtl, "Value must be between 60 and 86400.");
if (string.IsNullOrWhiteSpace(request.SOA.PrimaryNameserver))
throw new ArgumentNullException($"{paramNameBase}.{nameof(request.SOA.PrimaryNameserver)}");
if (request.SOA.Refresh < 600 || 86400 < request.SOA.Refresh)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Refresh)}", request.SOA.Refresh, "Value must be between 600 and 86400.");
if (request.SOA.Retry < 600 || 86400 < request.SOA.Retry)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Retry)}", request.SOA.Retry, "Value must be between 600 and 86400.");
if (request.SOA.TimeToLive < 300 || 86400 < request.SOA.TimeToLive)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.TimeToLive)}", request.SOA.TimeToLive, "Value must be between 300 and 86400.");
if (string.IsNullOrWhiteSpace(request.SOA.ZoneAdministrator))
throw new ArgumentNullException($"{paramNameBase}.{nameof(request.SOA.ZoneAdministrator)}");
}
var req = new InternalUpdateDnsZoneSettingsRequest
{
FlattenAllCnames = request.FlattenAllCnames,
FoundationDns = request.FoundationDns,
InternalDns = request.InternalDns,
MultiProvider = request.MultiProvider,
Nameservers = request.Nameservers,
NameserverTtl = request.NameserverTtl,
SecondaryOverrides = request.SecondaryOverrides,
SOA = request.SOA,
ZoneMode = request.ZoneMode
};
return client.PatchAsync<DnsZoneSettings, InternalUpdateDnsZoneSettingsRequest>($"/zones/{request.ZoneId}/dns_settings", req, cancellationToken);
}
/// <summary>
/// Show DNS settings for a zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
/// <param name="zoneId">The zone identifier.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<DnsZoneSettings>> ShowDnsZoneSettings(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<DnsZoneSettings>($"/zones/{zoneId}/dns_settings", null, cancellationToken);
}
}
}

View File

@@ -0,0 +1,31 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// The mode of a DNS zone.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/dns/settings/zone.ts#L94">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsZoneMode
{
/// <summary>
/// The standard mode.
/// </summary>
[EnumMember(Value = "standard")]
Standard = 1,
/// <summary>
/// The CDN-only mode.
/// </summary>
[EnumMember(Value = "cdn_only")]
CdnOnly = 2,
/// <summary>
/// The DNS-only mode.
/// </summary>
[EnumMember(Value = "dns_only")]
DnsOnly = 3
}
}

View File

@@ -0,0 +1,32 @@
namespace AMWD.Net.Api.Cloudflare.Dns.Internals
{
internal class InternalUpdateDnsZoneSettingsRequest
{
[JsonProperty("flatten_all_cnames")]
public bool? FlattenAllCnames { get; set; }
[JsonProperty("foundation_dns")]
public bool? FoundationDns { get; set; }
[JsonProperty("internal_dns")]
public DnsZoneInternalDns? InternalDns { get; set; }
[JsonProperty("multi_provider")]
public bool? MultiProvider { get; set; }
[JsonProperty("nameservers")]
public DnsZoneNameservers? Nameservers { get; set; }
[JsonProperty("ns_ttl")]
public int? NameserverTtl { get; set; }
[JsonProperty("secondary_overrides")]
public bool? SecondaryOverrides { get; set; }
[JsonProperty("soa")]
public DnsZoneSoa? SOA { get; set; }
[JsonProperty("zone_mode")]
public DnsZoneMode? ZoneMode { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Settings for this internal zone.
/// </summary>
public class DnsZoneInternalDns
{
/// <summary>
/// The ID of the zone to fallback to.
/// </summary>
[JsonProperty("reference_zone_id")]
public string? ReferenceZoneId { get; set; }
}
}

View File

@@ -0,0 +1,64 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
public class DnsZoneNameservers
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsZoneNameservers"/> class.
/// </summary>
/// <param name="type">Nameserver type.</param>
public DnsZoneNameservers(DnsZoneNameserversType type)
{
Type = type;
}
/// <summary>
/// Nameserver type.
/// </summary>
[JsonProperty("type")]
public DnsZoneNameserversType Type { get; set; }
/// <summary>
/// Configured nameserver set to be used for this zone.
/// </summary>
[JsonProperty("ns_set")]
public int? NameserverSet { get; set; }
}
/// <summary>
/// The type of a DNS zone nameserver.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/dns/settings/zone.ts#L115">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsZoneNameserversType
{
/// <summary>
/// The Cloudflare standard nameservers.
/// </summary>
[EnumMember(Value = "cloudflare.standard")]
Standard = 1,
/// <summary>
/// The account specific nameservers.
/// </summary>
[EnumMember(Value = "custom.account")]
Account = 2,
/// <summary>
/// The tenant specific nameservers.
/// </summary>
[EnumMember(Value = "custom.tenant")]
Tenant = 3,
/// <summary>
/// The zone specific nameservers.
/// </summary>
[EnumMember(Value = "custom.zone")]
Zone = 4
}
}

View File

@@ -0,0 +1,66 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// The response for a DNS zone edit.
/// </summary>
public class DnsZoneSettings
{
/// <summary>
/// Whether to flatten all CNAME records in the zone. Note that, due to DNS
/// limitations, a CNAME record at the zone apex will always be flattened.
/// </summary>
[JsonProperty("flatten_all_cnames")]
public bool? FlattenAllCnames { get; set; }
/// <summary>
/// Whether to enable Foundation DNS Advanced Nameservers on the zone.
/// </summary>
[JsonProperty("foundation_dns")]
public bool? FoundationDns { get; set; }
/// <summary>
/// Settings for this internal zone.
/// </summary>
[JsonProperty("internal_dns")]
public DnsZoneInternalDns? InternalDns { get; set; }
/// <summary>
/// Whether to enable multi-provider DNS, which causes Cloudflare to activate the
/// zone even when non-Cloudflare NS records exist, and to respect NS records at the
/// zone apex during outbound zone transfers.
/// </summary>
[JsonProperty("multi_provider")]
public bool? MultiProvider { get; set; }
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
[JsonProperty("nameservers")]
public DnsZoneNameservers? Nameservers { get; set; }
/// <summary>
/// The time to live (TTL) of the zone's nameserver (NS) records.
/// </summary>
[JsonProperty("ns_ttl")]
public int? NameserverTtl { get; set; }
/// <summary>
/// Allows a Secondary DNS zone to use (proxied) override records and CNAME
/// flattening at the zone apex.
/// </summary>
[JsonProperty("secondary_overrides")]
public bool? SecondaryOverrides { get; set; }
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
[JsonProperty("soa")]
public DnsZoneSoa? SOA { get; set; }
/// <summary>
/// Whether the zone mode is a regular or CDN/DNS only zone.
/// </summary>
[JsonProperty("zone_mode")]
public DnsZoneMode? ZoneMode { get; set; }
}
}

View File

@@ -0,0 +1,75 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
public class DnsZoneSoa
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsZoneSoa"/> class.
/// </summary>
/// <param name="expire">Time in seconds of being unable to query the primary server after which secondary servers should stop serving the zone.</param>
/// <param name="minttl">The time to live (TTL) for negative caching of records within the zone.</param>
/// <param name="mname">The primary nameserver for the zone.</param>
/// <param name="refresh">Time in seconds after which secondary servers should re-check the SOA record to see if the zone has been updated.</param>
/// <param name="retry">The time to live (TTL) for negative caching of records within the zone.</param>
/// <param name="rname">The email address of the zone administrator.</param>
/// <param name="ttl">The time to live (TTL) of the SOA record itself.</param>
public DnsZoneSoa(int expire, int minttl, string mname, int refresh, int retry, string rname, int ttl)
{
Expire = expire;
MinimumTtl = minttl;
PrimaryNameserver = mname;
Refresh = refresh;
Retry = retry;
ZoneAdministrator = rname;
TimeToLive = ttl;
}
/// <summary>
/// Time in seconds of being unable to query the primary server after which
/// secondary servers should stop serving the zone.
/// </summary>
[JsonProperty("expire")]
public int Expire { get; set; }
/// <summary>
/// The time to live (TTL) for negative caching of records within the zone.
/// </summary>
[JsonProperty("min_ttl")]
public int MinimumTtl { get; set; }
/// <summary>
/// The primary nameserver, which may be used for outbound zone transfers.
/// </summary>
[JsonProperty("mname")]
public string PrimaryNameserver { get; set; }
/// <summary>
/// Time in seconds after which secondary servers should re-check the SOA record to
/// see if the zone has been updated.
/// </summary>
[JsonProperty("refresh")]
public int Refresh { get; set; }
/// <summary>
/// Time in seconds after which secondary servers should retry queries after the
/// primary server was unresponsive.
/// </summary>
[JsonProperty("retry")]
public int Retry { get; set; }
/// <summary>
/// The email address of the zone administrator, with the first label representing
/// the local part of the email address.
/// </summary>
[JsonProperty("rname")]
public string ZoneAdministrator { get; set; }
/// <summary>
/// The time to live (TTL) of the SOA record itself.
/// </summary>
[JsonProperty("ttl")]
public int TimeToLive { get; set; }
}
}

View File

@@ -27,6 +27,14 @@ This package contains the feature set of the _DNS_ section of the Cloudflare API
- [Overwrite DNS Record](https://developers.cloudflare.com/api/resources/dns/subresources/records/methods/update/)
#### [Settings]
##### [Zone]
- [Update DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/methods/edit/)
- [Show DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/methods/get/)
---
Published under MIT License (see [choose a license])
@@ -39,3 +47,6 @@ Published under MIT License (see [choose a license])
[DNS]: https://developers.cloudflare.com/api/resources/dns/
[Records]: https://developers.cloudflare.com/api/resources/dns/subresources/records/
[Settings]: https://developers.cloudflare.com/api/resources/dns/subresources/settings/
[Zone]: https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/

View File

@@ -0,0 +1,71 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Represents a request to update DNS zone settings.
/// </summary>
public class UpdateDnsZoneSettingsRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateDnsZoneSettingsRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public UpdateDnsZoneSettingsRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// Whether to flatten all CNAME records in the zone. Note that, due to
/// DNS limitations, a CNAME record at the zone apex will always be flattened.
/// </summary>
public bool? FlattenAllCnames { get; set; }
/// <summary>
/// Whether to enable Foundation DNS Advanced Nameservers on the zone.
/// </summary>
public bool? FoundationDns { get; set; }
/// <summary>
/// Settings for this internal zone.
/// </summary>
public DnsZoneInternalDns? InternalDns { get; set; }
/// <summary>
/// Whether to enable multi-provider DNS, which causes Cloudflare to
/// activate the zone even when non-Cloudflare NS records exist, and to respect NS
/// records at the zone apex during outbound zone transfers.
/// </summary>
public bool? MultiProvider { get; set; }
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
public DnsZoneNameservers? Nameservers { get; set; }
/// <summary>
/// The time to live (TTL) of the zone's nameserver (NS) records.
/// </summary>
public int? NameserverTtl { get; set; }
/// <summary>
/// Allows a Secondary DNS zone to use (proxied) override records and CNAME
/// flattening at the zone apex.
/// </summary>
public bool? SecondaryOverrides { get; set; }
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
public DnsZoneSoa? SOA { get; set; }
/// <summary>
/// Whether the zone mode is a regular or CDN/DNS only zone.
/// </summary>
public DnsZoneMode? ZoneMode { get; set; }
}
}