8 AZION_Api
="https://api.azionapi.net"
10 ######## Public functions ########
12 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 # Used to add txt record
18 _debug
"Detect the root zone"
19 if ! _get_root
"$fulldomain"; then
20 _err
"Domain not found"
24 _debug _sub_domain
"$_sub_domain"
25 _debug _domain
"$_domain"
26 _debug _domain_id
"$_domain_id"
28 _info
"Add or update record"
29 _get_record
"$_domain_id" "$_sub_domain"
30 if [ "$record_id" ]; then
31 _payload
="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [$answers_list, \"$txtvalue\"], \"ttl\": 20}"
32 if _azion_rest PUT
"intelligent_dns/$_domain_id/records/$record_id" "$_payload"; then
33 if _contains
"$response" "$txtvalue"; then
34 _info
"Record updated."
39 _payload
="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [\"$txtvalue\"], \"ttl\": 20}"
40 if _azion_rest POST
"intelligent_dns/$_domain_id/records" "$_payload"; then
41 if _contains
"$response" "$txtvalue"; then
47 _err
"Failed to add or update record."
51 # Usage: fulldomain txtvalue
52 # Used to remove the txt record after validation
57 _debug
"Detect the root zone"
58 if ! _get_root
"$fulldomain"; then
59 _err
"Domain not found"
63 _debug _sub_domain
"$_sub_domain"
64 _debug _domain
"$_domain"
65 _debug _domain_id
"$_domain_id"
67 _info
"Removing record"
68 _get_record
"$_domain_id" "$_sub_domain"
69 if [ "$record_id" ]; then
70 if _azion_rest DELETE
"intelligent_dns/$_domain_id/records/$record_id"; then
71 _info
"Record removed."
74 _err
"Failed to remove record."
78 _info
"Record not found or already removed."
83 #################### Private functions below ##################################
84 # Usage: _acme-challenge.www.domain.com
86 # _sub_domain=_acme-challenge.www
88 # _domain_id=sdjkglgdfewsdfg
94 if ! _azion_rest GET
"intelligent_dns"; then
99 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
106 if _contains
"$response" "\"domain\":\"$h\""; then
107 _domain_id
=$
(echo "$response" |
tr '{' "\n" |
grep "\"domain\":\"$h\"" | _egrep_o
"\"id\":[0-9]*" | _head_n
1 | cut
-d : -f 2 |
tr -d \")
108 _debug _domain_id
"$_domain_id"
109 if [ "$_domain_id" ]; then
110 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
126 if ! _azion_rest GET
"intelligent_dns/$_domain_id/records"; then
130 if _contains
"$response" "\"entry\":\"$_record\""; then
131 _json_record
=$
(echo "$response" |
tr '{' "\n" |
grep "\"entry\":\"$_record\"")
132 if [ "$_json_record" ]; then
133 record_id
=$
(echo "$_json_record" | _egrep_o
"\"record_id\":[0-9]*" | _head_n
1 | cut
-d : -f 2 |
tr -d \")
134 answers_list
=$
(echo "$_json_record" | _egrep_o
"\"answers_list\":\[.*\]" | _head_n
1 | cut
-d : -f 2 |
tr -d \
[\
])
143 AZION_Email
="${AZION_Email:-$(_readaccountconf_mutable AZION_Email)}"
144 AZION_Password
="${AZION_Password:-$(_readaccountconf_mutable AZION_Password)}"
146 if ! _contains
"$AZION_Email" "@"; then
147 _err
"It seems that the AZION_Email is not a valid email address. Revalidate your environments."
151 if [ -z "$AZION_Email" ] ||
[ -z "$AZION_Password" ]; then
152 _err
"You didn't specified a AZION_Email/AZION_Password to generate Azion token."
156 _saveaccountconf_mutable AZION_Email
"$AZION_Email"
157 _saveaccountconf_mutable AZION_Password
"$AZION_Password"
159 _basic_auth
=$
(printf "%s:%s" "$AZION_Email" "$AZION_Password" | _base64
)
160 _debug _basic_auth
"$_basic_auth"
162 export _H1
="Accept: application/json; version=3"
163 export _H2
="Content-Type: application/json"
164 export _H3
="Authorization: Basic $_basic_auth"
166 response
="$(_post "" "$AZION_Api/tokens
" "" "POST
")"
167 if _contains
"$response" "\"token\":\"" >/dev
/null
; then
168 _azion_token
=$
(echo "$response" | _egrep_o
"\"token\":\"[^\"]*\"" | cut
-d : -f 2 |
tr -d \")
169 export AZION_Token
="$_azion_token"
171 _err
"Failed to generate Azion token"
181 if [ -z "$AZION_Token" ]; then
184 _debug2 token
"$AZION_Token"
186 export _H1
="Accept: application/json; version=3"
187 export _H2
="Content-Type: application/json"
188 export _H3
="Authorization: token $AZION_Token"
190 if [ "$_method" != "GET" ]; then
191 _debug _data
"$_data"
192 response
="$(_post "$_data" "$AZION_Api/$_uri" "" "$_method")"
194 response
="$(_get "$AZION_Api/$_uri")"
197 _debug2 response
"$response"
199 if [ "$?" != "0" ]; then
200 _err
"error $_method $_uri $_data"