Azure cli and a while loop to check for status

MrFlinstone 541 Reputation points
2021-01-17T15:03:39.13+00:00

have got an azure asynch operation which returns one of 3 statuses, the code is written in bash. Please see https://video2.skills-academy.com/en-us/rest/api/sql/failovergroups/failover

Succeeded  
Failed  
Canceled  

The issue with the code snippet below is that, it is not waiting for any of the 3 statuses above to be returned before it exits, sometimes the process runs very fast and sometimes its very slow. Ideally I feel that a loop is required where it will wait for any of the 3 statues above to be returned and then exit.

steps:  
  - task: AzureCLI@1  
    displayName: Failover  
    inputs:  
      azureSubscription: Sub  
      scriptLocation: inlineScript  
      inlineScript: |  
  
        URL=$(grep Azure-AsyncOperation out.log | sed "s/'//g" | sed "s/Azure-AsyncOperation: //g")  
        STATUS=$(az rest --method GET --url ${URL} --query 'status' -o tsv)  
         
        if [ ! ${STATUS} == "Succeeded" ]  
        then  
          echo "Operation did not succeed"  
          echo "Operation status is ${STATUS}"  
          exit 1                   
        fi  
  
        echo "Operation succeeded"  

While the status variable is not null or populated

If the status is == Failed then exit 1 and echo message saying failed
If the status is == cancelled then exit 1 and echo message saying failed
If the status is == Succeeded then exit 0 and echo message saying the processed successfully completed.

How can I achieve this in bash ?

Azure SQL Database
{count} votes

1 answer

Sort by: Most helpful
  1. Navtej Singh Saini 4,226 Reputation points Microsoft Employee
    2021-01-28T20:47:26.7+00:00

    @MrFlinstone

    From the looks of it you are trying to do as documented here. If that is the case you are right in saying the Loop is required to find out the details and the operation with URL to complete. Here is the sample code we wrote for the same:

    STATUS=$(az rest --method GET --url ${URL} --query 'status' -o tsv)  
    while [ -z $STATUS ] #-z is empty string, may need to modify if there is also a Status = pending option  
        do  
            echo "waiting for result"  
            sleep 1 #delay in seconds  
            #if pending statuses happen, poll the API endpoint again to get the latest status  
        done  
    if [ ! ${STATUS} == "Succeeded" ]  
      then  
        echo "Operation did not succeed"  
        echo "Operation status is ${STATUS}"  
        exit 1  
      else  
        #handle Suceeded status  
      fi  
    

    If this still doesn't work please get back to us with the URL and .log file data to TS it further.

    Regards
    Navtej S

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.