/How to update an existing rule with a new UrlPathMapConfig in an existing Azure Application Gateway
Created
February 26, 2020 12:31
-
-
Save karlospn/8228747641208e7817a9b7064d6b2f5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory=$true)][string]$gatewayName, | |
[Parameter(Mandatory=$true)][string]$resGroup, | |
[Parameter(Mandatory=$true)][string]$appName, | |
[Parameter(Mandatory=$true)][string]$protocol, | |
[Parameter(Mandatory=$true)][string]$appServiceHostname, | |
[Parameter(Mandatory=$true)][string]$pathRulePath) | |
function GetApplicationGateway($gatewayName, $resGroup) | |
{ | |
$gateway = Get-AzApplicationGateway -Name $gatewayName -ResourceGroupName $resGroup -ErrorAction Stop | |
if($null -eq $gateway) | |
{ | |
Throw "Gateway is null" | |
} | |
return $gateway | |
} | |
function GetBackendPool($gw) | |
{ | |
$backend = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $gw -ErrorAction Stop | |
if($null -eq $backend) | |
{ | |
Throw "BackendPool is null" | |
} | |
return $backend | |
} | |
function AddProbeToApplicationGateway($gw, $protocol, $appServiceHostname) | |
{ | |
$probeName = $appName + "_probe" | |
$probeHc = New-AzApplicationGatewayProbeHealthResponseMatch -Body null -StatusCode "200-399" -ErrorAction Stop | |
Add-AzApplicationGatewayProbeConfig ` | |
-ApplicationGateway $gw ` | |
-Name $probeName ` | |
-Protocol $protocol ` | |
-HostName $appServiceHostname ` | |
-Path "/health.svc/health" ` | |
-Interval 30 ` | |
-Timeout 50 ` | |
-UnhealthyThreshold 5 ` | |
-Match $probeHc ` | |
-ErrorAction Stop | Out-Null | |
return Get-AzApplicationGatewayProbeConfig -ApplicationGateway $gw -Name $probeName | |
} | |
function AddHttpSettingsToApplicationGateway($gw, $protocol, $appServiceHostname, $appProbe) | |
{ | |
$httpSettingName = $appName + "_HttpSetting" | |
Add-AzApplicationGatewayBackendHttpSetting ` | |
-ApplicationGateway $gw ` | |
-Name $httpSettingName ` | |
-Port 80 ` | |
-Protocol $protocol ` | |
-CookieBasedAffinity "Disabled" ` | |
-path "/" ` | |
-hostName $appServiceHostname ` | |
-Probe $appProbe ` | |
-ErrorAction Stop | Out-Null | |
$settings = Get-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $gw | Where-Object { $_.Name -eq $httpSettingName } | |
if($null -eq $settings) | |
{ | |
Throw "HttpSettings not found" | |
} | |
return $settings | |
} | |
function UpdateUrlPathMapToApplicationGateway($gw, $pathRulePath, $backendPool, $httpSettings) | |
{ | |
$prcName = $appName + "_pathRuleConfig" | |
$newPathRule = New-AzApplicationGatewayPathRuleConfig ` | |
-Name $prcName ` | |
-Paths $pathRulePath ` | |
-BackendAddressPool $backendPool ` | |
-BackendHttpSettings $httpSettings ` | |
-ErrorAction Stop | |
$pathMap = Get-AzApplicationGatewayUrlPathMapConfig -ApplicationGateway $gw -Name "ase_rule" -ErrorAction Stop | |
$pathRules = $pathmap.PathRules.ToArray() | |
$pathRules += $newPathRule | |
Set-AzApplicationGatewayUrlPathMapConfig ` | |
-ApplicationGateway $gw ` | |
-Name $pathMap.Name ` | |
-PathRules $pathRules ` | |
-DefaultBackendAddressPool $backendPool ` | |
-DefaultBackendHttpSettings $httpSettings ` | |
-ErrorAction Stop | |
} | |
function main() | |
{ | |
try { | |
$gw = GetApplicationGateway $gatewayName $resGroup | |
$backendPool = GetBackendPool $gw | |
$appProbe = AddProbeToApplicationGateway $gw $protocol $appServiceHostname | |
$httpSettings = AddHttpSettingsToApplicationGateway $gw $protocol $appServiceHostname $appProbe | |
UpdateUrlPathMapToApplicationGateway $gw $pathRulePath $backendPool $httpSettings | |
Set-AzApplicationGateway -ApplicationGateway $gw -ErrorAction Stop | |
} | |
catch { | |
Write-Error $_.Exception | |
} | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment