refactor: remove dirty debug code

- add tr to replace sed for robusty
- add comments
This commit is contained in:
Easton Man 2020-11-24 10:05:34 +08:00
parent 83a4db3b31
commit c4ddddd434

View File

@ -23,8 +23,9 @@ dns_huaweicloud_add() {
HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}" HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
HUAWEICLOUD_ProjectID="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}" HUAWEICLOUD_ProjectID="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}"
if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ]; then # Check information
_err "Not enough info provided to dns_huaweicloud!" if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Password}" ] || [ -z "${HUAWEICLOUD_ProjectID}" ]; then
_err "Not enough information provided to dns_huaweicloud!"
return 1 return 1
fi fi
@ -62,8 +63,9 @@ dns_huaweicloud_rm() {
HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}" HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
HUAWEICLOUD_ProjectID="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}" HUAWEICLOUD_ProjectID="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}"
if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ]; then # Check information
_err "Please provide enough information" if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Password}" ] || [ -z "${HUAWEICLOUD_ProjectID}" ]; then
_err "Not enough information provided to dns_huaweicloud!"
return 1 return 1
fi fi
@ -75,6 +77,8 @@ dns_huaweicloud_rm() {
_debug "Record Set ID is: ${record_id}" _debug "Record Set ID is: ${record_id}"
# Remove all records # 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" ]; do
_debug "Removing Record" _debug "Removing Record"
_rm_record "${token}" "${zoneid}" "${record_id}" _rm_record "${token}" "${zoneid}" "${record_id}"
@ -143,22 +147,12 @@ _add_record() {
export _H1="X-Auth-Token: ${_token}" export _H1="X-Auth-Token: ${_token}"
response=$(_get "${dns_api}/v2/zones/${zoneid}/recordsets?name=${_domain}") response=$(_get "${dns_api}/v2/zones/${zoneid}/recordsets?name=${_domain}")
_debug "${response}" _debug2 "${response}"
_exist_record=$(echo "${response}" | _egrep_o '"records":[^]]*' | sed 's/\"records\"\:\[//g') _exist_record=$(echo "${response}" | tr -d "\\r\\n" | _egrep_o '"records":[^]]*' | sed 's/\"records\"\:\[//g')
_debug "${_exist_record}" _debug "${_exist_record}"
# Check if record exist # Check if record exist
# Generate body data # Generate body data
_post_body="{
\"name\": \"${_domain}.\",
\"description\": \"ACME Challenge\",
\"type\": \"TXT\",
\"ttl\": 1,
\"records\": [
${_exist_record},
\"\\\"${_txtvalue}\\\"\"
]
}"
if [ -z "${_exist_record}" ]; then if [ -z "${_exist_record}" ]; then
_post_body="{ _post_body="{
\"name\": \"${_domain}.\", \"name\": \"${_domain}.\",
@ -169,6 +163,17 @@ _add_record() {
\"\\\"${_txtvalue}\\\"\" \"\\\"${_txtvalue}\\\"\"
] ]
}" }"
else
_post_body="{
\"name\": \"${_domain}.\",
\"description\": \"ACME Challenge\",
\"type\": \"TXT\",
\"ttl\": 1,
\"records\": [
${_exist_record},
\"\\\"${_txtvalue}\\\"\"
]
}"
fi fi
_record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")" _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
@ -179,20 +184,17 @@ _add_record() {
_debug "Removing Record" _debug "Removing Record"
_rm_record "${_token}" "${zoneid}" "${_record_id}" _rm_record "${_token}" "${zoneid}" "${_record_id}"
_record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")" _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
_debug "${_record_id}"
done done
# Add brand new records with all old and new records # Add brand new records with all old and new records
export _H2="Content-Type: application/json" export _H2="Content-Type: application/json"
export _H1="X-Auth-Token: ${_token}" export _H1="X-Auth-Token: ${_token}"
_debug "${_post_body}" _debug2 "${_post_body}"
sleep 2
_post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null _post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
_code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
if [ "$_code" != "202" ]; then if [ "$_code" != "202" ]; then
_err "dns_huaweicloud: http code ${_code}" _err "dns_huaweicloud: http code ${_code}"
sleep 60
return 1 return 1
fi fi
return 0 return 0