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 { /// /// Extends the with methods for working with zones. /// public static class ZoneExtensions { /// /// Lists, searches, sorts, and filters your zones. /// /// The . /// Filter options (optional). /// 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", options, cancellationToken); } /// /// Get details for a zone. /// /// The . /// The zone ID. /// 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); } /// /// Create a new zone. /// /// The . /// The request information. /// 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) { 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("zones", req, cancellationToken: cancellationToken); } /// /// Deletes an existing zone. /// /// The . /// The zone ID. /// 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 . /// The request information. /// 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.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($"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. /// /// 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 . /// The zone ID. /// 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); } } }