Skip to content

Commit 205f0ef

Browse files
committed
Create a tool to effectively manage DNS record sets in Azure in Azure
Signed-off-by: Godji Fortil <godji.fortil@lexisnexisrisk.com>
1 parent 9cbbf29 commit 205f0ef

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignored configuration files
2+
./godji/azure-dns-record-set-delete/config.sh
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Azure DNS Record Set Delete
2+
This script deletes specific DNS record sets from Azure based on a keyword. It is a valuable tool for managing DNS resources in Azure.
3+
4+
## Usage
5+
* Modify config.sh to include the correct arguments.
6+
* Run main.sh: `./run.sh`
7+
8+
## Config.sh
9+
10+
```
11+
#!/bin/bash
12+
13+
#Get the subscription name
14+
SUBSCRIPTION_NAME=""
15+
16+
# Get the resource group name and zone name from the user.
17+
RESOURCE_GROUP_NAME=""
18+
DNS_ZONE_NAME=""
19+
20+
# Get the keyword to select the dns record sets.
21+
# This should be common string in all the record names you want to delete.
22+
# Examples: hpcc, hpcc2, play
23+
KEYWORD=""
24+
25+
# Types of DNS record sets
26+
RECORD_TYPES=("A" "TXT")
27+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
#Get the subscription name
4+
SUBSCRIPTION_NAME="us-lnhpccplatform-dev"
5+
6+
# Get the resource group name and zone name from the user.
7+
RESOURCE_GROUP_NAME="app-dns-prod-eastus2"
8+
DNS_ZONE_NAME="us-lnhpccplatform-dev.azure.lnrsg.io"
9+
10+
# Get the keyword to select the dns record sets.
11+
# This should be the common string in all of the record names you want to delete.
12+
# Examples: hpcc, hpcc2, play
13+
KEYWORD="hpcc2"
14+
15+
# Types of DNS record sets
16+
RECORD_TYPES=("a" "txt")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
#Include config.sh
4+
source ./config.sh
5+
6+
# #Get the subscription name
7+
# echo "Enter the subscription name:"
8+
# read SUBSCRIPTION_NAME
9+
10+
# # Get the resource group name and zone name from the user.
11+
# echo "Enter the resource group name:"
12+
# read RESOURCE_GROUP_NAME
13+
# echo "Enter the zone name:"
14+
# read DNS_ZONE_NAME
15+
16+
# # Get the keyword to select the dns record sets.
17+
# # This should be common string in all the record names you want to delete.
18+
# # Examples: hpcc, hpcc2, play
19+
# echo "Enter the keyword to select a specific record set:"
20+
# read KEYWORD
21+
22+
# # Types of DNS record sets
23+
# RECORD_TYPES=("A" "TXT")
24+
25+
# Set the subscription
26+
az account set --name $SUBSCRIPTION_NAME
27+
28+
# Initialize the array
29+
RECORD_SETS=()
30+
31+
# Find the records that end with the exact keyword
32+
find-records () {
33+
# Use process substitution to capture the output of the pipeline and set the RECORD_SETS array.
34+
while IFS= read -r record; do
35+
RECORD_SETS+=("$record")
36+
done < <(az network dns record-set list \
37+
--resource-group "$RESOURCE_GROUP_NAME" \
38+
--zone-name "$DNS_ZONE_NAME" | grep -o '"name": "[^"]*"' | \
39+
grep -w "${KEYWORD}" | \
40+
awk -F'"' '{print $4}')
41+
}
42+
43+
# Delete the record sets
44+
delete-records () {
45+
if [ ${#RECORD_SETS[*]} -gt 0 ];then
46+
echo "The following list of records will be deleted:"
47+
echo ${RECORD_SETS[*]}
48+
echo "Are you sure you want to perform this operation? (y/n):"
49+
read confirm
50+
else
51+
echo "No records found."
52+
abort
53+
fi
54+
55+
if [ ${#RECORD_SETS[*]} -gt 0 ] && [ $confirm == 'y' ];then
56+
for type in "${RECORD_TYPES[@]}"; do
57+
# Delete the record sets.
58+
for record_set in "${RECORD_SETS[@]}"; do
59+
echo "az network dns record-set $type delete \
60+
--name "$record_set" \
61+
--zone-name "$DNS_ZONE_NAME" \
62+
--resource-group "$RESOURCE_GROUP_NAME""
63+
64+
az network dns record-set $type delete \
65+
--name "$record_set" \
66+
--zone-name "$DNS_ZONE_NAME" \
67+
--resource-group "$RESOURCE_GROUP_NAME" \
68+
--yes
69+
done
70+
done
71+
72+
deleted
73+
fi
74+
}
75+
76+
# Print a success message.
77+
deleted () {
78+
echo "Successfully deleted the DNS record sets."
79+
}
80+
81+
# Print an abort message
82+
abort () {
83+
echo "The operation has been aborted."
84+
}
85+
86+
# Call the functions
87+
find-records
88+
delete-records

0 commit comments

Comments
 (0)