123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #!/usr/bin/env sh
- dns_dgon_add() {
- fulldomain="$(echo "$1" | _lower_case)"
- txtvalue=$2
- DO_API_KEY="${DO_API_KEY:-$(_readaccountconf_mutable DO_API_KEY)}"
-
- if [ -z "$DO_API_KEY" ]; then
- DO_API_KEY=""
- _err "You did not specify DigitalOcean API key."
- _err "Please export DO_API_KEY and try again."
- return 1
- fi
- _info "Using digitalocean dns validation - add record"
- _debug fulldomain "$fulldomain"
- _debug txtvalue "$txtvalue"
-
- _saveaccountconf_mutable DO_API_KEY "$DO_API_KEY"
-
- if ! _get_base_domain "$fulldomain"; then
- _err "domain not found in your account for addition"
- return 1
- fi
- _debug _sub_domain "$_sub_domain"
- _debug _domain "$_domain"
-
- export _H1="Content-Type: application/json"
- export _H2="Authorization: Bearer $DO_API_KEY"
- PURL='https://api.digitalocean.com/v2/domains/'$_domain'/records'
- PBODY='{"type":"TXT","name":"'$_sub_domain'","data":"'$txtvalue'","ttl":120}'
- _debug PURL "$PURL"
- _debug PBODY "$PBODY"
-
-
- response="$(_post "$PBODY" "$PURL")"
-
- if [ "$?" != "0" ]; then
- _err "error in response: $response"
- return 1
- fi
- _debug2 response "$response"
-
- return 0
- }
- dns_dgon_rm() {
- fulldomain="$(echo "$1" | _lower_case)"
- txtvalue=$2
- DO_API_KEY="${DO_API_KEY:-$(_readaccountconf_mutable DO_API_KEY)}"
-
- if [ -z "$DO_API_KEY" ]; then
- DO_API_KEY=""
- _err "You did not specify DigitalOcean API key."
- _err "Please export DO_API_KEY and try again."
- return 1
- fi
- _info "Using digitalocean dns validation - remove record"
- _debug fulldomain "$fulldomain"
- _debug txtvalue "$txtvalue"
-
- if ! _get_base_domain "$fulldomain"; then
- _err "domain not found in your account for removal"
- return 1
- fi
- _debug _sub_domain "$_sub_domain"
- _debug _domain "$_domain"
-
- export _H1="Content-Type: application/json"
- export _H2="Authorization: Bearer $DO_API_KEY"
-
-
- GURL="https://api.digitalocean.com/v2/domains/$_domain/records"
-
- while [ -z "$record" ]; do
-
-
-
- domain_list="$(_get "$GURL")"
-
-
- record="$(echo "$domain_list" | _egrep_o "\"id\"\s*\:\s*\"*[0-9]+\"*[^}]*\"name\"\s*\:\s*\"$_sub_domain\"[^}]*\"data\"\s*\:\s*\"$txtvalue\"")"
-
- if [ -z "$record" ]; then
-
- nextpage="$(echo "$domain_list" | _egrep_o "\"links\".*" | _egrep_o "\"next\".*" | _egrep_o "http.*page\=[0-9]+")"
- if [ -z "$nextpage" ]; then
- _err "no record and no nextpage in digital ocean DNS removal"
- return 1
- fi
- _debug2 nextpage "$nextpage"
- GURL="$nextpage"
- fi
-
- done
-
- rec_id="$(echo "$record" | _egrep_o "id\"\s*\:\s*\"*[0-9]+" | _egrep_o "[0-9]+")"
- _debug rec_id "$rec_id"
-
-
- DURL="https://api.digitalocean.com/v2/domains/$_domain/records/$rec_id"
-
-
- response="$(_post "" "$DURL" "" "DELETE")"
-
- if [ "$?" != "0" ]; then
- _err "error in remove response: $response"
- return 1
- fi
- _debug2 response "$response"
-
- return 0
- }
- _get_base_domain() {
-
- fulldomain="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
- _debug fulldomain "$fulldomain"
-
- MAX_DOM=255
-
-
- export _H1="Content-Type: application/json"
- export _H2="Authorization: Bearer $DO_API_KEY"
- _debug DO_API_KEY "$DO_API_KEY"
-
-
- DOMURL="https://api.digitalocean.com/v2/domains"
-
- domain_list="$(_get "$DOMURL")"
-
- if [ "$?" != "0" ]; then
- _err "error in domain_list response: $domain_list"
- return 1
- fi
- _debug2 domain_list "$domain_list"
-
-
- i=2
- while [ $i -gt 0 ]; do
-
- _domain=$(printf "%s" "$fulldomain" | cut -d . -f "$i"-"$MAX_DOM")
-
- if [ -z "$_domain" ]; then
-
- _err "domain not found in DigitalOcean account"
- return 1
- fi
-
- found="$(echo "$domain_list" | _egrep_o "\"name\"\s*\:\s*\"$_domain\"")"
-
- if [ ! -z "$found" ]; then
-
- sub_point=$(_math $i - 1)
- _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-"$sub_point")
- _debug _domain "$_domain"
- _debug _sub_domain "$_sub_domain"
- return 0
- fi
-
- i=$(_math $i + 1)
- done
-
-
- _err "domain not found in DigitalOcean account, but we should never get here"
- return 1
- }
|