Added some Zone features

This commit is contained in:
2024-10-24 17:12:14 +02:00
parent 83620cb450
commit c6eb6ba05e
49 changed files with 3595 additions and 34 deletions

View File

@@ -0,0 +1,36 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A zone status.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ZoneStatus
{
/// <summary>
/// Zone is initializing.
/// </summary>
[EnumMember(Value = "initializing")]
Initializing = 1,
/// <summary>
/// Zone is pending.
/// </summary>
[EnumMember(Value = "pending")]
Pending = 2,
/// <summary>
/// Zone is active.
/// </summary>
[EnumMember(Value = "active")]
Active = 3,
/// <summary>
/// Zone has been moved.
/// </summary>
[EnumMember(Value = "moved")]
Moved = 4,
}
}

View File

@@ -0,0 +1,46 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Zone type.
/// </summary>
/// <remarks>
/// A full zone implies that DNS is hosted with Cloudflare.
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
/// </remarks>
[JsonConverter(typeof(StringEnumConverter))]
public enum ZoneType
{
/// <summary>
/// Full Setup (most common).
/// </summary>
/// <remarks>
/// Use Cloudflare as your primary DNS provider and manage your DNS records on Cloudflare.
/// </remarks>
[EnumMember(Value = "full")]
Full = 1,
/// <summary>
/// Zone transfers.
/// </summary>
/// <remarks>
/// Use Cloudflare and another DNS provider together across your entire zone to increase availability and fault tolerance.
/// <br />
/// DNS records will be transferred between providers using
/// <see href="https://datatracker.ietf.org/doc/html/rfc5936">AXFR</see> or <see href="https://datatracker.ietf.org/doc/html/rfc1995">IXFR</see>.
/// </remarks>
[EnumMember(Value = "secondary")]
Secondary = 2,
/// <summary>
/// Partial (CNAME) setup.
/// </summary>
/// <remarks>
/// Keep your primary DNS provider and only use Cloudflare's reverse proxy for individual subdomains.
/// </remarks>
[EnumMember(Value = "partial")]
Partial = 3,
}
}

View File

@@ -0,0 +1,36 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Field to order zones by.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ZonesOrderBy
{
/// <summary>
/// Order by zone name.
/// </summary>
[EnumMember(Value = "name")]
Name = 1,
/// <summary>
/// Order by zone status.
/// </summary>
[EnumMember(Value = "status")]
Status = 2,
/// <summary>
/// Order by account ID.
/// </summary>
[EnumMember(Value = "account.id")]
AccountId = 3,
/// <summary>
/// Order by account name.
/// </summary>
[EnumMember(Value = "account.name")]
AccountName = 4,
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Filter for listing zones.
/// </summary>
public class ListZonesFilter : IQueryParameterFilter
{
/// <summary>
/// An account ID.
/// </summary>
/// <value>account.id</value>
public string AccountId { get; set; }
/// <summary>
/// An account Name.
/// </summary>
/// <remarks>
/// Optional filter operators can be provided to extend refine the search:
/// <list type="bullet">
/// <item><description>equal</description> (default)</item>
/// <item><description>not_equal</description></item>
/// <item><description>starts_with</description></item>
/// <item><description>ends_with</description></item>
/// <item><description>contains</description></item>
/// <item><description>starts_with_case_sensitive</description></item>
/// <item><description>ends_with_case_sensitive</description></item>
/// <item><description>contains_case_sensitive</description></item>
/// </list>
/// </remarks>
/// <example>Dev Account</example>
/// <example>contains:Test</example>
/// <value>account.name</value>
public string AccountName { get; set; }
/// <summary>
/// Direction to order zones.
/// </summary>
/// <value>direction</value>
public SortDirection? OrderDirection { get; set; }
/// <summary>
/// Whether to match all search requirements or at least one (any).
/// </summary>
/// <value>match</value>
public FilterMatchType? MatchType { get; set; }
/// <summary>
/// A domain name.
/// </summary>
/// <remarks>
/// Optional filter operators can be provided to extend refine the search:
/// <list type="bullet">
/// <item><description>equal</description> (default)</item>
/// <item><description>not_equal</description></item>
/// <item><description>starts_with</description></item>
/// <item><description>ends_with</description></item>
/// <item><description>contains</description></item>
/// <item><description>starts_with_case_sensitive</description></item>
/// <item><description>ends_with_case_sensitive</description></item>
/// <item><description>contains_case_sensitive</description></item>
/// </list>
/// </remarks>
/// <example>example.com</example>
/// <example>contains:.org</example>
/// <example>ends_with:arpa</example>
/// <example>starts_with:dev</example>
/// <value>name</value>
public string Name { get; set; }
/// <summary>
/// Field to order zones by.
/// </summary>
/// <value>order</value>
public ZonesOrderBy? OrderBy { get; set; }
/// <summary>
/// Page number of paginated results.
/// </summary>
/// <value>page</value>
public int? Page { get; set; }
/// <summary>
/// Number of zones per page.
/// </summary>
/// <value>per_page</value>
public int? PerPage { get; set; }
/// <summary>
/// A zone status.
/// </summary>
/// <value>status</value>
public ZoneStatus? Status { get; set; }
/// <inheritdoc />
public IDictionary<string, string> GetQueryParameters()
{
var dict = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(AccountId))
dict.Add("account.id", AccountId);
if (!string.IsNullOrWhiteSpace(AccountName))
dict.Add("account.name", AccountName);
if (OrderDirection.HasValue && Enum.IsDefined(typeof(SortDirection), OrderDirection.Value))
dict.Add("direction", OrderDirection.Value.GetEnumMemberValue());
if (MatchType.HasValue && Enum.IsDefined(typeof(FilterMatchType), MatchType.Value))
dict.Add("match", MatchType.Value.GetEnumMemberValue());
if (!string.IsNullOrWhiteSpace(Name))
dict.Add("name", Name);
if (OrderBy.HasValue && Enum.IsDefined(typeof(ZonesOrderBy), OrderBy.Value))
dict.Add("order", OrderBy.Value.GetEnumMemberValue());
if (Page.HasValue && Page.Value >= 1)
dict.Add("page", Page.Value.ToString());
if (PerPage.HasValue && PerPage.Value >= 5 && PerPage.Value <= 50)
dict.Add("per_page", PerPage.Value.ToString());
if (Status.HasValue && Enum.IsDefined(typeof(ZoneStatus), Status.Value))
dict.Add("status", Status.Value.GetEnumMemberValue());
return dict;
}
}
}

View File

@@ -0,0 +1,14 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests
{
internal class CreateRequest
{
[JsonProperty("account")]
public AccountBase Account { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public ZoneType Type { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests
{
internal class EditRequest
{
[JsonProperty("type")]
public ZoneType? Type { get; set; }
[JsonProperty("vanity_name_servers")]
public IList<string> VanityNameServers { get; set; }
}
}

View File

@@ -0,0 +1,27 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Request to create a new zone.
/// </summary>
public class CreateZoneRequest
{
/// <summary>
/// The account identifier.
/// </summary>
public string AccountId { get; set; }
/// <summary>
/// The domain name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The zone type.
/// </summary>
/// <remarks>
/// A full zone implies that DNS is hosted with Cloudflare.
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
/// </remarks>
public ZoneType Type { get; set; }
}
}

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A request to edit a zone.
/// </summary>
public class EditZoneRequest
{
/// <summary>
/// Identifier.
/// </summary>
public string Id { get; set; }
/// <summary>
/// A full zone implies that DNS is hosted with Cloudflare. A partial zone is typically a partner-hosted zone or a CNAME setup.
/// <br/>
/// <em>This parameter is only available to Enterprise customers or if it has been explicitly enabled on a zone.</em>
/// </summary>
public ZoneType? Type { get; set; }
/// <summary>
/// An array of domains used for custom name servers.
/// <br/>
/// <em>This is only available for Business and Enterprise plans.</em>
/// </summary>
public IList<string> VanityNameServers { get; set; }
}
}

View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
/// </summary>
public static class ZoneExtensions
{
/// <summary>
/// Lists, searches, sorts, and filters your zones.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="options">Filter options (optional).</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<IReadOnlyList<Zone>>> ListZones(this ICloudflareClient client, ListZonesFilter options = null, CancellationToken cancellationToken = default)
{
return client.GetAsync<IReadOnlyList<Zone>>("zones", options, cancellationToken);
}
/// <summary>
/// Get details for a zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<Zone>> ZoneDetails(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<Zone>($"zones/{zoneId}", cancellationToken: cancellationToken);
}
/// <summary>
/// Create a new zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="request">The request information.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<Zone>> CreateZone(this ICloudflareClient client, CreateZoneRequest request, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
request.AccountId.ValidateCloudflareId();
request.Name.ValidateCloudflareName();
if (!RegexPatterns.ZoneName.IsMatch(request.Name))
throw new ArgumentException("Does not match the zone name pattern", nameof(request.Name));
if (!Enum.IsDefined(typeof(ZoneType), request.Type))
throw new ArgumentOutOfRangeException(nameof(request.Type));
var req = new CreateRequest
{
Account = new AccountBase { Id = request.AccountId },
Name = request.Name,
Type = request.Type
};
return client.PostAsync<Zone, CreateRequest>("zones", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Deletes an existing zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> DeleteZone(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.DeleteAsync<ZoneIdResponse>($"zones/{zoneId}", cancellationToken: cancellationToken);
}
/// <summary>
/// Edits a zone.
/// </summary>
/// <remarks>
/// Only one zone property can be changed at a time.
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="request">The request information.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<Zone>> EditZone(this ICloudflareClient client, EditZoneRequest request, CancellationToken cancellationToken = default)
{
request.Id.ValidateCloudflareId();
if (request.Type.HasValue && request.VanityNameServers != null)
throw new CloudflareException("Only one zone property can be changed at a time.");
if (request.Type.HasValue && !Enum.IsDefined(typeof(ZoneType), request.Type.Value))
throw new ArgumentOutOfRangeException(nameof(request.Type));
var req = new EditRequest();
if (request.Type.HasValue)
req.Type = request.Type.Value;
if (request.VanityNameServers != null)
req.VanityNameServers = request.VanityNameServers.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
return client.PatchAsync<Zone, EditRequest>($"zones/{request.Id}", req, cancellationToken);
}
// Triggeres a new activation check for a PENDING Zone. This can be triggered every 5 min for paygo/ent customers, every hour for FREE Zones.
/// <summary>
/// Triggeres a new activation check for a <see cref="ZoneStatus.Pending"/> zone.
/// </summary>
/// <remarks>
/// This can be triggered every 5 min for paygo/enterprise customers, every hour for FREE Zones.
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> RerunActivationCheck(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.PutAsync<ZoneIdResponse, object>($"zones/{zoneId}/activation_check", null, cancellationToken);
}
}
}