diff --git a/godji/azure-dns-record-set-delete/config.sh b/godji/azure-dns-record-set-delete/config.sh index 0afecb4..b58a487 100644 --- a/godji/azure-dns-record-set-delete/config.sh +++ b/godji/azure-dns-record-set-delete/config.sh @@ -8,9 +8,9 @@ RESOURCE_GROUP_NAME="" DNS_ZONE_NAME="" # Get the keyword to select the dns record sets. -# This should be common string in all the record names you want to delete. +# This should be the common string in all of the record names you want to delete. # Examples: hpcc, hpcc2, play KEYWORD="" # Types of DNS record sets -RECORD_TYPES=("a" "txt") +RECORD_TYPES=("a" "txt") \ No newline at end of file diff --git a/godji/azure-dns-record-set-delete/main.sh b/godji/azure-dns-record-set-delete/main.sh index d7dceba..68b02ea 100755 --- a/godji/azure-dns-record-set-delete/main.sh +++ b/godji/azure-dns-record-set-delete/main.sh @@ -25,15 +25,26 @@ source ./config.sh # Set the subscription az account set --name $SUBSCRIPTION_NAME -delete-records () { - # Get the list of record sets to delete - record_sets=($(az network dns record-set list \ +RECORD_SETS=() + +# Find the records that end with the specified keyword +find-records () { + # Filtering to only return the records that end exactly with the specified keyword + for record in "$(az network dns record-set list \ --resource-group $RESOURCE_GROUP_NAME \ - --zone-name $DNS_ZONE_NAME | grep name | grep ${KEYWORD} | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')) - echo $record_sets - if [ ${#record_sets[*]} -gt 0 ];then + --zone-name $DNS_ZONE_NAME | grep -o '"name": "[^"]*"' | \ + grep "${KEYWORD}" | \ + awk -F'"' '{print $4}')"; do + if [[ "$record" == *${KEYWORD} ]]; then + RECORD_SETS+=("$record") + fi + done +} + +delete-records () { + if [ ${#RECORD_SETS[*]} -gt 0 ];then echo "The following list of records will be deleted:" - echo ${record_sets[*]} + echo ${RECORD_SETS[*]} echo "Are you sure you want to perform this operation? (y/n):" read confirm else @@ -41,10 +52,10 @@ delete-records () { abort fi - if [ ${#record_sets[*]} -gt 0 ] && [ $confirm == 'y' ];then + if [ ${#RECORD_SETS[*]} -gt 0 ] && [ $confirm == 'y' ];then for type in "${RECORD_TYPES[@]}"; do # Delete the record sets. - for record_set in "${record_sets[@]}"; do + for record_set in "${RECORD_SETS[@]}"; do echo "az network dns record-set $type delete \ --name "$record_set" \ --zone-name "$DNS_ZONE_NAME" \ @@ -62,6 +73,8 @@ delete-records () { fi } +find-records + deleted () { # Print a success message. echo "Successfully deleted the DNS record sets."