// Create VNet for the LB public IEnumerable <IPublicIPAddress> EnsurePIPs(IPublicIPAddresses pips) { var creatablePips = new List <ICreatable <IPublicIPAddress> >(); for (int i = 0; i < PipNames.Length; i++) { creatablePips.Add(pips.Define(PipNames[i]) .WithRegion(Region) .WithNewResourceGroup(GroupName)); } return(pips.Create(creatablePips.ToArray())); }
public override ILoadBalancer CreateResource(ILoadBalancers resources) { pips = resources.Manager.PublicIPAddresses; availabilitySets = computeManager.AvailabilitySets; networks = resources.Manager.Networks; var existingVMs = loadBalancerHelper.EnsureVMs(networks, computeManager, 2); var existingPips = loadBalancerHelper.EnsurePIPs(pips); IPublicIPAddress pip0 = resources.Manager.PublicIPAddresses.GetByResourceGroup( loadBalancerHelper.GroupName, loadBalancerHelper.PipNames[0]); // Create a load balancer var lb = resources.Define(loadBalancerHelper.LoadBalancerName) .WithRegion(loadBalancerHelper.Region) .WithExistingResourceGroup(loadBalancerHelper.GroupName) // Load balancing rules .DefineLoadBalancingRule("rule1") .WithProtocol(TransportProtocol.Tcp) // Required .FromExistingPublicIPAddress(pip0) .FromFrontendPort(81) .ToBackend("backend1") .ToBackendPort(82) // Optionals .WithProbe("tcpProbe1") .WithIdleTimeoutInMinutes(10) .WithLoadDistribution(LoadDistribution.SourceIP) .Attach() // Inbound NAT pools .DefineInboundNatPool("natpool1") .WithProtocol(TransportProtocol.Tcp) .FromExistingPublicIPAddress(pip0) .FromFrontendPortRange(2000, 2001) .ToBackendPort(8080) .Attach() // Probes .DefineTcpProbe("tcpProbe1") .WithPort(25) // Required .WithIntervalInSeconds(15) // Optionals .WithNumberOfProbes(5) .Attach() .DefineHttpProbe("httpProbe1") .WithRequestPath("/") // Required .WithIntervalInSeconds(13) // Optionals .WithNumberOfProbes(4) .Attach() // Backends .DefineBackend("backend1") .WithExistingVirtualMachines(new List <IHasNetworkInterfaces>(existingVMs)) .Attach() .Create(); // Verify frontends Assert.Equal(1, lb.Frontends.Count); Assert.Equal(1, lb.PublicFrontends.Count); Assert.Equal(0, lb.PrivateFrontends.Count); var frontend = lb.Frontends.Values.First(); Assert.True(frontend.IsPublic); var publicFrontend = (ILoadBalancerPublicFrontend)frontend; Assert.Equal(pip0.Id, publicFrontend.PublicIPAddressId, ignoreCase: true); // Verify backends Assert.Equal(1, lb.Backends.Count); // Verify probes Assert.Equal(1, lb.HttpProbes.Count); Assert.True(lb.HttpProbes.ContainsKey("httpProbe1")); Assert.Equal(1, lb.TcpProbes.Count); Assert.True(lb.TcpProbes.ContainsKey("tcpProbe1")); // Verify rules Assert.Equal(1, lb.LoadBalancingRules.Count); Assert.True(lb.LoadBalancingRules.ContainsKey("rule1")); var rule = lb.LoadBalancingRules["rule1"]; Assert.NotNull(rule.Backend); Assert.Equal("tcpProbe1", rule.Probe.Name, ignoreCase: true); // Verify inbound NAT pools Assert.True(lb.InboundNatPools.ContainsKey("natpool1")); Assert.Equal(1, lb.InboundNatPools.Count); var inboundNatPool = lb.InboundNatPools["natpool1"]; Assert.Equal(2000, inboundNatPool.FrontendPortRangeStart); Assert.Equal(2001, inboundNatPool.FrontendPortRangeEnd); Assert.Equal(8080, inboundNatPool.BackendPort); return(lb); }
public override ILoadBalancer CreateResource(ILoadBalancers resources) { pips = resources.Manager.PublicIPAddresses; networks = resources.Manager.Networks; availabilitySets = computeManager.AvailabilitySets; var existingVMs = loadBalancerHelper.EnsureVMs(networks, computeManager, 2); Assert.Equal(2, existingVMs.Count()); var existingPips = loadBalancerHelper.EnsurePIPs(pips); var nic1 = existingVMs.ElementAt(0).GetPrimaryNetworkInterface(); var nic2 = existingVMs.ElementAt(1).GetPrimaryNetworkInterface(); IPublicIPAddress pip = resources.Manager.PublicIPAddresses.GetByResourceGroup( loadBalancerHelper.GroupName, loadBalancerHelper.PipNames[0]); // Create a load balancer var lb = resources.Define(loadBalancerHelper.LoadBalancerName) .WithRegion(loadBalancerHelper.Region) .WithExistingResourceGroup(loadBalancerHelper.GroupName) // Load balancing rules .DefineLoadBalancingRule("rule1") .WithProtocol(TransportProtocol.Tcp) // Required .FromExistingPublicIPAddress(pip) .FromFrontendPort(81) .ToBackend("backend1") .ToBackendPort(82) // Optionals .WithProbe("tcpProbe1") .WithIdleTimeoutInMinutes(10) .WithLoadDistribution(LoadDistribution.SourceIP) .Attach() // Inbound NAT rules .DefineInboundNatRule("natrule1") .WithProtocol(TransportProtocol.Tcp) .FromExistingPublicIPAddress(pip) // Implicitly uses the same frontend because the PIP is the same .FromFrontendPort(88) .Attach() // Probes .DefineTcpProbe("tcpProbe1") .WithPort(25) // Required .WithIntervalInSeconds(15) // Optionals .WithNumberOfProbes(5) .Attach() .DefineHttpProbe("httpProbe1") .WithRequestPath("/") // Required .WithIntervalInSeconds(13) // Optionals .WithNumberOfProbes(4) .Attach() .Create(); string backendName = lb.Backends.Values.First().Name; string frontendName = lb.Frontends.Values.First().Name; // Connect NICs explicitly nic1.Update() .WithExistingLoadBalancerBackend(lb, backendName) .WithExistingLoadBalancerInboundNatRule(lb, "natrule1") .Apply(); NetworkInterfaceHelper.PrintNic(nic1); Assert.Equal(nic1.PrimaryIPConfiguration.ListAssociatedLoadBalancerBackends().ElementAt(0) .Name, backendName, ignoreCase: true); Assert.Equal("natrule1", nic1.PrimaryIPConfiguration.ListAssociatedLoadBalancerInboundNatRules().ElementAt(0) .Name, ignoreCase: true); nic2.Update() .WithExistingLoadBalancerBackend(lb, backendName) .Apply(); NetworkInterfaceHelper.PrintNic(nic2); Assert.Equal(nic2.PrimaryIPConfiguration.ListAssociatedLoadBalancerBackends().ElementAt(0) .Name, backendName, ignoreCase: true); // Verify frontends Assert.Single(lb.Frontends.Values); Assert.Single(lb.PublicFrontends.Values); Assert.Empty(lb.PrivateFrontends.Values); Assert.True(lb.Frontends.ContainsKey(frontendName)); var frontend = lb.Frontends[frontendName]; Assert.True(frontend.IsPublic); var publicFrontend = (ILoadBalancerPublicFrontend)frontend; Assert.Equal(pip.Id, publicFrontend.PublicIPAddressId, ignoreCase: true); pip.Refresh(); Assert.Equal(pip.GetAssignedLoadBalancerFrontend().Name, frontendName, ignoreCase: true); // Verify backends Assert.True(lb.Backends.ContainsKey(backendName)); Assert.Single(lb.Backends.Values); // Verify probes Assert.True(lb.HttpProbes.ContainsKey("httpProbe1")); Assert.Single(lb.HttpProbes.Values); Assert.True(lb.TcpProbes.ContainsKey("tcpProbe1")); Assert.Single(lb.TcpProbes.Values); // Verify rules Assert.Single(lb.LoadBalancingRules.Values); Assert.True(lb.LoadBalancingRules.ContainsKey("rule1")); var rule = lb.LoadBalancingRules["rule1"]; Assert.Equal(rule.Backend.Name, backendName, ignoreCase: true); Assert.Equal(rule.Frontend.Name, frontendName, ignoreCase: true); Assert.Equal("tcpProbe1", rule.Probe.Name, ignoreCase: true); // Verify inbound NAT rules Assert.Single(lb.InboundNatRules.Values); Assert.True(lb.InboundNatRules.ContainsKey("natrule1")); var inboundNatRule = lb.InboundNatRules["natrule1"]; Assert.Equal(inboundNatRule.Frontend.Name, frontendName, ignoreCase: true); Assert.Equal(88, inboundNatRule.FrontendPort); Assert.Equal(88, inboundNatRule.BackendPort); return(lb); }