浏览代码

feat: add retry count for removing record set

This avoids infinite loop when something went wrong and throws a error
Easton Man 2 年之前
父节点
当前提交
acbd8bce21
共有 1 个文件被更改,包括 50 次插入10 次删除
  1. 50 10
      dnsapi/dns_huaweicloud.sh

+ 50 - 10
dnsapi/dns_huaweicloud.sh

@@ -98,19 +98,59 @@ dns_huaweicloud_rm() {
   fi
   _debug "Zone ID is:" "${zoneid}"
 
+  _recursive_rm_record "${token}" "${fulldomain}" "${zoneid}" "${record_id}"
+  ret="$?"
+  if [ "${ret}" != "0" ]; then
+    _err "dns_api(dns_huaweicloud): Error removing record."
+    return 1
+  fi
+ 
+  return 0
+}
+
+###################  Private functions below ##################################
+
+
+# _recursive_rm_record
+# remove all records from the record set
+# 
+# _token=$1
+# _domain=$2
+# _zoneid=$3
+# _record_id=$4
+#
+# Returns 0 on success
+_recursive_rm_record() {
+  _token=$1
+  _domain=$2
+  _zoneid=$3
+  _record_id=$4
+
+  # Most likely to have problems will huaweicloud side if more than 50 attempts but still cannot fully remove the record set
+  # Maybe can be removed manually in the dashboard
+  _retry_cnt=50
+
   # Remove all records
   # Therotically HuaweiCloud does not allow more than one record set
   # But remove them recurringly to increase robusty
-  while [ "${record_id}" != "0" ]; do
+
+  while [ "${_record_id}" != "0" && "${_retry_cnt}" != "0" ]; do
     _debug "Removing Record"
-    _rm_record "${token}" "${zoneid}" "${record_id}"
-    record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
+    _retry_cnt=$((${_retry_cnt} - 1))
+    _rm_record "${_token}" "${_zoneid}" "${_record_id}"
+    _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${_zoneid}")"
+    _debug2 "Checking record exists: record_id=${_record_id}"
   done
+
+  # Check if retry count is reached
+  if [ "${_retry_cnt}" == "0" ]; then
+    _debug "Failed to remove record after 50 attempts, please try removing it manually in the dashboard"
+    return 1
+  fi
+
   return 0
 }
 
-###################  Private functions below ##################################
-
 # _get_zoneid
 #
 # _token=$1
@@ -216,11 +256,11 @@ _add_record() {
   _debug "Record Set ID is:" "${_record_id}"
 
   # Remove all records
-  while [ "${_record_id}" != "0" ]; do
-    _debug "Removing Record"
-    _rm_record "${_token}" "${zoneid}" "${_record_id}"
-    _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
-  done
+  _recursive_rm_record "${token}" "${_domain}" "${_zoneid}" "${_record_id}"
+  ret="$?"
+  if [ "${ret}" != "0" ]; then
+    return 1
+  fi
 
   # Add brand new records with all old and new records
   export _H2="Content-Type: application/json"