modules/LoadBalacingRulesMigration/LoadBalacingRulesMigration.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Load Modules
Import-Module ((Split-Path $PSScriptRoot -Parent) + "\Log\Log.psd1")
function LoadBalacingRulesMigration {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $True)][Microsoft.Azure.Commands.Network.Models.PSLoadBalancer] $BasicLoadBalancer,
        [Parameter(Mandatory = $True)][Microsoft.Azure.Commands.Network.Models.PSLoadBalancer] $StdLoadBalancer
    )
    log -Message "[LoadBalacingRulesMigration] Initiating LoadBalacing Rules Migration"
    $loadBalancingRules = $BasicLoadBalancer.LoadBalancingRules
    foreach ($loadBalancingRule in $loadBalancingRules) {
        log -Message "[LoadBalacingRulesMigration] Adding LoadBalacing Rule $($loadBalancingRule.Name) to Standard Load Balancer"

        try {
            $ErrorActionPreference = 'Stop'
            $loadBalancingRuleConfig = @{
                Name                    = $loadBalancingRule.Name
                Protocol                = $loadBalancingRule.Protocol
                FrontendPort            = $loadBalancingRule.FrontendPort
                BackendPort             = $loadBalancingRule.BackendPort
                IdleTimeoutInMinutes    = $loadBalancingRule.IdleTimeoutInMinutes
                EnableFloatingIP        = $loadBalancingRule.EnableFloatingIP
                LoadDistribution        = $loadBalancingRule.LoadDistribution
                DisableOutboundSnat     = $loadBalancingRule.DisableOutboundSnat
                EnableTcpReset          = $loadBalancingRule.EnableTcpReset
                FrontendIPConfiguration = (Get-AzLoadBalancerFrontendIpConfig -LoadBalancer $StdLoadBalancer -Name ($loadBalancingRule.FrontendIpConfiguration.Id).split('/')[-1])
                BackendAddressPool      = (Get-AzLoadBalancerBackendAddressPool -LoadBalancer $StdLoadBalancer -Name ($loadBalancingRule.BackendAddressPool.Id).split('/')[-1])
                Probe                   = (Get-AzLoadBalancerProbeConfig -LoadBalancer $StdLoadBalancer -Name ($loadBalancingRule.Probe.Id).split('/')[-1])
            }
            $StdLoadBalancer | Add-AzLoadBalancerRuleConfig @loadBalancingRuleConfig > $null
        }
        catch {
            $message = @"
                [LoadBalacingRulesMigration] An error occured when adding Load Balancing Rule '$($loadBalancingRule.Name)' to new Standard load
                balancer '$($StdLoadBalancer.Name)'. To recover, address the following error, delete the standard LB, redeploy the Basic
                load balancer from the backup 'ARMTemplate-$($BasicLoadBalancer.Name)-$($BasicLoadBalancer.ResourceGroupName)...' file, add backend
                pool membership back (see the backup '$('State-' + $BasicLoadBalancer.Name + '-' + $BasicLoadBalancer.ResourceGroupName + '...')' state
                file for original pool membership), and retry the migration. Error: $_
"@

            log "Error" $message
            Exit
        }
    }
    log -Message "[LoadBalacingRulesMigration] Saving Standard Load Balancer $($StdLoadBalancer.Name)"

    try {
        $ErrorActionPreference = 'Stop'
        Set-AzLoadBalancer -LoadBalancer $StdLoadBalancer > $null
    }
    catch {
        $message = @"
        [LoadBalacingRulesMigration] An error occured when adding Load Balancing Rules configuration to new Standard load
        balancer '$($StdLoadBalancer.Name)'. To recover address the following error, and try again specifying the
        -FailedMigrationRetryFilePath parameter and Basic Load Balancer backup State file located either in this directory
        or the directory specified with -RecoveryBackupPath. `nError message: $_
"@

        log "Error" $message
        Exit
    }
    log -Message "[LoadBalacingRulesMigration] LoadBalacing Rules Migration Completed"
}

Export-ModuleMember -Function LoadBalacingRulesMigration