3 #Original Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net>
4 #v4 Update Author: Aaron W. Swenson <aaron@grandmasfridge.org>
6 LINODE_V4_API_URL
="https://api.linode.com/v4/domains"
8 ######## Public functions #####################
10 #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
15 if ! _Linode_API
; then
20 _debug
"Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'"
22 _debug
"First detect the root zone"
23 if ! _get_root
"$fulldomain"; then
24 _err
"Domain does not exist."
27 _debug _domain_id
"$_domain_id"
28 _debug _sub_domain
"$_sub_domain"
29 _debug _domain
"$_domain"
33 \"name\": \"$_sub_domain\",
34 \"target\": \"$txtvalue\"
37 if _rest POST
"/$_domain_id/records" "$_payload" && [ -n "$response" ]; then
38 _resource_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"id\":\s*[0-9]+" | cut
-d : -f 2 |
tr -d " " | _head_n
1)
39 _debug _resource_id
"$_resource_id"
41 if [ -z "$_resource_id" ]; then
42 _err
"Error adding the domain resource."
46 _info
"Domain resource successfully added."
53 #Usage: dns_linode_rm _acme-challenge.www.domain.com
57 if ! _Linode_API
; then
62 _debug
"Calling: dns_linode_rm() '${fulldomain}'"
64 _debug
"First detect the root zone"
65 if ! _get_root
"$fulldomain"; then
66 _err
"Domain does not exist."
69 _debug _domain_id
"$_domain_id"
70 _debug _sub_domain
"$_sub_domain"
71 _debug _domain
"$_domain"
73 if _rest GET
"/$_domain_id/records" && [ -n "$response" ]; then
74 response
="$(echo "$response" | tr -d "\n" | tr '{' "|
" | sed 's/|/&{/g' | tr "|
" "\n")"
76 resource
="$(echo "$response" | _egrep_o "{.
*\"name
\":\s
*\"$_sub_domain\".
*}")"
77 if [ "$resource" ]; then
78 _resource_id
=$
(printf "%s\n" "$resource" | _egrep_o
"\"id\":\s*[0-9]+" | _head_n
1 | cut
-d : -f 2 |
tr -d \
)
79 if [ "$_resource_id" ]; then
80 _debug _resource_id
"$_resource_id"
82 if _rest DELETE
"/$_domain_id/records/$_resource_id" && [ -n "$response" ]; then
83 # On 200/OK, empty set is returned. Check for error, if any.
84 _error_response
=$
(printf "%s\n" "$response" | _egrep_o
"\"errors\"" | cut
-d : -f 2 |
tr -d " " | _head_n
1)
86 if [ -n "$_error_response" ]; then
87 _err
"Error deleting the domain resource: $_error_response"
91 _info
"Domain resource successfully deleted."
105 #################### Private functions below ##################################
108 if [ -z "$LINODE_V4_API_KEY" ]; then
111 _err
"You didn't specify the Linode v4 API key yet."
112 _err
"Please create your key and try again."
117 _saveaccountconf LINODE_V4_API_KEY
"$LINODE_V4_API_KEY"
120 #################### Private functions below ##################################
121 #_acme-challenge.www.domain.com
123 # _sub_domain=_acme-challenge.www
132 response
="$(echo "$response" | tr -d "\n" | tr '{' "|
" | sed 's/|/&{/g' | tr "|
" "\n")"
134 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
141 hostedzone
="$(echo "$response" | _egrep_o "{.
*\"domain
\":\s
*\"$h\".
*}")"
142 if [ "$hostedzone" ]; then
143 _domain_id
=$
(printf "%s\n" "$hostedzone" | _egrep_o
"\"id\":\s*[0-9]+" | _head_n
1 | cut
-d : -f 2 |
tr -d \
)
144 if [ "$_domain_id" ]; then
145 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
158 #method method action data
167 export _H1
="Accept: application/json"
168 export _H2
="Content-Type: application/json"
169 export _H3
="Authorization: Bearer $LINODE_V4_API_KEY"
171 if [ "$mtd" != "GET" ]; then
172 # both POST and DELETE.
174 response
="$(_post "$data" "$LINODE_V4_API_URL$ep" "" "$mtd")"
176 response
="$(_get "$LINODE_V4_API_URL$ep$data")"
179 if [ "$?" != "0" ]; then
183 _debug2 response
"$response"