5 # HUAWEICLOUD_ProjectID
7 iam_api
="https://iam.myhuaweicloud.com"
8 dns_api
="https://dns.ap-southeast-1.myhuaweicloud.com"
10 ######## Public functions #####################
12 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 # Used to add txt record
15 # Ref: https://support.huaweicloud.com/intl/zh-cn/api-dns/zh-cn_topic_0132421999.html
18 dns_huaweicloud_add
() {
22 HUAWEICLOUD_Username
="${HUAWEICLOUD_Username:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
23 HUAWEICLOUD_Password
="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
24 HUAWEICLOUD_ProjectID
="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}"
26 if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ]; then
27 _err
"Not enough info provided to dns_huaweicloud!"
31 token
="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_ProjectID}")"
33 zoneid
="$(_get_zoneid "${token}" "${fulldomain}")"
36 _debug
"Adding Record"
37 _add_record
"${token}" "${fulldomain}" "${txtvalue}"
39 if [ "${ret}" != "0" ]; then
40 _err
"dns_huaweicloud: Error adding record."
44 # Do saving work if all succeeded
45 _saveaccountconf_mutable HUAWEICLOUD_Username
"${HUAWEICLOUD_Username}"
46 _saveaccountconf_mutable HUAWEICLOUD_Password
"${HUAWEICLOUD_Password}"
47 _saveaccountconf_mutable HUAWEICLOUD_ProjectID
"${HUAWEICLOUD_ProjectID}"
51 # Usage: fulldomain txtvalue
52 # Used to remove the txt record after validation
54 # Ref: https://support.huaweicloud.com/intl/zh-cn/api-dns/dns_api_64005.html
57 dns_huaweicloud_rm
() {
61 HUAWEICLOUD_Username
="${HUAWEICLOUD_Username:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
62 HUAWEICLOUD_Password
="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
63 HUAWEICLOUD_ProjectID
="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}"
65 if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ]; then
66 _err
"Please provide enough information"
70 token
="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_ProjectID}")"
72 zoneid
="$(_get_zoneid "${token}" "${fulldomain}")"
74 record_id
="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
75 _debug
"Record Set ID is: ${record_id}"
78 while [ "${record_id}" != "0" ]; do
79 _debug
"Removing Record"
80 _rm_record
"${token}" "${zoneid}" "${record_id}"
81 record_id
="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
86 ################### Private functions below ##################################
93 # printf "%s" "${_zoneid}"
97 export _H1
="X-Auth-Token: ${_token}"
101 h
=$
(printf "%s" "${_domain_string}" | cut
-d .
-f $i-100)
107 response
=$
(_get
"${dns_api}/v2/zones?name=${h}")
109 if _contains
"${response}" "id"; then
110 _debug
"Get Zone ID Success."
111 _zoneid
=$
(echo "${response}" | _egrep_o
"\"id\": *\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \" |
tr -d " ")
112 printf "%s" "${_zoneid}"
121 _get_recordset_id
() {
125 export _H1
="X-Auth-Token: ${_token}"
127 response
=$
(_get
"${dns_api}/v2/zones/${_zoneid}/recordsets?name=${_domain}")
128 if _contains
"${response}" "id"; then
129 _id
="$(echo "${response}" | _egrep_o "\"id
\": *\"[^
\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")"
142 # Get Existing Records
143 export _H1
="X-Auth-Token: ${_token}"
144 response
=$
(_get
"${dns_api}/v2/zones/${zoneid}/recordsets?name=${_domain}")
147 _exist_record
=$
(echo "${response}" |
sed -e ':a' -e 'N;$!ba;' -e 's/\n/ /g' | _egrep_o
'"records":[^]]*' |
sed 's/\"records\"\:\[//g')
148 _debug
"${_exist_record}"
150 # Check if record exist
153 \"name\": \"${_domain}.\",
154 \"description\": \"ACME Challenge\",
159 \"\\\"${_txtvalue}\\\"\"
162 if [ -z "${_exist_record}" ]; then
164 \"name\": \"${_domain}.\",
165 \"description\": \"ACME Challenge\",
169 \"\\\"${_txtvalue}\\\"\"
174 _record_id
="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
175 _debug
"Record Set ID is: ${_record_id}"
178 while [ "${_record_id}" != "0" ]; do
179 _debug
"Removing Record"
180 _rm_record
"${_token}" "${zoneid}" "${_record_id}"
181 _record_id
="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
182 _debug
"${_record_id}"
185 # Add brand new records with all old and new records
186 export _H2
="Content-Type: application/json"
187 export _H1
="X-Auth-Token: ${_token}"
189 _debug
"${_post_body}"
191 _post
"${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev
/null
192 _code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r
\\n
")"
193 if [ "$_code" != "202" ]; then
194 _err
"dns_huaweicloud: http code ${_code}"
201 # _rm_record $token $zoneid $recordid
202 # assume ${dns_api} exist
210 export _H2
="Content-Type: application/json"
211 export _H1
="X-Auth-Token: ${_token}"
213 _post
"" "${dns_api}/v2/zones/${_zone_id}/recordsets/${_record_id}" false
"DELETE" >/dev
/null
222 _debug
"Getting Token"
231 \"name\": \"${_username}\",
232 \"password\": \"${_password}\",
234 \"name\": \"${_username}\"
241 \"id\": \"${_project}\"
246 export _H1
="Content-Type: application/json;charset=utf8"
247 _post
"${body}" "${iam_api}/v3/auth/tokens" >/dev
/null
248 _code
=$
(grep "^HTTP" "$HTTP_HEADER" | _tail_n
1 | cut
-d " " -f 2 |
tr -d "\\r\\n")
249 _token
=$
(grep "^X-Subject-Token" "$HTTP_HEADER" | cut
-d " " -f 2-)
251 printf "%s" "${_token}"