Skip to content

Commit

Permalink
Code enhancement
Browse files Browse the repository at this point in the history
Signed-off-by: Godji Fortil <[email protected]>
  • Loading branch information
gfortil committed Jun 28, 2024
1 parent 29d8172 commit 61ae2f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions godji/azure-dns-record-set-delete/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
31 changes: 22 additions & 9 deletions godji/azure-dns-record-set-delete/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,37 @@ 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
echo "No records found."
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" \
Expand All @@ -62,6 +73,8 @@ delete-records () {
fi
}

find-records

deleted () {
# Print a success message.
echo "Successfully deleted the DNS record sets."
Expand Down

0 comments on commit 61ae2f9

Please sign in to comment.