From 5b96a6c0ab5610ba6ebb4a23f4c8562e96dadced Mon Sep 17 00:00:00 2001 From: Jericho Date: Thu, 28 Jan 2021 15:51:29 -0500 Subject: [PATCH] (GH-368) Add missing 'limit' and 'offset' parameters to IpAddresses.GetUnassignedAsync --- .../Tests/IpAddresses.cs | 2 +- Source/StrongGrid/Extensions/Public.cs | 25 ++++++++++++++----- Source/StrongGrid/Utilities/Utils.cs | 4 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs b/Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs index 6f0009f2..a5590afc 100644 --- a/Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs +++ b/Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs @@ -40,7 +40,7 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken await log.WriteLineAsync($"There are {assigned.Length} assigned IP addresses").ConfigureAwait(false); // GET THE UNASSIGNED IP ADDRESSES - var unAssigned = await client.IpAddresses.GetUnassignedAsync(cancellationToken).ConfigureAwait(false); + var unAssigned = await client.IpAddresses.GetUnassignedAsync(cancellationToken: cancellationToken).ConfigureAwait(false); await log.WriteLineAsync($"There are {unAssigned.Length} unassigned IP addresses").ConfigureAwait(false); // GET THE REMAINING IP ADDRESSES diff --git a/Source/StrongGrid/Extensions/Public.cs b/Source/StrongGrid/Extensions/Public.cs index e2f6d6c5..1f7c6fb1 100644 --- a/Source/StrongGrid/Extensions/Public.cs +++ b/Source/StrongGrid/Extensions/Public.cs @@ -1130,19 +1130,32 @@ public static Task UpdateAsync(this ISegments segments, string segmentI /// Retrieve unassigned IP addresses. /// /// The IP addresses resource. + /// The number of IPs you want returned at the same time. + /// The offset for the number of IPs that you are requesting. /// Cancellation token. /// /// An array of Ip addresses. /// - public static async Task GetUnassignedAsync(this IIpAddresses ipAddresses, CancellationToken cancellationToken = default) + public static async Task GetUnassignedAsync(this IIpAddresses ipAddresses, int limit = 10, int offset = 0, CancellationToken cancellationToken = default) { - var allIpAddresses = await ipAddresses.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false); + var unassignedIpAddresses = new List(limit); + var currentOffset = 0; - var unassignedIpAddresses = allIpAddresses.Records - .Where(ip => ip.Pools == null || !ip.Pools.Any()) - .ToArray(); + while (true) + { + var allIpAddresses = await ipAddresses.GetAllAsync(limit: Utils.MaxSendGridPagingLimit, offset: currentOffset, cancellationToken: cancellationToken).ConfigureAwait(false); + unassignedIpAddresses.AddRange(allIpAddresses.Records.Where(ip => ip.Pools == null || !ip.Pools.Any())); + + if (unassignedIpAddresses.Count >= offset + limit) break; + if (allIpAddresses.Next == null) break; - return unassignedIpAddresses; + currentOffset += Utils.MaxSendGridPagingLimit; + } + + return unassignedIpAddresses + .Skip(offset) + .Take(limit) + .ToArray(); } } diff --git a/Source/StrongGrid/Utilities/Utils.cs b/Source/StrongGrid/Utilities/Utils.cs index 88a05a84..0b113cec 100644 --- a/Source/StrongGrid/Utilities/Utils.cs +++ b/Source/StrongGrid/Utilities/Utils.cs @@ -11,6 +11,10 @@ internal static class Utils { private static readonly byte[] Secp256R1Prefix = Convert.FromBase64String("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE"); + public const int MaxSendGridPagingLimit = 500; + + public static DateTime Epoch { get; } = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + public static RecyclableMemoryStreamManager MemoryStreamManager { get; } = new RecyclableMemoryStreamManager(); ///