Skip to content

Commit 61ae2f9

Browse files
committed
Code enhancement
Signed-off-by: Godji Fortil <godji.fortil@lexisnexisrisk.com>
1 parent 29d8172 commit 61ae2f9

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

godji/azure-dns-record-set-delete/config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ RESOURCE_GROUP_NAME=""
88
DNS_ZONE_NAME=""
99

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

1515
# Types of DNS record sets
16-
RECORD_TYPES=("a" "txt")
16+
RECORD_TYPES=("a" "txt")

godji/azure-dns-record-set-delete/main.sh

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,37 @@ source ./config.sh
2525
# Set the subscription
2626
az account set --name $SUBSCRIPTION_NAME
2727

28-
delete-records () {
29-
# Get the list of record sets to delete
30-
record_sets=($(az network dns record-set list \
28+
RECORD_SETS=()
29+
30+
# Find the records that end with the specified keyword
31+
find-records () {
32+
# Filtering to only return the records that end exactly with the specified keyword
33+
for record in "$(az network dns record-set list \
3134
--resource-group $RESOURCE_GROUP_NAME \
32-
--zone-name $DNS_ZONE_NAME | grep name | grep ${KEYWORD} | awk '{print $2}' | sed 's/"//g' | sed 's/,//g'))
33-
echo $record_sets
34-
if [ ${#record_sets[*]} -gt 0 ];then
35+
--zone-name $DNS_ZONE_NAME | grep -o '"name": "[^"]*"' | \
36+
grep "${KEYWORD}" | \
37+
awk -F'"' '{print $4}')"; do
38+
if [[ "$record" == *${KEYWORD} ]]; then
39+
RECORD_SETS+=("$record")
40+
fi
41+
done
42+
}
43+
44+
delete-records () {
45+
if [ ${#RECORD_SETS[*]} -gt 0 ];then
3546
echo "The following list of records will be deleted:"
36-
echo ${record_sets[*]}
47+
echo ${RECORD_SETS[*]}
3748
echo "Are you sure you want to perform this operation? (y/n):"
3849
read confirm
3950
else
4051
echo "No records found."
4152
abort
4253
fi
4354

44-
if [ ${#record_sets[*]} -gt 0 ] && [ $confirm == 'y' ];then
55+
if [ ${#RECORD_SETS[*]} -gt 0 ] && [ $confirm == 'y' ];then
4556
for type in "${RECORD_TYPES[@]}"; do
4657
# Delete the record sets.
47-
for record_set in "${record_sets[@]}"; do
58+
for record_set in "${RECORD_SETS[@]}"; do
4859
echo "az network dns record-set $type delete \
4960
--name "$record_set" \
5061
--zone-name "$DNS_ZONE_NAME" \
@@ -62,6 +73,8 @@ delete-records () {
6273
fi
6374
}
6475

76+
find-records
77+
6578
deleted () {
6679
# Print a success message.
6780
echo "Successfully deleted the DNS record sets."

0 commit comments

Comments
 (0)