From 8ef73130a9c537fff16e6b8f2fc8387cf801bb90 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 | 2 ++ 3 files changed, 22 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 0f4d6ea2..d356ec7a 100644 --- a/Source/StrongGrid/Extensions/Public.cs +++ b/Source/StrongGrid/Extensions/Public.cs @@ -118,19 +118,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 5ec4888c..2e56f485 100644 --- a/Source/StrongGrid/Utilities/Utils.cs +++ b/Source/StrongGrid/Utilities/Utils.cs @@ -12,6 +12,8 @@ internal static class Utils private static readonly byte[] Secp256R1Prefix = Convert.FromBase64String("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE"); private static readonly byte[] CngBlobPrefix = { 0x45, 0x43, 0x53, 0x31, 0x20, 0, 0, 0 }; + 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();