Skip to content

Commit 549647d

Browse files
committed
Implement support and tests for listing historical billable load balancers
1 parent 01c4163 commit 549647d

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/corelib/Providers/Rackspace/CloudLoadBalancerProvider.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,37 @@ public Task<IEnumerable<string>> ListAllowedDomainsAsync(CancellationToken cance
945945
/// <inheritdoc/>
946946
public Task<IEnumerable<LoadBalancer>> ListBillableLoadBalancersAsync(DateTimeOffset? startTime, DateTimeOffset? endTime, int? offset, int? limit, CancellationToken cancellationToken)
947947
{
948-
throw new NotImplementedException();
948+
if (endTime < startTime)
949+
throw new ArgumentOutOfRangeException("endTime");
950+
if (offset < 0)
951+
throw new ArgumentOutOfRangeException("offset");
952+
if (limit <= 0)
953+
throw new ArgumentOutOfRangeException("limit");
954+
955+
UriTemplate template = new UriTemplate("/loadbalancers/billable?startTime={startTime}&endTime={endTime}&offset={offset}&limit={limit}");
956+
var parameters = new Dictionary<string, string>();
957+
if (startTime != null)
958+
parameters.Add("startTime", startTime.Value.ToString("yyyy-MM-dd"));
959+
if (endTime != null)
960+
parameters.Add("endTime", endTime.Value.ToString("yyyy-MM-dd"));
961+
if (offset != null)
962+
parameters.Add("offset", offset.ToString());
963+
if (limit != null)
964+
parameters.Add("limit", limit.ToString());
965+
966+
Func<Task<Tuple<IdentityToken, Uri>>, HttpWebRequest> prepareRequest =
967+
PrepareRequestAsyncFunc(HttpMethod.GET, template, parameters);
968+
969+
Func<Task<HttpWebRequest>, Task<ListLoadBalancersResponse>> requestResource =
970+
GetResponseAsyncFunc<ListLoadBalancersResponse>(cancellationToken);
971+
972+
Func<Task<ListLoadBalancersResponse>, IEnumerable<LoadBalancer>> resultSelector =
973+
task => (task.Result != null ? task.Result.LoadBalancers : null) ?? Enumerable.Empty<LoadBalancer>();
974+
975+
return AuthenticateServiceAsync(cancellationToken)
976+
.ContinueWith(prepareRequest)
977+
.ContinueWith(requestResource).Unwrap()
978+
.ContinueWith(resultSelector);
949979
}
950980

951981
/// <inheritdoc/>

src/testing/integration/Providers/Rackspace/UserLoadBalancerTests.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,39 @@ public async Task TestListAllowedDomains()
692692
[TestMethod]
693693
[TestCategory(TestCategories.User)]
694694
[TestCategory(TestCategories.LoadBalancers)]
695-
public void TestListBillableLoadBalancers()
695+
public async Task TestListBillableLoadBalancers()
696696
{
697-
Assert.Inconclusive("Not yet implemented.");
697+
ILoadBalancerService provider = CreateProvider();
698+
using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(60))))
699+
{
700+
IEnumerable<LoadBalancingProtocol> protocols = await provider.ListProtocolsAsync(cancellationTokenSource.Token);
701+
LoadBalancingProtocol httpProtocol = protocols.First(i => i.Name.Equals("HTTP", StringComparison.OrdinalIgnoreCase));
702+
703+
string loadBalancerName = CreateRandomLoadBalancerName();
704+
705+
LoadBalancerConfiguration configuration = new LoadBalancerConfiguration(
706+
name: loadBalancerName,
707+
nodes: null,
708+
protocol: httpProtocol,
709+
virtualAddresses: new[] { new LoadBalancerVirtualAddress(LoadBalancerVirtualAddressType.Public) },
710+
algorithm: LoadBalancingAlgorithm.RoundRobin);
711+
LoadBalancer tempLoadBalancer = await provider.CreateLoadBalancerAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
712+
713+
IEnumerable<LoadBalancer> billable = await provider.ListBillableLoadBalancersAsync(DateTimeOffset.Now.Date.AddDays(-60), DateTimeOffset.Now.Date.AddDays(1), null, null, cancellationTokenSource.Token);
714+
Assert.IsNotNull(billable);
715+
LoadBalancer[] loadBalancers = billable.ToArray();
716+
if (loadBalancers.Length == 0)
717+
Assert.Inconclusive("No billable load balancers were reported.");
718+
719+
Console.WriteLine("Billable Load Balancers:");
720+
foreach (LoadBalancer loadBalancer in loadBalancers)
721+
Console.WriteLine(" {0}", loadBalancer.Id);
722+
723+
/* Cleanup
724+
*/
725+
726+
await provider.RemoveLoadBalancerAsync(tempLoadBalancer.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
727+
}
698728
}
699729

700730
[TestMethod]

0 commit comments

Comments
 (0)