JulianHayward/AzAPICall

Results with "NextLink" will stuck in a loop

Closed this issue · 1 comments

Example of listing more than 100 private DNS zones will stuck in an endless loop:
https://docs.microsoft.com/en-us/rest/api/dns/privatedns/privatezones/list

Initial requests:
image

Next link request:
image

Unfortunately, a ":443" is within the NextLink response and then won't match the "$arrayAzureManagementEndPointUrls":

if ($arrayAzureManagementEndPointUrls | Where-Object { $uri -match $_ }) {
    $targetEndpoint = "ManagementAPI"
    $bearerToUse = $htBearerAccessToken.AccessTokenManagement
}
else {
    $targetEndpoint = "MSGraphAPI"
    $bearerToUse = $htBearerAccessToken.AccessTokenMSGraph
}

Regarding to this, no bearer token will be used and the variable "$bearerToUse" is empty.
The request will got a authorization error regarding the missing bearer token.

Quick solution is to check and replace the ":443":

if ($azAPIRequestConvertedFromJson.nextLink) {
  $isMore = $true
  if ($uri -eq $azAPIRequestConvertedFromJson.nextLink) {
      if ($restartDueToDuplicateNextlinkCounter -gt 3) {
          Write-Host " $currentTask restartDueToDuplicateNextlinkCounter: #$($restartDueToDuplicateNextlinkCounter) - Please report this error/exit"
          Throw "Error - check the last console output for details"
      }
      else {
          $restartDueToDuplicateNextlinkCounter++
          Write-Host "nextLinkLog: uri is equal to nextLinkUri"
          Write-Host "nextLinkLog: uri: $uri"
          Write-Host "nextLinkLog: nextLinkUri: $($azAPIRequestConvertedFromJson.nextLink)"
          Write-Host "nextLinkLog: re-starting (#$($restartDueToDuplicateNextlinkCounter)) '$currentTask'"
          $apiCallResultsCollection = [System.Collections.ArrayList]@()
          $uri = $initialUri
          Start-Sleep -Seconds 1
          createBearerToken -targetEndPoint $targetEndpoint
          Start-Sleep -Seconds 1
      }
  }
  else {
      $uri = $azAPIRequestConvertedFromJson.nextLink
      if($uri -match ":443"){
          $uri = $uri -replace ":443"
      }
  }
  if ($htParameters.DebugAzAPICall -eq $true) { Write-Host "   DEBUG: nextLink: $Uri" -ForegroundColor $debugForeGroundColor }
  }

i contact the PG and see if they change it or if we need to implement a workaround for it