Skip to content

Create a tool to effectively manage DNS record sets in Azure in Azure. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignored configuration files
./godji/azure-dns-record-set-delete/config.sh
27 changes: 27 additions & 0 deletions godji/azure-dns-record-set-delete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Azure DNS Record Set Delete
This script deletes specific DNS record sets from Azure based on a keyword. It is a valuable tool for managing DNS resources in Azure.

## Usage
* Modify config.sh to include the correct arguments.
* Run main.sh: `./run.sh`

## Config.sh

```
#!/bin/bash

#Get the subscription name
SUBSCRIPTION_NAME=""

# Get the resource group name and zone name from the user.
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.
# Examples: hpcc, hpcc2, play
KEYWORD=""

# Types of DNS record sets
RECORD_TYPES=("A" "TXT")
```
16 changes: 16 additions & 0 deletions godji/azure-dns-record-set-delete/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

#Get the subscription name
SUBSCRIPTION_NAME="us-lnhpccplatform-dev"

# Get the resource group name and zone name from the user.
RESOURCE_GROUP_NAME="app-dns-prod-eastus2"
DNS_ZONE_NAME="us-lnhpccplatform-dev.azure.lnrsg.io"

# Get the keyword to select the dns record sets.
# This should be the common string in all of the record names you want to delete.
# Examples: hpcc, hpcc2, play
KEYWORD="hpcc2"

# Types of DNS record sets
RECORD_TYPES=("a" "txt")
88 changes: 88 additions & 0 deletions godji/azure-dns-record-set-delete/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

#Include config.sh
source ./config.sh

# #Get the subscription name
# echo "Enter the subscription name:"
# read SUBSCRIPTION_NAME

# # Get the resource group name and zone name from the user.
# echo "Enter the resource group name:"
# read RESOURCE_GROUP_NAME
# echo "Enter the zone name:"
# read 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.
# # Examples: hpcc, hpcc2, play
# echo "Enter the keyword to select a specific record set:"
# read KEYWORD

# # Types of DNS record sets
# RECORD_TYPES=("A" "TXT")

# Set the subscription
az account set --name $SUBSCRIPTION_NAME

# Initialize the array
RECORD_SETS=()

# Find the records that end with the exact keyword
find-records () {
# Use process substitution to capture the output of the pipeline and set the RECORD_SETS array.
while IFS= read -r record; do
RECORD_SETS+=("$record")
done < <(az network dns record-set list \
--resource-group "$RESOURCE_GROUP_NAME" \
--zone-name "$DNS_ZONE_NAME" | grep -o '"name": "[^"]*"' | \
grep -w "${KEYWORD}" | \
awk -F'"' '{print $4}')
}

# Delete the record sets
delete-records () {
if [ ${#RECORD_SETS[*]} -gt 0 ];then
echo "The following list of records will be deleted:"
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
for type in "${RECORD_TYPES[@]}"; do
# Delete the record sets.
for record_set in "${RECORD_SETS[@]}"; do
echo "az network dns record-set $type delete \
--name "$record_set" \
--zone-name "$DNS_ZONE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME""

az network dns record-set $type delete \
--name "$record_set" \
--zone-name "$DNS_ZONE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--yes
done
done

deleted
fi
}

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

# Print an abort message
abort () {
echo "The operation has been aborted."
}

# Call the functions
find-records
delete-records