【问题标题】:Disable AFD Backend Pool's Backend Host with Azure Cli or Azure REST API使用 Azure Cli 或 Azure REST API 禁用 AFD 后端池的后端主机
【发布时间】:2020-09-23 01:05:26
【问题描述】:

我正在尝试更新 AFD 后端池的现有后端主机,使其状态从 Enabled 变为 Disabled

有没有办法更新 Front Door 后端池的现有后端主机?

目前,我在下面的azure front door cli docs中只能看到addlistremove

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

update 有吗?

我还查看了Azure REST API docs,但没有找到用于更新 AFD 后端池的后端主机的端点。

【问题讨论】:

    标签: azure azure-front-door


    【解决方案1】:

    我能够解决我的问题,以下脚本需要使用带有登录服务原则的 azure cli。

    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"
    

    【讨论】:

      【解决方案2】:

      我能够使用 PowerShell 实现您的要求。

      这是脚本:

      $resourceGroup1 = "前门" $frontDoor1 = "msrini"

      $afd = Get-AzFrontDoor -ResourceGroupName $resourceGroup1 -name $frontDoor1

      $loadBalancingSetting1=$afd.LoadBalancingSettings

      $afd.BackendPools.backends[0].EnabledState = "已禁用"

      $backendpool1=$afd.BackendPools

      $frontendEndpoint1 = $afd.FrontendEndpoints

      $healthProbeSetting1= $afd.HealthProbeSettings

      $routingrule1 = $afd.RoutingRules

      $backendpoolsettings1 = $afd.BackendPoolsSetting

      Set-AzFrontDoor -Name $frontDoor1 -ResourceGroupName $resourceGroup1 -RoutingRule $routingrule1 -BackendPool $backendpool1 -FrontendEndpoint $frontendEndpoint1 -LoadBalancingSetting $loadBalancingSetting1 -HealthProbeSetting $healthProbeSetting1 -BackendPoolsSetting $backendpoolsettings1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多