modules/ValidateMigration/ValidateMigration.psm1

Import-Module ((Split-Path $PSScriptRoot -Parent) + "/Log/Log.psd1")

Function ValidateMigration {
    [Parameter(Mandatory = $True)][Microsoft.Azure.Commands.Network.Models.PSLoadBalancer] $BasicLoadBalancer,
    [Parameter(Mandatory = $True)][string] $StandardLoadBalancerName,
    [Parameter(Mandatory = $true)][psobject] $scenario

    log -Message "[ValidateMigration] Initiating Validation of Migration"

    # validate the standard load balancer exists and is standard SKU
    $standardLoadBalancer = Get-AzLoadBalancer -Name $StandardLoadBalancerName -ResourceGroupName $BasicLoadBalancer.ResourceGroupName -ErrorAction SilentlyContinue

    If ($null -eq $standardLoadBalancer) {
        log -Message "[ValidateMigration] Standard Load Balancer does not exist" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer exists" -Severity Information
    }

    If ($standardLoadBalancer.Sku.Name -ne "Standard") {
        log -Message "[ValidateMigration] Standard Load Balancer is not Standard SKU" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer is Standard SKU" -Severity Information
    }

    # validate the standard load balancer has the same number of frontend IPs as the basic load balancer
    If ($standardLoadBalancer.FrontendIPConfigurations.Count -ne $BasicLoadBalancer.FrontendIPConfigurations.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of frontend IPs as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of frontend IPs as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has the same number of backend pools as the basic load balancer
    If ($standardLoadBalancer.BackendAddressPools.Count -ne $BasicLoadBalancer.BackendAddressPools.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of backend pools as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of backend pools as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has the same number of load balancing rules as the basic load balancer
    If ($standardLoadBalancer.LoadBalancingRules.Count -ne $BasicLoadBalancer.LoadBalancingRules.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of load balancing rules as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of load balancing rules as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has the same number of probes as the basic load balancer
    If ($standardLoadBalancer.Probes.Count -ne $BasicLoadBalancer.Probes.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of probes as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of probes as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has the same number of inbound NAT rules as the basic load balancer
    If ($standardLoadBalancer.InboundNatRules.Count -ne $BasicLoadBalancer.InboundNatRules.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of inbound NAT rules as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of inbound NAT rules as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has the same number of inbound NAT pools as the basic load balancer
    If ($standardLoadBalancer.InboundNatPools.Count -ne $BasicLoadBalancer.InboundNatPools.Count) {
        log -Message "[ValidateMigration] Standard Load Balancer does not have the same number of inbound NAT pools as the Basic Load Balancer" -Severity Error
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has the same number of inbound NAT pools as the Basic Load Balancer" -Severity Information
    }

    # validate the standard load balancer has outbound rules
    If ($standardLoadBalancer.OutboundRules.Count -eq 0 -and $scenario.InternalOrExternal -eq "External") {
        log -Message "[ValidateMigration] Standard Load Balancer does not have outbound rules" -Severity Warning
    }
    Else {
        log -Message "[ValidateMigration] Standard Load Balancer has outbound rules" -Severity Information
    }

    # validate the standard load balancer frontend IP addresses are the same as the basic load balancer frontend IP addresses
    If ($scenario.InternalorExternal -eq 'Internal') {
        $basicLoadBalancerFrontendIPs = $BasicLoadBalancer.FrontendIPConfigurations.properties.privateIPAddress
        $standardLoadBalancerFrontendIPs = $standardLoadBalancer.FrontendIPConfigurations.properties.privateIPAddress

        ForEach ($basicLoadBalancerFrontendIP in $basicLoadBalancerFrontendIPs) {
            If ($standardLoadBalancerFrontendIPs -notcontains $basicLoadBalancerFrontendIP) {
                log -Message "[ValidateMigration] Standard Load Balancer is missing Basic Load Balancer private IP address '$basicLoadBalancerFrontendIP'" -Severity Error
            }
            Else {
                log -Message "[ValidateMigration] Standard Load Balancer has Basic Load Balancer private IP address '$basicLoadBalancerFrontendIP'" -Severity Information
            }
        }
    }
    ElseIf ($scenario.InternalorExternal -eq 'External') {
        $basicLoadBalancerFrontendIPs = $BasicLoadBalancer.FrontendIPConfigurations.properties.publicIPAddress.id
        $standardLoadBalancerFrontendIPs = $standardLoadBalancer.FrontendIPConfigurations.properties.publicIPAddress.id

        ForEach ($basicLoadBalancerFrontendIP in $basicLoadBalancerFrontendIPs) {
            If ($standardLoadBalancerFrontendIPs -notcontains $basicLoadBalancerFrontendIP) {
                log -Message "[ValidateMigration] External Standard Load Balancer is missing Basic Load Balancer public IP address '$basicLoadBalancerFrontendIP'" -Severity Error
            }
            Else {
                log -Message "[ValidateMigration] External Standard Load Balancer has Basic Load Balancer public IP address '$basicLoadBalancerFrontendIP'" -Severity Information
            }
        }
    }

    # validate that the standard load balancer backend pools have the same membership as the basic load balancer backend pools
    $basicLoadBalancerBackendPoolsIPConfigs = $BasicLoadBalancer.BackendAddressPools.properties.backendIPConfigurations.id
    $standardLoadBalancerBackendPoolIPConfigs = $standardLoadBalancer.BackendAddressPools.properties.backendIPConfigurations.id

    ForEach ($basicLoadBalancerBackendPoolsIPConfig in $basicLoadBalancerBackendPoolsIPConfigs) {
        If ($standardLoadBalancerBackendPoolIPConfigs -notcontains $basicLoadBalancerBackendPoolsIPConfig) {
            log -Message "[ValidateMigration] Standard Load Balancer is missing Basic Load Balancer backend pool membership '$basicLoadBalancerBackendPoolsIPConfig'" -Severity Error
        }
        Else {
            log -Message "[ValidateMigration] Standard Load Balancer has Basic Load Balancer backend pool membership '$basicLoadBalancerBackendPoolsIPConfig'" -Severity Information
        }
    }
}