Skip to content

Commit 2030459

Browse files
authored
Merge pull request #4547 from eastonman/master
dnsapi(huaweicloud): Add retry count to avoid infinite loop when things go wrong
2 parents ce629e8 + 1f777a9 commit 2030459

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

dnsapi/dns_huaweicloud.sh

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,59 @@ dns_huaweicloud_rm() {
9898
fi
9999
_debug "Zone ID is:" "${zoneid}"
100100

101+
record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
102+
_recursive_rm_record "${token}" "${fulldomain}" "${zoneid}" "${record_id}"
103+
ret="$?"
104+
if [ "${ret}" != "0" ]; then
105+
_err "dns_api(dns_huaweicloud): Error removing record."
106+
return 1
107+
fi
108+
109+
return 0
110+
}
111+
112+
################### Private functions below ##################################
113+
114+
# _recursive_rm_record
115+
# remove all records from the record set
116+
#
117+
# _token=$1
118+
# _domain=$2
119+
# _zoneid=$3
120+
# _record_id=$4
121+
#
122+
# Returns 0 on success
123+
_recursive_rm_record() {
124+
_token=$1
125+
_domain=$2
126+
_zoneid=$3
127+
_record_id=$4
128+
129+
# Most likely to have problems will huaweicloud side if more than 50 attempts but still cannot fully remove the record set
130+
# Maybe can be removed manually in the dashboard
131+
_retry_cnt=50
132+
101133
# Remove all records
102134
# Therotically HuaweiCloud does not allow more than one record set
103135
# But remove them recurringly to increase robusty
104-
while [ "${record_id}" != "0" ]; do
136+
137+
while [ "${_record_id}" != "0" ] && [ "${_retry_cnt}" != "0" ]; do
105138
_debug "Removing Record"
106-
_rm_record "${token}" "${zoneid}" "${record_id}"
107-
record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
139+
_retry_cnt=$((_retry_cnt - 1))
140+
_rm_record "${_token}" "${_zoneid}" "${_record_id}"
141+
_record_id="$(_get_recordset_id "${_token}" "${_domain}" "${_zoneid}")"
142+
_debug2 "Checking record exists: record_id=${_record_id}"
108143
done
144+
145+
# Check if retry count is reached
146+
if [ "${_retry_cnt}" = "0" ]; then
147+
_debug "Failed to remove record after 50 attempts, please try removing it manually in the dashboard"
148+
return 1
149+
fi
150+
109151
return 0
110152
}
111153

112-
################### Private functions below ##################################
113-
114154
# _get_zoneid
115155
#
116156
# _token=$1
@@ -124,7 +164,7 @@ _get_zoneid() {
124164

125165
i=1
126166
while true; do
127-
h=$(printf "%s" "${_domain_string}" | cut -d . -f $i-100)
167+
h=$(printf "%s" "${_domain_string}" | cut -d . -f "$i"-100)
128168
if [ -z "$h" ]; then
129169
#not valid
130170
return 1
@@ -135,11 +175,11 @@ _get_zoneid() {
135175
if _contains "${response}" '"id"'; then
136176
zoneidlist=$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
137177
zonenamelist=$(echo "${response}" | _egrep_o "\"name\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
138-
_debug2 "Return Zone ID(s):" "${zoneidlist}"
139-
_debug2 "Return Zone Name(s):" "${zonenamelist}"
178+
_debug2 "Returned Zone ID(s):" "${zoneidlist}"
179+
_debug2 "Returned Zone Name(s):" "${zonenamelist}"
140180
zoneidnum=0
141181
zoneidcount=$(echo "${zoneidlist}" | grep -c '^')
142-
_debug "Retund Zone ID(s) Count:" "${zoneidcount}"
182+
_debug "Returned Zone ID(s) Count:" "${zoneidcount}"
143183
while [ "${zoneidnum}" -lt "${zoneidcount}" ]; do
144184
zoneidnum=$(_math "$zoneidnum" + 1)
145185
_zoneid=$(echo "${zoneidlist}" | sed -n "${zoneidnum}p")
@@ -206,28 +246,24 @@ _add_record() {
206246
\"type\": \"TXT\",
207247
\"ttl\": 1,
208248
\"records\": [
209-
${_exist_record},
210-
\"\\\"${_txtvalue}\\\"\"
249+
${_exist_record},\"\\\"${_txtvalue}\\\"\"
211250
]
212251
}"
213252
fi
214253

215254
_record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
216255
_debug "Record Set ID is:" "${_record_id}"
217256

218-
# Remove all records
219-
while [ "${_record_id}" != "0" ]; do
220-
_debug "Removing Record"
221-
_rm_record "${_token}" "${zoneid}" "${_record_id}"
222-
_record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
223-
done
224-
225257
# Add brand new records with all old and new records
226258
export _H2="Content-Type: application/json"
227259
export _H1="X-Auth-Token: ${_token}"
228260

229261
_debug2 "${_post_body}"
230-
_post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
262+
if [ -z "${_exist_record}" ]; then
263+
_post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
264+
else
265+
_post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets/${_record_id}" false "PUT" >/dev/null
266+
fi
231267
_code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
232268
if [ "$_code" != "202" ]; then
233269
_err "dns_huaweicloud: http code ${_code}"

0 commit comments

Comments
 (0)