3 #NETLIFY_ACCESS_TOKEN="xxxx"
5 NETLIFY_HOST
="api.netlify.com/api/v1/"
6 NETLIFY_URL
="https://$NETLIFY_HOST"
8 ######## Public functions #####################
10 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
15 NETLIFY_ACCESS_TOKEN
="${NETLIFY_ACCESS_TOKEN:-$(_readaccountconf_mutable NETLIFY_ACCESS_TOKEN)}"
17 if [ -z "$NETLIFY_ACCESS_TOKEN" ]; then
18 NETLIFY_ACCESS_TOKEN
=""
19 _err
"Please specify your Netlify Access Token and try again."
22 _saveaccountconf_mutable NETLIFY_ACCESS_TOKEN
"$NETLIFY_ACCESS_TOKEN"
26 _debug fulldomain
"$fulldomain"
27 _debug txtvalue
"$txtvalue"
29 if ! _get_root
"$fulldomain"; then
34 _debug _domain_id
"$_domain_id"
35 _debug _sub_domain
"$_sub_domain"
36 _debug _domain
"$_domain"
38 dnsRecordURI
="dns_zones/$_domain_id/dns_records"
40 body
="{\"type\":\"TXT\", \"hostname\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"ttl\":\"10\"}"
42 _netlify_rest POST
"$dnsRecordURI" "$body" "$NETLIFY_ACCESS_TOKEN"
43 _code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r
\\n
")"
44 if [ "$_code" = "200" ] ||
[ "$_code" = '201' ]; then
45 _info
"validation value added"
48 _err
"error adding validation value ($_code)"
52 _err
"Not fully implemented!"
56 #Usage: dns_myapi_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
57 #Remove the txt record after validation.
62 _debug txtdomain
"$txtdomain"
65 NETLIFY_ACCESS_TOKEN
="${NETLIFY_ACCESS_TOKEN:-$(_readaccountconf_mutable NETLIFY_ACCESS_TOKEN)}"
67 if ! _get_root
"$txtdomain"; then
72 _debug _domain_id
"$_domain_id"
73 _debug _sub_domain
"$_sub_domain"
74 _debug _domain
"$_domain"
76 dnsRecordURI
="dns_zones/$_domain_id/dns_records"
78 _netlify_rest GET
"$dnsRecordURI" "" "$NETLIFY_ACCESS_TOKEN"
80 _record_id
=$
(echo "$response" | _egrep_o
"\"type\":\"TXT\",[^\}]*\"value\":\"$txt\"" |
head -n 1 | _egrep_o
"\"id\":\"[^\"\}]*\"" | cut
-d : -f 2 |
tr -d \")
81 _debug _record_id
"$_record_id"
82 if [ "$_record_id" ]; then
83 _netlify_rest DELETE
"$dnsRecordURI/$_record_id" "" "$NETLIFY_ACCESS_TOKEN"
84 _code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r
\\n
")"
85 if [ "$_code" = "200" ] ||
[ "$_code" = '204' ]; then
86 _info
"validation value removed"
89 _err
"error removing validation value ($_code)"
97 #################### Private functions below ##################################
105 _netlify_rest GET
"dns_zones" "" "$accesstoken"
108 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
109 _debug2
"Checking domain: $h"
112 _err
"Invalid domain"
116 if _contains
"$response" "\"name\":\"$h\"" >/dev
/null
; then
117 _domain_id
=$
(echo "$response" | _egrep_o
"\"[^\"]*\",\"name\":\"$h\"" | cut
-d , -f 1 |
tr -d \")
118 if [ "$_domain_id" ]; then
119 if [ "$i" = 1 ]; then
120 #create the record at the domain apex (@) if only the domain name was provided as --domain-alias
123 _sub_domain
=$
(echo "$domain" | cut
-d .
-f 1-$p)
142 token_trimmed
=$
(echo "$NETLIFY_ACCESS_TOKEN" |
tr -d '"')
144 export _H1
="Content-Type: application/json"
145 export _H2
="Authorization: Bearer $token_trimmed"
149 if [ "$m" != "GET" ]; then
151 response
="$(_post "$data" "$NETLIFY_URL$ep" "" "$m")"
153 response
="$(_get "$NETLIFY_URL$ep")"
156 if [ "$?" != "0" ]; then
160 _debug2 response
"$response"