Skip to content

Commit

Permalink
(GH-368) Add missing 'limit' and 'offset' parameters to IpAddresses.G…
Browse files Browse the repository at this point in the history
…etUnassignedAsync
  • Loading branch information
Jericho committed Aug 11, 2021
1 parent f1181a8 commit 5b96a6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions Source/StrongGrid/Extensions/Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,19 +1130,32 @@ public static Task<Segment> UpdateAsync(this ISegments segments, string segmentI
/// Retrieve unassigned IP addresses.
/// </summary>
/// <param name="ipAddresses">The IP addresses resource.</param>
/// <param name="limit">The number of IPs you want returned at the same time.</param>
/// <param name="offset">The offset for the number of IPs that you are requesting.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// An array of <see cref="IpAddress">Ip addresses</see>.
/// </returns>
public static async Task<IpAddress[]> GetUnassignedAsync(this IIpAddresses ipAddresses, CancellationToken cancellationToken = default)
public static async Task<IpAddress[]> 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<IpAddress>(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();
}

}
Expand Down
4 changes: 4 additions & 0 deletions Source/StrongGrid/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
Expand Down

0 comments on commit 5b96a6c

Please sign in to comment.