using System.Linq; using System.Threading; using System.Threading.Tasks; using AMWD.Net.Api.Cloudflare.Zones.Internals; namespace AMWD.Net.Api.Cloudflare.Zones { /// /// Extensions for Zones. /// public static class ZonesExtensions { /// /// Create Zone. /// /// The instance. /// The request. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> CreateZone(this ICloudflareClient client, CreateZoneRequest request, CancellationToken cancellationToken = default) { request.AccountId?.ValidateCloudflareId(); request.Name.ValidateLength(253, nameof(request.Name)); if (request.Type.HasValue && !Enum.IsDefined(typeof(ZoneType), request.Type.Value)) throw new ArgumentOutOfRangeException(nameof(request.Type)); var req = new InternalCreateZoneRequest( request.AccountId, request.Name, request.Type ); return client.PostAsync($"/zones", req, cancellationToken: cancellationToken); } /// /// Deletes an existing zone. /// /// The instance. /// The zone identifier. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> DeleteZone(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); return client.DeleteAsync($"/zones/{zoneId}", cancellationToken: cancellationToken); } /// /// Edits a zone. /// /// /// Only one zone property can be changed at a time. /// /// The instance. /// The request. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> EditZone(this ICloudflareClient client, EditZoneRequest request, CancellationToken cancellationToken = default) { request.ZoneId.ValidateCloudflareId(); if (request.Paused.HasValue && (request.Type.HasValue || request.VanityNameServers != null)) throw new CloudflareException("Only one zone property can be changed at a time."); 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 InternalEditZoneRequest { Paused = request.Paused, Type = request.Type }; if (request.VanityNameServers != null) req.VanityNameServers = request.VanityNameServers.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); return client.PatchAsync($"/zones/{request.ZoneId}", req, cancellationToken: cancellationToken); } /// /// Zone Details. /// /// The instance. /// The zone identifier. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> ZoneDetails(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); return client.GetAsync($"/zones/{zoneId}", cancellationToken: cancellationToken); } /// /// Lists, searches, sorts, and filters your zones. /// /// /// Listing zones across more than 500 accounts is currently not allowed. /// /// The instance. /// Filter options. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task>> ListZones(this ICloudflareClient client, ListZonesFilter? options = null, CancellationToken cancellationToken = default) { return client.GetAsync>($"/zones", queryFilter: options, cancellationToken: cancellationToken); } /// /// Triggeres a new activation check for a zone. /// /// /// This can be triggered every 5 min for paygo/enterprise customers, every hour for FREE Zones. /// /// The instance. /// The zone identifier. /// A cancellation token used to propagate notification that this operation should be canceled. public static Task> RerunActivationCheck(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default) { zoneId.ValidateCloudflareId(); return client.PutAsync($"/zones/{zoneId}/activation_check", null, cancellationToken); } } }