Search code examples
azureazure-front-door

Disable AFD Backend Pool's Backend Host with Azure Cli or Azure REST API


Im trying to update an existing backend host of a AFD backend pool to have its status from Enabled to Disabled.

Is there a way to update existing backend host of Front Door backend pools?

currently, i can only see add, list and remove in the following azure front door cli docs:

  • az network front-door backend-pool backend add
  • az network front-door backend-pool backend list
  • az network front-door backend-pool backend remove

Is there one for update?

I've also looked into the Azure REST API docs and have not found an endpoint to update a backend host of AFD backend pools.

azure front door update backend


Solution

  • I was able to resolve my issue, the below script requires using azure cli with a logged in service principle.

    Param(
      [string]$desiredState, #"Enabled" or "Disabled"
      [string]$frontDoorName,
      [string]$resourceGroupName,
      [string]$targetBackendPool,
      [string]$targetBackendHost
    )
    
    # Get Access Token
    $token = az account get-access-token | ConvertFrom-Json
    $accessToken = $token.accessToken
    $subscriptionId = $token.subscription
    $tenantId = $token.tenant
    
    $uri = "https://management.azure.com/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroupName)/providers/Microsoft.Network/frontDoors/$($frontDoorName)?api-version=2019-05-01"
    $headers = @{ "Authorization" = "Bearer $accessToken" }
    $contentType = "application/json"
    
    # Get AFD Configuration.
    Write-Host "Invoking Azure REST API to get AFD configuration"
    $afdConfig = Invoke-WebRequest -method get -uri $uri -headers $headers | ConvertFrom-Json
    
    # Edit AFD Configuration to toggle backend host state
    Write-Host "Checking BackendHost: $targetBackendHost In Backend Pool: $targetBackendPool"
    
    foreach ($backendPool in $afdConfig.properties.backendPools) {
        if ($backendPool.name -eq $targetBackendPool) {
            foreach ($backends in $backendPool.properties.backends) {
                if ($backends.address -eq $targetBackendHost) {
                    $currentState = $backends.enabledState
                    Write-Host "Current State of $targetBackendHost is $currentState"
                    if ($currentState -eq $desiredState) {
                        Write-Host "$targetBackendHost is already in the desired state of $desiredState"
                        exit
                    }
                    $backends.enabledState = $desiredState
                    Write-Host "$targetBackendHost Updated to $desiredState."
               }
            }
        }
    }
    
    $updateRequest = $afdConfig | ConvertTo-Json -Depth 100
    
    # Update AFD Configuration.
    Write-Host "Invoking Azure REST API to update AFD configuration"
    Invoke-WebRequest -method put -uri $uri -headers $headers -ContentType $contentType -body $updateRequest
    
    # Poll current state until the change has been fully propogated in azure
    Write-Host "Polling AFD until update is complete."
    do {
        $afdConfig = Invoke-WebRequest -method get -uri $uri -headers $headers | ConvertFrom-Json
    
        foreach ($backendPool in $afdConfig.properties.backendPools) {
            if ($backendPool.name -eq $targetBackendPool) {
                foreach ($backends in $backendPool.properties.backends) {
                    if ($backends.address -eq $targetBackendHost) {
                        $currentState = $backends.enabledState
                    }
                }
            }
        }
        Write-Host "$targetBackendHost is currently in $currentState"
        Start-Sleep -Seconds 1
    } until ($currentState -eq $desiredState)
    
    Write-Host "$targetBackendHost has successfully been updated to $desiredState"