Skip to content

Commit

Permalink
Merge pull request #1488 from REOScotte/master
Browse files Browse the repository at this point in the history
Update rotate_azure_load_balancer_pool.json
  • Loading branch information
twerthi authored Mar 18, 2024
2 parents d42aa95 + 26c4bff commit bcc336b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions step-templates/rotate_azure_load_balancer_pool.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"Id": "947623c6-940d-4a54-a18b-c755a1035dce",
"Name": "Rotate Azure Load Balancer Pool",
"Description": "Updates all rules on an Azure load balancer to point to the next backed pool in a specified list. The current backend pool will be determined and the next pool in a provided list will become the target of all rules. If the current pool doesn't exist in the list, the first pool in the list will be used. This means that a specific pool can be chosen by specifying only a single pool.",
"Description": "Updates all rules on an Azure load balancer to point to the next backend pool in a specified list. The current backend pool will be determined and the next pool in a provided list will become the target of all rules. If the current pool doesn't exist in the list, the first pool in the list will be used. This means that a specific pool can be chosen by specifying only a single pool.",
"ActionType": "Octopus.AzurePowerShell",
"Version": 1,
"Version": 2,
"CommunityActionTemplateId": null,
"Packages": [],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"OctopusUseBundledTooling": "False",
"Octopus.Action.Script.ScriptBody": "$LoadBalancerNames = [string[]]$OctopusParameters['RotateAzureLoadBalancerPool.LoadBalancerName']\n$AvailablePools = [string[]]$OctopusParameters['RotateAzureLoadBalancerPool.AvailablePools']\n$RuleNames = [string[]]$OctopusParameters['RotateAzureLoadBalancerPool.RuleNames']\n$WhatIf = [bool]::Parse($OctopusParameters['RotateAzureLoadBalancerPool.WhatIf'])\n\nif ($null -eq $LoadBalancerNames) {\n throw 'No load balancers selected. Please select at least one load balancer.'\n} else {\n $LoadBalancerNames = $LoadBalancerNames.Split(\"`n\").Trim().Where({$_}) # Trim white space and blank lines.\n}\nif ($null -eq $AvailablePools) {\n throw 'No pools selected. Please select at least one pool.'\n} else {\n $AvailablePools = $AvailablePools.Split(\"`n\").Trim().Where({$_}) # Trim white space and blank lines.\n}\nif ($null -eq $RuleNames) {\n throw 'No rules selected. Please select at least one rule name or use an asterisk (*) to select all rules.'\n} else {\n $RuleNames = $RuleNames.Split(\"`n\").Trim().Where({$_}) # Trim white space and blank lines.\n}\n\nforeach ($loadBalancerName in $LoadBalancerNames) {\n $loadBalancer = Get-AzLoadBalancer -Name $loadBalancerName\n $resourceGroupName = $loadBalancer.ResourceGroupName\n $allPools = Get-AzLoadBalancerBackendAddressPool -LoadBalancerName $loadBalancerName -ResourceGroupName $resourceGroupName\n\n # Start by assuming no rules match\n $rules = @()\n\n # Add each distinct rule that matches one of the rule names\n foreach ($ruleName in $RuleNames) {\n $rules += $LoadBalancer.LoadBalancingRules | Where-Object {\n $_.Id.Split('/')[-1] -like $ruleName -and\n $_.Id -notin $rules.Id\n }\n }\n\n if ($rules.Count -eq 0) {\n Write-Warning \"No matching rules were found on load balancer '$loadBalancerName'.\"\n continue\n }\n\n # Update each rule with a new pool \n foreach ($rule in $rules) {\n $currentPoolName = $rule.BackendAddressPool.Id.Split('/')[-1]\n\n # This will find the next pool in the list, cycling back to the beginning if at the end. If the current pool isn't in the list,\n # its index will be -1. The next index will be zero so the first pool will be selected. \n $index = $AvailablePools.IndexOf($currentPoolName)\n $nextIndex = ($index + 1) % $AvailablePools.Count\n $newPoolName = $AvailablePools[$nextIndex]\n\n # Get the new pool to use\n if ($newPoolName -in $allPools.Name) {\n $newPool = Get-AzLoadBalancerBackendAddressPool -ResourceGroupName $resourceGroupName -LoadBalancerName $loadBalancerName -Name $newPoolName\n } else {\n throw \"Backend Pool '$newPoolName' does not exist on load balancer '$loadBalancerName'.\"\n }\n\n if ($currentPoolName -eq $newPoolName) {\n Write-Highlight \"Rule '$($rule.Name)' is already pointing to pool '$currentPoolName' on load balancer '$loadBalancerName'.\"\n } else {\n Write-Highlight \"Rule '$($rule.Name)' is pointing to pool '$currentPoolName'. Updating to pool '$newPoolName' on load balancer '$loadBalancerName'.\"\n $rule.BackendAddressPool.Id = $newPool.Id\n\n foreach ($pool in $rule.BackendAddressPools) {\n $pool.Id = $newPool.Id\n }\n }\n }\n\n if ($WhatIf) {\n Write-Highlight \"WhatIf is set to true so skipping changes on Azure for load balancer '$loadBalancerName'.\"\n } else {\n Write-Verbose \"Writing changes to Azure for load balancer '$loadBalancerName'.\"\n Set-AzLoadBalancer -LoadBalancer $loadBalancer | Out-Null\n }\n}",
"Octopus.Action.Script.ScriptBody": "$LoadBalancerNames = [string]$OctopusParameters['RotateAzureLoadBalancerPool.LoadBalancerName']\r\n$AvailablePools = [string]$OctopusParameters['RotateAzureLoadBalancerPool.AvailablePools']\r\n$RuleNames = [string]$OctopusParameters['RotateAzureLoadBalancerPool.RuleNames']\r\n$WhatIf = [bool]::Parse($OctopusParameters['RotateAzureLoadBalancerPool.WhatIf'])\r\n\r\nif ($null -eq $LoadBalancerNames) {\r\n throw 'No load balancers selected. Please select at least one load balancer.'\r\n} else {\r\n # Trim white space and blank lines and get all load balancers that match the names.\r\n $loadBalancers = $LoadBalancerNames.Split(\"`n\").Trim().Where({ $_ }) | ForEach-Object { Get-AzLoadBalancer -Name $_ }\r\n}\r\nif ($null -eq $AvailablePools) {\r\n throw 'No pools selected. Please select at least one pool.'\r\n} else {\r\n $AvailablePools = $AvailablePools.Split(\"`n\").Trim().Where({ $_ }) # Trim white space and blank lines.\r\n}\r\nif ($null -eq $RuleNames) {\r\n throw 'No rules selected. Please select at least one rule name or use an asterisk (*) to select all rules.'\r\n} else {\r\n $RuleNames = $RuleNames.Split(\"`n\").Trim().Where({ $_ }) # Trim white space and blank lines.\r\n}\r\n\r\nforeach ($loadBalancer in $loadBalancers) {\r\n $loadBalancerName = $loadBalancer.Name\r\n $resourceGroupName = $loadBalancer.ResourceGroupName\r\n $allPools = Get-AzLoadBalancerBackendAddressPool -LoadBalancerName $loadBalancerName -ResourceGroupName $resourceGroupName\r\n\r\n Write-Host \"Updating Load Balancer '$loadBalancerName'.\"\r\n\r\n # Start by assuming no rules match\r\n $rules = @()\r\n\r\n # Add each distinct rule that matches one of the rule names\r\n foreach ($ruleName in $RuleNames) {\r\n $rules += $LoadBalancer.LoadBalancingRules | Where-Object {\r\n $_.Id.Split('/')[-1] -like $ruleName -and\r\n $_.Id -notin $rules.Id\r\n }\r\n }\r\n\r\n if ($rules.Count -eq 0) {\r\n Write-Warning \"No matching rules were found on load balancer '$loadBalancerName'.\"\r\n continue\r\n }\r\n\r\n # Resolve any wildcards in AvailablePools to get a list of valid pool names.\r\n $validPoolNames = @()\r\n foreach ($name in $AvailablePools) {\r\n $validPoolNames += $allPools.Name | Where-Object { $_ -like $name }\r\n }\r\n\r\n # The same pool could match multiple wildcards so get a unique list\r\n if ($validPoolNames) {\r\n $validPoolNames = [array]($validPoolNames | Select-Object -Unique)\r\n } else {\r\n Write-Warning \"No valid pools were found on load balancer '$loadBalancerName'.\"\r\n continue\r\n }\r\n\r\n # Update each rule with a new pool \r\n foreach ($rule in $rules) {\r\n $currentPoolName = $rule.BackendAddressPool.Id.Split('/')[-1]\r\n\r\n # This will find the next pool in the list, cycling back to the beginning if at the end. If the current pool isn't in the list,\r\n # its index will be -1. The next index will be zero so the first pool will be selected. \r\n $index = $validPoolNames.IndexOf($currentPoolName)\r\n $nextIndex = ($index + 1) % $validPoolNames.Count\r\n $newPoolName = $validPoolNames[$nextIndex]\r\n\r\n # Get the new pool to use\r\n if ($newPoolName -in $allPools.Name) {\r\n $newPool = Get-AzLoadBalancerBackendAddressPool -ResourceGroupName $resourceGroupName -LoadBalancerName $loadBalancerName -Name $newPoolName\r\n } else {\r\n throw \"Backend Pool '$newPoolName' does not exist on load balancer '$loadBalancerName'.\"\r\n }\r\n\r\n if ($currentPoolName -eq $newPoolName) {\r\n Write-Highlight \"Rule '$($rule.Name)' is already pointing to pool '$currentPoolName' on load balancer '$loadBalancerName'.\"\r\n } else {\r\n Write-Highlight \"Rule '$($rule.Name)' is pointing to pool '$currentPoolName'. Updating to pool '$newPoolName' on load balancer '$loadBalancerName'.\"\r\n $rule.BackendAddressPool.Id = $newPool.Id\r\n\r\n foreach ($pool in $rule.BackendAddressPools) {\r\n $pool.Id = $newPool.Id\r\n }\r\n }\r\n }\r\n\r\n if ($WhatIf) {\r\n Write-Highlight \"WhatIf is set to true so skipping changes on Azure for load balancer '$loadBalancerName'.\"\r\n } else {\r\n Write-Verbose \"Writing changes to Azure for load balancer '$loadBalancerName'.\"\r\n Set-AzLoadBalancer -LoadBalancer $loadBalancer | Out-Null\r\n }\r\n}\r\n",
"Octopus.Action.Azure.AccountId": "#{RotateAzureLoadBalancerPool.Account}"
},
"Parameters": [
Expand All @@ -28,7 +28,7 @@
"Id": "7a8c14dd-072d-432e-9071-296381dd9cc9",
"Name": "RotateAzureLoadBalancerPool.LoadBalancerName",
"Label": "Load Balancer",
"HelpText": "The name of the load balancer to use. Multiple load balancers can be selected if they have the same pool names.\n\nEnter one load balancer per line.",
"HelpText": "The name of the load balancer to use. Multiple load balancers can be selected if they have the same pool names. Wildcards are supported.\n\nEnter one load balancer per line.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
Expand All @@ -38,7 +38,7 @@
"Id": "1295c989-efbd-4605-a362-b72ea9762c4f",
"Name": "RotateAzureLoadBalancerPool.AvailablePools",
"Label": "Available Pools",
"HelpText": "A list of pools to available pools. If multiple pools are specified, rules will be updated to use the next pool in the list. If a single pool is specified, rules will use that pool.\n\nEnter one pool per line.",
"HelpText": "A list of available pools to use. If multiple pools are specified, rules will be updated to use the next pool in the list. If a single pool is specified, rules will use that pool. Wildcards are supported.\n\nEnter one pool per line.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
Expand Down

0 comments on commit bcc336b

Please sign in to comment.