public async Task TestAccessLists() { ILoadBalancerService provider = CreateProvider(); using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(60)))) { IEnumerable<LoadBalancingProtocol> protocols = await provider.ListProtocolsAsync(cancellationTokenSource.Token); LoadBalancingProtocol httpProtocol = protocols.First(i => i.Name.Equals("HTTP", StringComparison.OrdinalIgnoreCase)); string loadBalancerName = CreateRandomLoadBalancerName(); LoadBalancerConfiguration configuration = new LoadBalancerConfiguration( name: loadBalancerName, nodes: null, protocol: httpProtocol, virtualAddresses: new[] { new LoadBalancerVirtualAddress(LoadBalancerVirtualAddressType.ServiceNet) }, algorithm: LoadBalancingAlgorithm.RoundRobin); LoadBalancer tempLoadBalancer = await provider.CreateLoadBalancerAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); // verify initially null IEnumerable<NetworkItem> accessList = await provider.ListAccessListAsync(tempLoadBalancer.Id, cancellationTokenSource.Token); Assert.IsNotNull(accessList); Assert.IsFalse(accessList.Any()); // allow docs.rackspace.com IPHostEntry resolved = await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("docs.rackspace.com", null, null), Dns.EndGetHostEntry); NetworkItem firstNetworkItem = new NetworkItem(resolved.AddressList[0], AccessType.Allow); await provider.CreateAccessListAsync(tempLoadBalancer.Id, firstNetworkItem, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); //List<NetworkItem> expected = new List<NetworkItem> { firstNetworkItem }; LoadBalancer loadBalancer = await provider.GetLoadBalancerAsync(tempLoadBalancer.Id, cancellationTokenSource.Token); Assert.IsNotNull(loadBalancer.AccessList); Assert.AreEqual(1, loadBalancer.AccessList.Count); Assert.AreEqual(firstNetworkItem.Address, loadBalancer.AccessList[0].Address); Assert.AreEqual(firstNetworkItem.AccessType, loadBalancer.AccessList[0].AccessType); //for (int i = 0; i < expected.Count; i++) //{ // Assert.AreEqual(expected[i].Address, loadBalancer.AccessList[i].Address); // Assert.AreEqual(expected[i].AccessType, loadBalancer.AccessList[i].AccessType); //} // allow developer.rackspace.com // deny google.com, yahoo.com, microsoft.com, amazon.com NetworkItem[] batch = new[] { new NetworkItem( (await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("google.com", null, null), Dns.EndGetHostEntry)).AddressList[0], AccessType.Deny), new NetworkItem( (await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("developer.rackspace.com", null, null), Dns.EndGetHostEntry)).AddressList[0], AccessType.Allow), new NetworkItem( (await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("yahoo.com", null, null), Dns.EndGetHostEntry)).AddressList[0], AccessType.Deny), new NetworkItem( (await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("microsoft.com", null, null), Dns.EndGetHostEntry)).AddressList[0], AccessType.Deny), new NetworkItem( (await Task.Factory.FromAsync<IPHostEntry>(Dns.BeginGetHostEntry("amazon.com", null, null), Dns.EndGetHostEntry)).AddressList[0], AccessType.Deny), }; await provider.CreateAccessListAsync(tempLoadBalancer.Id, batch, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); //expected.AddRange(batch.AsEnumerable().Reverse()); loadBalancer = await provider.GetLoadBalancerAsync(tempLoadBalancer.Id, cancellationTokenSource.Token); Assert.IsNotNull(loadBalancer.AccessList); Assert.AreEqual(6, loadBalancer.AccessList.Count); //for (int i = 0; i < expected.Count; i++) //{ // Assert.AreEqual(expected[i].Address, loadBalancer.AccessList[i].Address); // Assert.AreEqual(expected[i].AccessType, loadBalancer.AccessList[i].AccessType); //} // remove a single item from the middle of the access list: developer.rackspace.com await provider.RemoveAccessListAsync(tempLoadBalancer.Id, loadBalancer.AccessList[2].Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); //expected.RemoveAt(2); loadBalancer = await provider.GetLoadBalancerAsync(tempLoadBalancer.Id, cancellationTokenSource.Token); Assert.IsNotNull(loadBalancer.AccessList); Assert.AreEqual(5, loadBalancer.AccessList.Count); //for (int i = 0; i < expected.Count; i++) //{ // Assert.AreEqual(expected[i].Address, loadBalancer.AccessList[i].Address); // Assert.AreEqual(expected[i].AccessType, loadBalancer.AccessList[i].AccessType); //} // remove two items from the middle of the access list: developer.rackspace.com await provider.RemoveAccessListRangeAsync(tempLoadBalancer.Id, new[] { loadBalancer.AccessList[0].Id, loadBalancer.AccessList[2].Id }, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); //expected.RemoveAt(2); //expected.RemoveAt(0); loadBalancer = await provider.GetLoadBalancerAsync(tempLoadBalancer.Id, cancellationTokenSource.Token); Assert.IsNotNull(loadBalancer.AccessList); Assert.AreEqual(3, loadBalancer.AccessList.Count); //for (int i = 0; i < expected.Count; i++) //{ // Assert.AreEqual(expected[i].Address, loadBalancer.AccessList[i].Address); // Assert.AreEqual(expected[i].AccessType, loadBalancer.AccessList[i].AccessType); //} // clear the access list await provider.ClearAccessListAsync(tempLoadBalancer.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); // clear the access list again try { try { await provider.ClearAccessListAsync(tempLoadBalancer.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); } catch (WebException ex) { throw new AggregateException(ex); } } catch (AggregateException aggregate) { aggregate.Flatten().Handle( ex => { WebException webException = ex as WebException; if (webException == null) return false; HttpWebResponse response = webException.Response as HttpWebResponse; if (response == null) return false; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); return true; }); } /* Cleanup */ await provider.RemoveLoadBalancerAsync(tempLoadBalancer.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null); } }
public static void CreateAccessList(this ILoadBalancerService service, LoadBalancerId loadBalancerId, NetworkItem networkItem) { if (service == null) throw new ArgumentNullException("service"); try { service.CreateAccessListAsync(loadBalancerId, networkItem, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Wait(); } catch (AggregateException ex) { ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions; if (innerExceptions.Count == 1) throw innerExceptions[0]; throw; } }