TestCases/SVT/LoadBalancer/LoadBalancerResource.ps1

Set-StrictMode -Version Latest 
class LoadBalancerResource:TestResource{

    hidden [PSObject] $LBLocation = 'East US';

    hidden [PSObject] $SubnetName = 'AzSDKTest-LB-Subnet';
    hidden [PSObject] $VirtualNetworkName = 'AzSDKTest-LB-VNet';

    hidden [PSObject] $PublicIPName = 'AzSDKTest-LB-PublicIP';
    hidden [PSObject] $PublicIPDNS = 'azsdktestlbpublicip';

    hidden [PSObject] $FrontEndIPName = 'AzSDKTest-LB-Frontend';
    hidden [PSObject] $BackEndIPName = 'AzSDKTest-LB-backend';

    hidden [PSObject] $inboundNATRule1Name = 'AzSDKTest-LB-RDP1';
    hidden [PSObject] $inboundNATRule2Name = 'AzSDKTest-LB-RDP2';

    hidden [PSObject] $HealthProbName = 'AzSDKTest-LB-HealthProbe';
    hidden [PSObject] $LBRuleName = 'AzSDKTest-LB-HTTP';
    

    LoadBalancerResource([TestCase] $testcase, [TestSettings] $testsettings):Base($testcase, $testsettings){
     
    }

#Setting the properties as required by this resource type.
    [void]SetDerivedResourceProps(){
        $this.ResourceName = "AzSDKTest-LB" #Else set the default resource name
        $this.ResourceType = "Microsoft.Network/loadBalancers" 
    }

    #Checks and deploys the load balancer if it does not exist.
    [void] InitializeResource(){
        if(!$this.IfResourceExists()){
            $this.DeployLoadBalancer()    
        }
    }

    #Deploy the LoadBalancer
    [void]DeployLoadBalancer()
    {
        try
        {
            
            $publicIP = $this.DeployPublicIP()

            # Create a front-end IP pool and a back-end address pool
            $frontendIP = New-AzureRmLoadBalancerFrontendIpConfig -Name $this.FrontEndIPName -PublicIpAddress $publicIP
            $beaddresspool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name $this.BackEndIPName

            # Create the NAT rules.
            $inboundNATRule1= New-AzureRmLoadBalancerInboundNatRuleConfig -Name $this.inboundNATRule1Name -FrontendIpConfiguration $frontendIP -Protocol TCP -FrontendPort 3441 -BackendPort 3389
            $inboundNATRule2= New-AzureRmLoadBalancerInboundNatRuleConfig -Name $this.inboundNATRule2Name -FrontendIpConfiguration $frontendIP -Protocol TCP -FrontendPort 3442 -BackendPort 3389

            # Create a health probe. There are two ways to configure a probe:
            $healthProbe = New-AzureRmLoadBalancerProbeConfig -Name $this.HealthProbName -RequestPath 'HealthProbe.aspx' -Protocol http -Port 80 -IntervalInSeconds 15 -ProbeCount 2

            # TCP Prob
            $healthProbe = New-AzureRmLoadBalancerProbeConfig -Name $this.HealthProbName -Protocol Tcp -Port 80 -IntervalInSeconds 15 -ProbeCount 2


            # Create a load balancer rule.
            $lbrule = New-AzureRmLoadBalancerRuleConfig -Name $this.LBRuleName -FrontendIpConfiguration $frontendIP -BackendAddressPool  $beAddressPool -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80


            # Create the load balancer by using the previously created objects.
            $lb = New-AzureRmLoadBalancer -ResourceGroupName $this.ResourceGroupName -Name $this.ResourceName -Location $this.LBLocation -FrontendIpConfiguration $frontendIP -InboundNatRule $inboundNATRule1,$inboundNatRule2 -LoadBalancingRule $lbrule -BackendAddressPool $beAddressPool -Probe $healthProbe


            if($lb.ProvisioningState -eq "Succeeded")
            {
                [CommonHelper]::Log("Load balancer "+$this.ResourceName + " is successfully deployed", [MessageType]::Information)
            }
        }
        catch{
            [CommonHelper]::Log("Error while deploying the Load balancer: " + $this.ResourceName, [MessageType]::Error)
        }
    }

    #Set Public IP Address for Load Balancer
    [void]AddPublicIpAddress()
    {
        $lb = Get-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName
        if($null -ne $lb)
        {
            # Remove existing Load Balancer
            Remove-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName -Confirm:$false -Force 

            # Deploy new Load Balancer with Public IP Configuration
            $this.DeployLoadBalancer()
            [CommonHelper]::Log("Successfully set the authorization rules for queue: "+$this.QueueName, [MessageType]::Information)

        }
    }

    # Remove Public IP Address for Load Balancer
    [void]RemovePublicIpAddress()
    {
        try
        {

            $lb = Get-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName
            if($null -ne $lb)
            {
                # Remove existing Load Balancer
                Remove-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName -Confirm:$false -Force 

                # Create new Load Balancer with no configuration
                New-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName -Location $this.LBLocation

                [CommonHelper]::Log("Successfully deployed Load Balancer without public ip configuration : "+ $lb.Name , [MessageType]::Information)
            }
            else
            {
                # Create new Load Balancer with no configuration
                New-AzureRmLoadBalancer -Name $this.ResourceName -ResourceGroupName $this.ResourceGroupName -Location $this.LBLocation
                [CommonHelper]::Log("Successfully deployed Load Balancer without public ip configuration : "+ $lb.Name , [MessageType]::Information)
            }
        }
        catch
        {
            [CommonHelper]::Log("Error while removing Public IP Address steps in Load Balancer: " + $this.ResourceName, [MessageType]::Error)
        }
    }

    # Deploy Public IP Address resource if not available
    [PSObject]DeployPublicIP()
    {
        # Check Public IP Address reource exist
        $publicIP = Get-AzureRmPublicIpAddress -Name $this.PublicIPName -ResourceGroupName $this.ResourceGroupName
        if($null -eq $publicIP)
        {
            $publicIP = New-AzureRmPublicIpAddress -Name $this.PublicIPName -ResourceGroupName $this.ResourceGroupName -Location $this.LBLocation -AllocationMethod Static -DomainNameLabel $this.PublicIPDNS
            [CommonHelper]::Log("Public IP "+ $this.PublicIPName + " is successfully deployed", [MessageType]::Information)
        }
        else 
        {
            [CommonHelper]::Log("Public IP "+ $this.PublicIPName + " is already available.", [MessageType]::Information)    
        }

        return $publicIP;
    }
}