3 #Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net>
5 LINODE_API_URL
="https://api.linode.com/?api_key=$LINODE_API_KEY&api_action="
7 ######## Public functions #####################
9 #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14 if ! _Linode_API
; then
19 _debug
"Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'"
21 _debug
"First detect the root zone"
22 if ! _get_root
"$fulldomain"; then
23 _err
"Domain does not exist."
26 _debug _domain_id
"$_domain_id"
27 _debug _sub_domain
"$_sub_domain"
28 _debug _domain
"$_domain"
30 _parameters
="&DomainID=$_domain_id&Type=TXT&Name=$_sub_domain&Target=$txtvalue"
32 if _rest GET
"domain.resource.create" "$_parameters" && [ -n "$response" ]; then
33 _resource_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"ResourceID\":\s*[0-9]+" | cut
-d : -f 2 |
tr -d " " | _head_n
1)
34 _debug _resource_id
"$_resource_id"
36 if [ -z "$_resource_id" ]; then
37 _err
"Error adding the domain resource."
41 _info
"Domain resource successfully added."
48 #Usage: dns_linode_rm _acme-challenge.www.domain.com
52 if ! _Linode_API
; then
57 _debug
"Calling: dns_linode_rm() '${fulldomain}'"
59 _debug
"First detect the root zone"
60 if ! _get_root
"$fulldomain"; then
61 _err
"Domain does not exist."
64 _debug _domain_id
"$_domain_id"
65 _debug _sub_domain
"$_sub_domain"
66 _debug _domain
"$_domain"
68 _parameters
="&DomainID=$_domain_id"
70 if _rest GET
"domain.resource.list" "$_parameters" && [ -n "$response" ]; then
71 response
="$(echo "$response" | tr -d "\n" | tr '{' "|
" | sed 's/|/&{/g' | tr "|
" "\n")"
73 resource
="$(echo "$response" | _egrep_o "{.
*\"NAME
\":\s
*\"$_sub_domain\".
*}")"
74 if [ "$resource" ]; then
75 _resource_id
=$
(printf "%s\n" "$resource" | _egrep_o
"\"RESOURCEID\":\s*[0-9]+" | _head_n
1 | cut
-d : -f 2 |
tr -d \
)
76 if [ "$_resource_id" ]; then
77 _debug _resource_id
"$_resource_id"
79 _parameters
="&DomainID=$_domain_id&ResourceID=$_resource_id"
81 if _rest GET
"domain.resource.delete" "$_parameters" && [ -n "$response" ]; then
82 _resource_id
=$
(printf "%s\n" "$response" | _egrep_o
"\"ResourceID\":\s*[0-9]+" | cut
-d : -f 2 |
tr -d " " | _head_n
1)
83 _debug _resource_id
"$_resource_id"
85 if [ -z "$_resource_id" ]; then
86 _err
"Error deleting the domain resource."
90 _info
"Domain resource successfully deleted."
104 #################### Private functions below ##################################
107 if [ -z "$LINODE_API_KEY" ]; then
110 _err
"You didn't specify the Linode API key yet."
111 _err
"Please create your key and try again."
116 _saveaccountconf LINODE_API_KEY
"$LINODE_API_KEY"
119 #################### Private functions below ##################################
120 #_acme-challenge.www.domain.com
122 # _sub_domain=_acme-challenge.www
130 if _rest GET
"domain.list"; then
131 response
="$(echo "$response" | tr -d "\n" | tr '{' "|
" | sed 's/|/&{/g' | tr "|
" "\n")"
133 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
140 hostedzone
="$(echo "$response" | _egrep_o "{.
*\"DOMAIN
\":\s
*\"$h\".
*}")"
141 if [ "$hostedzone" ]; then
142 _domain_id
=$
(printf "%s\n" "$hostedzone" | _egrep_o
"\"DOMAINID\":\s*[0-9]+" | _head_n
1 | cut
-d : -f 2 |
tr -d \
)
143 if [ "$_domain_id" ]; then
144 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
157 #method method action data
166 export _H1
="Accept: application/json"
167 export _H2
="Content-Type: application/json"
169 if [ "$mtd" != "GET" ]; then
170 # both POST and DELETE.
172 response
="$(_post "$data" "$LINODE_API_URL$ep" "" "$mtd")"
174 response
="$(_get "$LINODE_API_URL$ep$data")"
177 if [ "$?" != "0" ]; then
181 _debug2 response
"$response"