]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_scaleway.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_scaleway.sh
1 #!/usr/bin/env sh
2
3 # Scaleway API
4 # https://developers.scaleway.com/en/products/domain/dns/api/
5 #
6 # Requires Scaleway API token set in SCALEWAY_API_TOKEN
7
8 ######## Public functions #####################
9
10 SCALEWAY_API="https://api.scaleway.com/domain/v2beta1"
11
12 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13 dns_scaleway_add() {
14 fulldomain=$1
15 txtvalue=$2
16
17 if ! _scaleway_check_config; then
18 return 1
19 fi
20
21 _debug "First detect the root zone"
22 if ! _get_root "$fulldomain"; then
23 _err "invalid domain"
24 return 1
25 fi
26
27 _debug _sub_domain "$_sub_domain"
28 _debug _domain "$_domain"
29
30 _info "Adding record"
31 _scaleway_create_TXT_record "$_domain" "$_sub_domain" "$txtvalue"
32 if _contains "$response" "records"; then
33 return 0
34 else
35 _err error "$response"
36 return 1
37 fi
38 _info "Record added."
39
40 return 0
41 }
42
43 dns_scaleway_rm() {
44 fulldomain=$1
45 txtvalue=$2
46
47 if ! _scaleway_check_config; then
48 return 1
49 fi
50
51 _debug "First detect the root zone"
52 if ! _get_root "$fulldomain"; then
53 _err "invalid domain"
54 return 1
55 fi
56
57 _debug _sub_domain "$_sub_domain"
58 _debug _domain "$_domain"
59
60 _info "Deleting record"
61 _scaleway_delete_TXT_record "$_domain" "$_sub_domain" "$txtvalue"
62 if _contains "$response" "records"; then
63 return 0
64 else
65 _err error "$response"
66 return 1
67 fi
68 _info "Record deleted."
69
70 return 0
71 }
72
73 #################### Private functions below ##################################
74
75 _scaleway_check_config() {
76 SCALEWAY_API_TOKEN="${SCALEWAY_API_TOKEN:-$(_readaccountconf_mutable SCALEWAY_API_TOKEN)}"
77 if [ -z "$SCALEWAY_API_TOKEN" ]; then
78 _err "No API key specified for Scaleway API."
79 _err "Create your key and export it as SCALEWAY_API_TOKEN"
80 return 1
81 fi
82 if ! _scaleway_rest GET "dns-zones"; then
83 _err "Invalid API key specified for Scaleway API."
84 return 1
85 fi
86
87 _saveaccountconf_mutable SCALEWAY_API_TOKEN "$SCALEWAY_API_TOKEN"
88
89 return 0
90 }
91
92 #_acme-challenge.www.domain.com
93 #returns
94 # _sub_domain=_acme-challenge.www
95 # _domain=domain.com
96 _get_root() {
97 domain=$1
98 i=1
99 p=1
100 while true; do
101 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
102 if [ -z "$h" ]; then
103 #not valid
104 return 1
105 fi
106
107 _scaleway_rest GET "dns-zones/$h/records"
108
109 if ! _contains "$response" "subdomain not found" >/dev/null; then
110 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
111 _domain="$h"
112 return 0
113 fi
114 p=$i
115 i=$(_math "$i" + 1)
116 done
117 _err "Unable to retrive DNS zone matching this domain"
118 return 1
119 }
120
121 # this function add a TXT record
122 _scaleway_create_TXT_record() {
123 txt_zone=$1
124 txt_name=$2
125 txt_value=$3
126
127 _scaleway_rest PATCH "dns-zones/$txt_zone/records" "{\"return_all_records\":false,\"changes\":[{\"add\":{\"records\":[{\"name\":\"$txt_name\",\"data\":\"$txt_value\",\"type\":\"TXT\",\"ttl\":60}]}}]}"
128
129 if _contains "$response" "records"; then
130 return 0
131 else
132 _err "error1 $response"
133 return 1
134 fi
135 }
136
137 # this function delete a TXT record based on name and content
138 _scaleway_delete_TXT_record() {
139 txt_zone=$1
140 txt_name=$2
141 txt_value=$3
142
143 _scaleway_rest PATCH "dns-zones/$txt_zone/records" "{\"return_all_records\":false,\"changes\":[{\"delete\":{\"id_fields\":{\"name\":\"$txt_name\",\"data\":\"$txt_value\",\"type\":\"TXT\"}}}]}"
144
145 if _contains "$response" "records"; then
146 return 0
147 else
148 _err "error2 $response"
149 return 1
150 fi
151 }
152
153 _scaleway_rest() {
154 m=$1
155 ep="$2"
156 data="$3"
157 _debug "$ep"
158 _scaleway_url="$SCALEWAY_API/$ep"
159 _debug2 _scaleway_url "$_scaleway_url"
160 export _H1="x-auth-token: $SCALEWAY_API_TOKEN"
161 export _H2="Accept: application/json"
162 export _H3="Content-Type: application/json"
163
164 if [ "$data" ] || [ "$m" != "GET" ]; then
165 _debug data "$data"
166 response="$(_post "$data" "$_scaleway_url" "" "$m")"
167 else
168 response="$(_get "$_scaleway_url")"
169 fi
170 if [ "$?" != "0" ] || _contains "$response" "denied_authentication" || _contains "$response" "Method not allowed" || _contains "$response" "json parse error: unexpected EOF"; then
171 _err "error $response"
172 return 1
173 fi
174 _debug2 response "$response"
175 return 0
176 }