Search code examples
bashazureazure-cliexpress-router

Existence of a resource within a resource group


How to check the existence of a express route within a resource group. I have list of resource group but not all resource groups have express routes within them. What is the condition that needs to be written to check if there is a express route inside each resource group

rgList ==> List of all the resource groups in a specific subscription

erList ==> List of all the express routes in a specific subscription

rgList="$(az group list --subscription "sub1" --query [].name --output tsv)"
erList="$(az network express-route list --subscription "sub1" --query [].name --output tsv)"
for i in $rgList; do
    if [[ $(az network express-route list -g "$i" --subscription "sub-network-central-services" --query "[].name | length(@))" > 0 ]]
    then
        for j in $erList; do
            hubname+="$(az network express-route auth list --circuit-name "$j" --resource-group "$i" --query [].authorizations[].name --output tsv)"
        done
    fi
done

I am getting the below error bash: syntax error near unexpected token `done'


Solution

  • As mentioned above, yes there were some syntactical errors while testing in our local environment we have fixed those errors & we have modified the above script as well.

    The Modified script will pull the authorization names & Express-route circuit name.

    If want to get the list of resource groups which has express-route circuit you can directly refer to Alternative Script in the My solution.

    Here is the Modified Bash Script:

    hubname=""
    rglist=$(az group list --subscription "<subNameorSubId>" --query [].name --output tsv)
    erList=$(az network express-route list --subscription "<subNameorSubId>" --query [].name --output tsv)
    for i in $rglist;
    do
    if [[ $(az network express-route list -g $i --subscription "<subNameorSubId>" --query "[].name | length(@)") > 0 ]]
        then
            for j in $erList;
            do
                 hubname+=$(az network express-route auth list --circuit-name $j --resource-group $i --query [].name --output tsv)
                 echo $j,$hubname
            done
        fi
    done
    

    Here is the sample output screenshot for reference:

    enter image description here

    Alternatively, you can use the below bash script this will give you the list of resource groups if an express-route circuit is present in it.

    Alternative Script:

     rgList=$(az group list --subscription "<SubNameorSubId>" --query [].name --output tsv)
        for i in $rgList;do
            resourcelist=$(az resource list -g $i --query [].type -o tsv)
            for j in $resourcelist;do
                if [[ $j ==  "Microsoft.Network/expressRouteCircuits" ]]
                then
                    echo $i
                fi
            done
        done
    

    Here is the Sample Output for reference:

    enter image description here