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,25 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests
{
internal class PurgeRequest
{
[JsonProperty("purge_everything")]
public bool? PurgeEverything { get; set; }
[JsonProperty("tags")]
public IList<string> Tags { get; set; }
[JsonProperty("hosts")]
public IList<string> Hostnames { get; set; }
[JsonProperty("prefixes")]
public IList<string> Prefixes { get; set; }
[JsonProperty("files")]
public IList<string> Urls { get; set; }
[JsonProperty("files")]
public IList<UrlWithHeaders> UrlsWithHeaders { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests
{
internal class UrlWithHeaders
{
[JsonProperty("headers")]
public Dictionary<string, string> Headers { get; set; } = [];
[JsonProperty("url")]
public string Url { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Url with headers to purge.
/// </summary>
public class ZonePurgeCachedUrlRequest
{
/// <summary>
/// Defined headers to specifiy the purge request.
/// </summary>
public Dictionary<string, string> Headers { get; set; } = [];
/// <summary>
/// The file url to purge.
/// </summary>
public string Url { get; set; }
}
}

View File

@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
/// </summary>
public static class ZoneCacheExtensions
{
/// <summary>
/// Purges all cached contents for a zone.
/// </summary>
/// <remarks>
/// Removes ALL files from Cloudflare's cache. All tiers can purge everything.
/// </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>> PurgeCachedContent(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
var req = new PurgeRequest
{
PurgeEverything = true
};
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Purges cached contents by URLs.
/// <br />
/// Granularly removes one or more files from Cloudflare's cache by specifying URLs.
/// All tiers can purge by URL.
/// </summary>
/// <remarks>
/// <para>
/// To purge files with custom cache keys, include the headers used to compute the cache key as in the example.
/// If you have a device type or geo in your cache key, you will need to include the CF-Device-Type or CF-IPCountry headers.
/// If you have lang in your cache key, you will need to include the Accept-Language header.
/// </para>
/// <para>
/// <strong>NB</strong>: When including the Origin header, be sure to include the <strong>scheme</strong> and <strong>hostname</strong>.
/// The port number can be omitted if it is the default port (80 for http, 443 for https), but must be included otherwise.
/// </para>
/// <para>
/// <strong>NB</strong>: For Zones on Free/Pro/Business plan, you may purge up to 30 URLs in one API call.
/// For Zones on Enterprise plan, you may purge up to 500 URLs in one API call.
/// </para>
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="urls">List of URLs to purge.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByUrl(this ICloudflareClient client, string zoneId, IReadOnlyList<ZonePurgeCachedUrlRequest> urls, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
if (urls == null)
throw new ArgumentNullException(nameof(urls));
var req = new PurgeRequest();
if (urls.Any(u => u.Headers.Count > 0))
{
req.UrlsWithHeaders = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url))
.Select(u => new UrlWithHeaders
{
Url = u.Url,
Headers = u.Headers
.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
}).ToList();
}
else
{
req.Urls = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url)).Select(u => u.Url).ToList();
}
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Purges cached contents by cache-tags.
/// </summary>
/// <remarks>
/// <para>
/// For more information on cache tags and purging by tags, please refer to
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#purge-cache-by-cache-tags-enterprise-only">purge by cache-tags documentation page</see>.
/// </para>
/// <para>
/// Cache-Tag purging has a rate limit of 30_000 purge API calls in every 24 hour period.
/// You may purge up to 30 tags in one API call.
/// </para>
/// <para>
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
/// </para>
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="tags">List of tags to purge.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByTag(this ICloudflareClient client, string zoneId, IReadOnlyList<string> tags, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
if (tags == null)
throw new ArgumentNullException(nameof(tags));
var req = new PurgeRequest
{
Tags = tags.Where(t => !string.IsNullOrWhiteSpace(t)).ToList()
};
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Purges cached contents by hosts.
/// </summary>
/// <remarks>
/// <para>
/// For more information purging by hostnames, please refer to
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-hostname/">purge by hostname documentation page</see>.
/// </para>
/// <para>
/// Host purging has a rate limit of 30_000 purge API calls in every 24 hour period.
/// You may purge up to 30 hosts in one API call.
/// </para>
/// <para>
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
/// </para>
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="hosts">List of hostnames to purge.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByHost(this ICloudflareClient client, string zoneId, IReadOnlyList<string> hosts, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
if (hosts == null)
throw new ArgumentNullException(nameof(hosts));
var req = new PurgeRequest
{
Hostnames = hosts.Where(h => !string.IsNullOrWhiteSpace(h)).ToList()
};
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Purges cached contents by prefixes.
/// </summary>
/// <remarks>
/// <para>
/// For more information purging by prefixes, please refer to
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge_by_prefix/">purge by prefix documentation page</see>.
/// </para>
/// <para>
/// Prefix purging has a rate limit of 30_000 purge API calls in every 24 hour period.
/// You may purge up to 30 prefixes in one API call.
/// </para>
/// <para>
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
/// </para>
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="prefixes">List of prefixes to purge.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByPrefix(this ICloudflareClient client, string zoneId, IReadOnlyList<string> prefixes, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
if (prefixes == null)
throw new ArgumentNullException(nameof(prefixes));
var req = new PurgeRequest
{
Prefixes = prefixes.Where(h => !string.IsNullOrWhiteSpace(h)).ToList()
};
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
}
}
}

View File

@@ -17,4 +17,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Cloudflare.Zones.Tests" PublicKey="$(PublicKey)" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
internal class CreateZoneHoldFilter : IQueryParameterFilter
{
public bool IncludeSubdomains { get; set; }
public IDictionary<string, string> GetQueryParameters()
{
var dict = new Dictionary<string, string>();
if (IncludeSubdomains)
dict.Add("include_subdomains", "true");
return dict;
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
internal class DeleteZoneHoldFilter : IQueryParameterFilter
{
public DateTime? HoldAfter { get; set; }
public IDictionary<string, string> GetQueryParameters()
{
var dict = new Dictionary<string, string>();
if (HoldAfter.HasValue)
dict.Add("hold_after", HoldAfter.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
return dict;
}
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
/// </summary>
public static class ZoneHoldExtensions
{
/// <summary>
/// Retrieve whether the zone is subject to a zone hold, and metadata about the hold.
/// </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<ZoneHold>> GetZoneHold(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<ZoneHold>($"zones/{zoneId}/hold", cancellationToken: cancellationToken);
}
/// <summary>
/// Enforce a zone hold on the zone, blocking the creation and activation of zones with this zone's hostname.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="includeSubdomains">
/// If set, the zone hold will extend to block any subdomain of the given zone, as well as SSL4SaaS Custom Hostnames.
/// For example, a zone hold on a zone with the hostname 'example.com' and <paramref name="includeSubdomains"/>=<see langword="true"/> will block 'example.com', 'staging.example.com', 'api.staging.example.com', etc.
/// </param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneHold>> CreateZoneHold(this ICloudflareClient client, string zoneId, bool includeSubdomains = false, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
var filter = new CreateZoneHoldFilter
{
IncludeSubdomains = includeSubdomains
};
return client.PostAsync<ZoneHold, object>($"zones/{zoneId}/hold", null, filter, cancellationToken);
}
/// <summary>
/// Stop enforcement of a zone hold on the zone, permanently or temporarily, allowing the creation and activation of zones with this zone's hostname.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
/// <param name="zoneId">The zone ID.</param>
/// <param name="holdAfter">
/// If <paramref name="holdAfter"/> is provided, the hold will be temporarily disabled, then automatically re-enabled by the system at the time specified.
/// Otherwise, the hold will be disabled indefinitely.
/// </param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<ZoneHold>> DeleteZoneHold(this ICloudflareClient client, string zoneId, DateTime? holdAfter = null, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
var filter = new DeleteZoneHoldFilter
{
HoldAfter = holdAfter
};
return client.DeleteAsync<ZoneHold>($"zones/{zoneId}/hold", filter, cancellationToken);
}
}
}

View File

@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A DNS Zone.
/// </summary>
public class Zone
{
/// <summary>
/// Identifier.
/// </summary>
// <= 32 characters
[JsonProperty("id")]
public string Id { get; set; }
/// <summary>
/// The account the zone belongs to.
/// </summary>
[JsonProperty("account")]
public AccountBase Account { get; set; }
/// <summary>
/// The last time proof of ownership was detected and the zone was made active.
/// </summary>
[JsonProperty("activated_on")]
public DateTime ActivatedOn { get; set; }
/// <summary>
/// When the zone was created.
/// </summary>
[JsonProperty("created_on")]
public DateTime CreatedOn { get; set; }
/// <summary>
/// The interval (in seconds) from when development mode expires (positive integer)
/// or last expired (negative integer) for the domain.
/// If development mode has never been enabled, this value is 0.
/// </summary>
[JsonProperty("development_mode")]
public int DevelopmentMode { get; set; }
/// <summary>
/// Metadata about the zone.
/// </summary>
[JsonProperty("meta")]
public ZoneMetaData Meta { get; set; }
/// <summary>
/// When the zone was last modified.
/// </summary>
[JsonProperty("modified_on")]
public DateTime ModifiedOn { get; set; }
/// <summary>
/// The domain name.
/// </summary>
// <= 253 characters
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// The name servers Cloudflare assigns to a zone.
/// </summary>
[JsonProperty("name_servers")]
public IReadOnlyList<string> NameServers { get; set; }
/// <summary>
/// DNS host at the time of switching to Cloudflare.
/// </summary>
[JsonProperty("original_dnshost")]
public string OriginalDnshost { get; set; }
/// <summary>
/// Original name servers before moving to Cloudflare.
/// </summary>
[JsonProperty("original_name_servers")]
public IReadOnlyList<string> OriginalNameServers { get; set; }
/// <summary>
/// Registrar for the domain at the time of switching to Cloudflare.
/// </summary>
[JsonProperty("original_registrar")]
public string OriginalRegistrar { get; set; }
/// <summary>
/// The owner of the zone.
/// </summary>
[JsonProperty("owner")]
public OwnerBase Owner { get; set; }
/// <summary>
/// Indicates whether the zone is only using Cloudflare DNS services.
/// A <see langword="true"/> value means the zone will not receive security or performance benefits.
/// </summary>
[JsonProperty("paused")]
public bool Paused { get; set; }
/// <summary>
/// The zone status on Cloudflare.
/// </summary>
[JsonProperty("status")]
public ZoneStatus Status { 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..
/// </summary>
[JsonProperty("type")]
public ZoneType Type { get; set; }
/// <summary>
/// An array of domains used for custom name servers.
/// <em>This is only available for Business and Enterprise plans.</em>
/// </summary>
[JsonProperty("vanity_name_servers")]
public IReadOnlyList<string> VanityNameServers { get; set; }
}
}

View File

@@ -0,0 +1,28 @@
using System;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A zone hold.
/// </summary>
public class ZoneHold
{
/// <summary>
/// Gets or sets a value indicating whether the zone is on hold.
/// </summary>
[JsonProperty("hold")]
public bool Hold { get; set; }
/// <summary>
/// Gets or sets an information whether subdomains are included in the hold.
/// </summary>
[JsonProperty("include_subdomains")]
public string IncludeSubdomains { get; set; }
/// <summary>
/// Gets or sets the time after which the zone is no longer on hold.
/// </summary>
[JsonProperty("hold_after")]
public DateTime HoldAfter { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// The deleted zone.
/// </summary>
public class ZoneIdResponse
{
/// <summary>
/// Identifier.
/// </summary>
// <= 32 characters
[JsonProperty("id")]
public string Id { get; set; }
}
}

View File

@@ -0,0 +1,50 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// The zone metadata.
/// </summary>
public class ZoneMetaData
{
/// <summary>
/// The zone is only configured for CDN.
/// </summary>
[JsonProperty("cdn_only")]
public bool CdnOnly { get; set; }
/// <summary>
/// Number of Custom Certificates the zone can have.
/// </summary>
[JsonProperty("custom_certificate_quota")]
public int CustomCertificateQuota { get; set; }
/// <summary>
/// The zone is only configured for DNS.
/// </summary>
[JsonProperty("dns_only")]
public bool DnsOnly { get; set; }
/// <summary>
/// The zone is setup with Foundation DNS.
/// </summary>
[JsonProperty("foundation_dns")]
public bool FoundationDns { get; set; }
/// <summary>
/// Number of Page Rules a zone can have.
/// </summary>
[JsonProperty("page_rule_quota")]
public int PageRuleQuota { get; set; }
/// <summary>
/// The zone has been flagged for phishing.
/// </summary>
[JsonProperty("phishing_detected")]
public bool PhishingDetected { get; set; }
/// <summary>
/// Step.
/// </summary>
[JsonProperty("step")]
public int Step { get; set; }
}
}

View File

@@ -4,13 +4,34 @@ With this extension package, you'll get all features available to manage a Zone
## Methods
### Zone
- [ListZones](https://developers.cloudflare.com/api/operations/zones-get)
- [ZoneDetails](https://developers.cloudflare.com/api/operations/zones-0-get)
- [CreateZone](https://developers.cloudflare.com/api/operations/zones-post)
- [DeleteZone](https://developers.cloudflare.com/api/operations/zones-0-delete)
- [ZoneDetails](https://developers.cloudflare.com/api/operations/zones-0-get)
- [EditZone](https://developers.cloudflare.com/api/operations/zones-0-patch)
- [RerunActivationCheck](https://developers.cloudflare.com/api/operations/put-zones-zone_id-activation_check)
- [PurgeCachedContent](https://developers.cloudflare.com/api/operations/zone-purge)
### Zone Holds
- [DeleteZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-delete)
- [GetZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-get)
- [CreateZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-post)
### DNS Settings for a Zone
- TBD
### DNS Records for a Zone
- TBD
---
Published under MIT License (see [choose a license])

View File

@@ -0,0 +1,9 @@
using System.Text.RegularExpressions;
namespace AMWD.Net.Api.Cloudflare.Zones
{
internal static class RegexPatterns
{
public static readonly Regex ZoneName = new(@"^([a-zA-Z0-9][\-a-zA-Z0-9]*\.)+[\-a-zA-Z0-9]{2,20}$", RegexOptions.Compiled);
}
}

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);
}
}
}

View File

@@ -32,19 +32,15 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" PublicKey="0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" />
</ItemGroup>
<ItemGroup>
<None Include="$(SolutionDir)/package-icon.png" Pack="true" PackagePath="/" />
<None Include="README.md" Pack="true" PackagePath="/" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="AMWD.Net.API.Cloudflare" Version="0.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="$(SolutionDir)\Cloudflare\Cloudflare.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AMWD.NetRevisionTask" Version="1.2.1">
<PrivateAssets>all</PrivateAssets>
@@ -52,5 +48,12 @@
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="AMWD.Net.API.Cloudflare" Version="0.1.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="$(SolutionDir)\Cloudflare\Cloudflare.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
</Project>