Search code examples
bashdevopscloudflare

How to skip error cloudflare and continue with other resoucre


I am trying to run and generate resource for cloudflare but there is error to be able to run all the resource in the array for the script.

The error is :

FATA[0006] Could not route to /client/v4/accounts/access/certificates, perhaps your object identifier is invalid? (7003) 

The resource that failed is cloudflare_access_mutual_tls_certificate

The script array is just minimum here, but would like the script to skip error and continue with the next in the array if one of the resource in the array failed.

#!/bin/bash


set -e
examplearray=("cloudflare_access_application" "cloudflare_access_bookmark" "cloudflare_access_ca_certificate" "cloudflare_access_group" 
"cloudflare_access_identity_provider" "cloudflare_zone_settings_override")

for example in ${examplearray[@]}; do
    echo Extracting $example ...
    cf-terraforming generate --resource-type $example > $example.txt
    content=$(cat $example.txt)

done

Solution

  • Just continue in the case of error.

    The array seems entirely unnecessary (just enumerate the values directly as arguments to the for loop) but here's a light refactoring.

    #!/bin/bash
    
    set -e
    examplearray=("cloudflare_access_application" "cloudflare_access_bookmark" "cloudflare_access_ca_certificate" "cloudflare_access_group" 
    "cloudflare_access_identity_provider" "cloudflare_zone_settings_override")
    
    # notice quotes for robustness
    for example in "${examplearray[@]}"; do
        # diagnostics should go to stderr
        echo "Extracting $example ..." >&2
        content=$(cf-terraforming generate --resource-type "$example") || continue
        echo "$content" > "$example".txt
    done