public static void PrintPIP(IPublicIPAddress resource)
        {
            var info = new StringBuilder().Append("Public IP Address: ").Append(resource.Id)
                       .Append("\n\tName: ").Append(resource.Name)
                       .Append("\n\tResource group: ").Append(resource.ResourceGroupName)
                       .Append("\n\tRegion: ").Append(resource.Region)
                       .Append("\n\tTags: ").Append(resource.Tags)
                       .Append("\n\tIP Address: ").Append(resource.IPAddress)
                       .Append("\n\tLeaf domain label: ").Append(resource.LeafDomainLabel)
                       .Append("\n\tFQDN: ").Append(resource.Fqdn)
                       .Append("\n\tReverse FQDN: ").Append(resource.ReverseFqdn)
                       .Append("\n\tIdle timeout (minutes): ").Append(resource.IdleTimeoutInMinutes)
                       .Append("\n\tIP allocation method: ").Append(resource.IPAllocationMethod)
                       .Append("\n\tIP version: ").Append(resource.Version);

            // Show the associated load balancer if any
            info.Append("\n\tLoad balancer association: ");
            if (resource.HasAssignedLoadBalancer)
            {
                var frontend = resource.GetAssignedLoadBalancerFrontend();
                var lb       = frontend.Parent;
                info.Append("\n\t\tLoad balancer ID: ").Append(lb.Id)
                .Append("\n\t\tFrontend name: ").Append(frontend.Name);
            }
            else
            {
                info.Append("(None)");
            }

            // Show the associated NIC if any
            info.Append("\n\tNetwork interface association: ");
            if (resource.HasAssignedNetworkInterface)
            {
                var nicIP = resource.GetAssignedNetworkInterfaceIPConfiguration();
                var nic   = nicIP.Parent;
                info.Append("\n\t\tNetwork interface ID: ").Append(nic.Id)
                .Append("\n\t\tIP config name: ").Append(nicIP.Name);
            }
            else
            {
                info.Append("(None)");
            }

            TestHelper.WriteLine(info.ToString());
        }
        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);
        }