Created
April 27, 2019 14:03
-
-
Save selcukusta/18f16c8c4c2c8162ae2f4be65edb08f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
| [string]$resourceGroupName = "bootcamp-rg", | |
| [string]$trafficManagerProfile = "bootcamp-traffic-manager", | |
| [string]$tagName = "multi-instance" | |
| ) | |
| $servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" | |
| Connect-AzAccount ` | |
| -ServicePrincipal ` | |
| -TenantId $servicePrincipalConnection.TenantId ` | |
| -ApplicationId $servicePrincipalConnection.ApplicationId ` | |
| -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | |
| $active_routes = New-Object "System.Collections.Generic.List[string]" | |
| $endpoints = Get-AzTrafficManagerProfile $resourceGroupName $trafficManagerProfile | |
| foreach ($profile in $endpoints.Endpoints) { | |
| $target = $profile.Target | |
| Write-Output "$target is added to active route table." | |
| $active_routes.Add($target) | |
| } | |
| $azContainerInstances = (Get-AzContainerGroup $resourceGroupName) | Select-Object Fqdn, Tags | |
| $containerInstances = New-Object "System.Collections.Generic.Dictionary[string,bool]" | |
| foreach ($instance in $azContainerInstances) { | |
| $fqdn = $instance.Fqdn | |
| $taggedAsScale = $instance.Tags.ContainsKey($tagName) -and $instance.Tags[$tagName] -eq "true" | |
| $containerInstances.Add($fqdn, $taggedAsScale) | |
| Write-Output "'$fqdn' is tagged as '$tagName=$taggedAsScale'" | |
| } | |
| foreach ($fqdn in $containerInstances.Keys) { | |
| if ($containerInstances[$fqdn] -and !$active_routes.Contains($fqdn)) { | |
| New-AzTrafficManagerEndpoint ` | |
| -ProfileName $trafficManagerProfile ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Type ExternalEndpoints ` | |
| -Name $fqdn ` | |
| -Target $fqdn ` | |
| -Weight 1 ` | |
| -EndpointStatus Enabled | |
| Write-Output "$fqdn is ADDED to traffic manager." | |
| } | |
| elseif (!$containerInstances[$fqdn] -and $active_routes.Contains($fqdn)) { | |
| Remove-AzTrafficManagerEndpoint ` | |
| -ProfileName $trafficManagerProfile ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Type ExternalEndpoints ` | |
| -Name $fqdn ` | |
| -Force | |
| Write-Output "$fqdn is REMOVED from traffic manager." | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment