Add "ZoneHold" extensions

This commit is contained in:
2025-06-24 20:44:09 +02:00
parent f9b7dcb7d2
commit 682f25ae75
13 changed files with 724 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Internals
{
internal class InternalCreateZoneHoldFilter : IQueryParameterFilter
{
public bool? IncludeSubdomains { get; set; }
public IDictionary<string, string> GetQueryParameters()
{
var dict = new Dictionary<string, string>();
if (IncludeSubdomains.HasValue)
dict.Add("include_subdomains", IncludeSubdomains.Value ? "true" : "false");
return dict;
}
}
}

View File

@@ -0,0 +1,17 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Internals
{
internal class InternalRemoveZoneHoldFilter : 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,11 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Internals
{
internal class InternalUpdateZoneHoldRequest
{
[JsonProperty("hold_after")]
public DateTime? HoldAfter { get; set; }
[JsonProperty("include_subdomains")]
public bool? IncludeSubdomains { get; set; }
}
}

View File

@@ -0,0 +1,27 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A Cloudflare zone hold.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/holds.ts#L88">Source</see>
/// </summary>
public class ZoneHold
{
/// <summary>
/// Whether the zone is on hold.
/// </summary>
[JsonProperty("hold")]
public bool? Hold { get; set; }
/// <summary>
/// The hold is enabled if the value is in the past.
/// </summary>
[JsonProperty("hold_after")]
public DateTime? HoldAfter { get; set; }
/// <summary>
/// Whether to include subdomains in the hold.
/// </summary>
[JsonProperty("include_subdomains")]
public bool? IncludeSubdomains { get; set; }
}
}

View File

@@ -25,6 +25,13 @@ This package contains the feature set of the _Domain/Zone Management_ section of
- [Rerun The Activation Check](https://developers.cloudflare.com/api/resources/zones/subresources/activation_check/methods/trigger/)
##### [Holds]
- [Create Zone Hold](https://developers.cloudflare.com/api/resources/zones/subresources/holds/methods/create/)
- [Remove Zone Hold](https://developers.cloudflare.com/api/resources/zones/subresources/holds/methods/delete/)
- [Update Zone Hold](https://developers.cloudflare.com/api/resources/zones/subresources/holds/methods/edit/)
- [Get Zone Hold](https://developers.cloudflare.com/api/resources/zones/subresources/holds/methods/get/)
@@ -41,3 +48,4 @@ Published under MIT License (see [choose a license])
[Registrar]: https://developers.cloudflare.com/api/resources/registrar/
[Zones]: https://developers.cloudflare.com/api/resources/zones/
[Activation Check]: https://developers.cloudflare.com/api/resources/zones/subresources/activation_check/
[Holds]: https://developers.cloudflare.com/api/resources/zones/subresources/holds/

View File

@@ -0,0 +1,32 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Represents a request to create a zone hold.
/// </summary>
public class CreateZoneHoldRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="CreateZoneHoldRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public CreateZoneHoldRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// If provided, the zone hold will extend to block any subdomain of the given zone, as well as SSL4SaaS Custom Hostnames.
/// </summary>
/// <remarks>
/// For example, a zone hold on a zone with the hostname 'example.com' and
/// <c><see cref="IncludeSubdomains"/>=<see langword="true"/></c> will block
/// 'example.com', 'staging.example.com', 'api.staging.example.com', etc.
/// </remarks>
public bool? IncludeSubdomains { get; set; }
}
}

View File

@@ -0,0 +1,29 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Represents a request to remove a zone hold.
/// </summary>
public class RemoveZoneHoldRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="RemoveZoneHoldRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public RemoveZoneHoldRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// If it is provided, the hold will be temporarily disabled,
/// then automatically re-enabled by the system at the time specified in this timestamp.
/// Otherwise, the hold will be disabled indefinitely.
/// </summary>
public DateTime? HoldAfter { get; set; }
}
}

View File

@@ -0,0 +1,37 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Represents a request to update a zone hold.
/// </summary>
public class UpdateZoneHoldRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateZoneHoldRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public UpdateZoneHoldRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// If the value is provided and future-dated, the hold will be temporarily disabled,
/// then automatically re-enabled by the system at the time specified in this timestamp.
/// A past-dated value will have no effect on an existing, enabled hold.
/// Providing an empty string will set its value to the current time.
/// </summary>
public DateTime? HoldAfter { get; set; }
/// <summary>
/// If <see langword="true"/>, 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 <c><see cref="IncludeSubdomains"/>=<see langword="true"/></c>
/// will block 'example.com', 'staging.example.com', 'api.staging.example.com', etc.
/// </summary>
public bool? IncludeSubdomains { get; set; }
}
}

View File

@@ -0,0 +1,82 @@
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Zones.Internals;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Extensions for <see href="https://developers.cloudflare.com/api/resources/zones/subresources/holds/">Zone Holds</see>.
/// </summary>
public static class ZoneHoldsExtensions
{
/// <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"/> 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<ZoneHold>> CreateZoneHold(this ICloudflareClient client, CreateZoneHoldRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
var filter = new InternalCreateZoneHoldFilter
{
IncludeSubdomains = request.IncludeSubdomains
};
return client.PostAsync<ZoneHold, object>($"/zones/{request.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"/> 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<ZoneHold>> RemoveZoneHold(this ICloudflareClient client, RemoveZoneHoldRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
var filter = new InternalRemoveZoneHoldFilter
{
HoldAfter = request.HoldAfter
};
return client.DeleteAsync<ZoneHold>($"/zones/{request.ZoneId}/hold", filter, cancellationToken);
}
/// <summary>
/// Update the <see cref="UpdateZoneHoldRequest.HoldAfter"/> and/or <see cref="UpdateZoneHoldRequest.IncludeSubdomains"/> values on an existing zone hold.
/// The hold is enabled if the <see cref="UpdateZoneHoldRequest.HoldAfter"/> is in the past.
/// </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<ZoneHold>> UpdateZoneHold(this ICloudflareClient client, UpdateZoneHoldRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
var req = new InternalUpdateZoneHoldRequest
{
HoldAfter = request.HoldAfter,
IncludeSubdomains = request.IncludeSubdomains
};
return client.PatchAsync<ZoneHold, InternalUpdateZoneHoldRequest>($"/zones/{request.ZoneId}/hold", req, cancellationToken);
}
/// <summary>
/// Retrieve whether the zone is subject to a zone hold, and metadata about the hold.
/// </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<ZoneHold>> GetZoneHold(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<ZoneHold>($"/zones/{zoneId}/hold", cancellationToken: cancellationToken);
}
}
}