Skip to content

Commit da66431

Browse files
pileus-linesJF
authored andcommitted
Update dns_infomaniak.sh
because infomaniak API v1 no longer works
1 parent 42bbd1b commit da66431

File tree

1 file changed

+38
-49
lines changed

1 file changed

+38
-49
lines changed

dnsapi/dns_infomaniak.sh

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_infomaniak
66
Options:
77
INFOMANIAK_API_TOKEN API Token
88
Issues: github.com/acmesh-official/acme.sh/issues/3188
9+
910
'
1011

1112
# To use this API you need visit the API dashboard of your account
@@ -65,33 +66,32 @@ dns_infomaniak_add() {
6566
_debug fulldomain "$fulldomain"
6667
_debug txtvalue "$txtvalue"
6768

68-
fqdn=${fulldomain#_acme-challenge.}
69-
7069
# guess which base domain to add record to
71-
zone_and_id=$(_find_zone "$fqdn")
72-
if [ -z "$zone_and_id" ]; then
73-
_err "cannot find zone to modify"
70+
zone=$(_get_zone "$fulldomain")
71+
if [ -z "$zone" ]; then
72+
_err "cannot find zone:<${zone}> to modify"
7473
return 1
7574
fi
76-
zone=${zone_and_id% *}
77-
domain_id=${zone_and_id#* }
7875

7976
# extract first part of domain
8077
key=${fulldomain%."$zone"}
8178

82-
_debug "zone:$zone id:$domain_id key:$key"
79+
_debug "key:$key"
80+
_debug "txtvalue: $txtvalue"
8381

8482
# payload
8583
data="{\"type\": \"TXT\", \"source\": \"$key\", \"target\": \"$txtvalue\", \"ttl\": $INFOMANIAK_TTL}"
8684

8785
# API call
88-
response=$(_post "$data" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record")
89-
if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
90-
_info "Record added"
91-
_debug "Response: $response"
92-
return 0
86+
response=$(_post "$data" "${INFOMANIAK_API_URL}/2/zones/${zone}/records")
87+
if [ -n "$response" ]; then
88+
if [ ! "$(echo "$response" | _contains '"result":"success"')" ]; then
89+
_info "Record added"
90+
_debug "response: $response"
91+
return 0
92+
fi
9393
fi
94-
_err "could not create record"
94+
_err "Could not create record."
9595
_debug "Response: $response"
9696
return 1
9797
}
@@ -106,7 +106,7 @@ dns_infomaniak_rm() {
106106

107107
if [ -z "$INFOMANIAK_API_TOKEN" ]; then
108108
INFOMANIAK_API_TOKEN=""
109-
_err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
109+
_err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN."
110110
return 1
111111
fi
112112

@@ -138,63 +138,52 @@ dns_infomaniak_rm() {
138138
_debug fulldomain "$fulldomain"
139139
_debug txtvalue "$txtvalue"
140140

141-
fqdn=${fulldomain#_acme-challenge.}
142-
143141
# guess which base domain to add record to
144-
zone_and_id=$(_find_zone "$fqdn")
145-
if [ -z "$zone_and_id" ]; then
146-
_err "cannot find zone to modify"
142+
zone=$(_get_zone "$fulldomain")
143+
if [ -z "$zone" ]; then
144+
_err "cannot find zone:<$zone> to modify"
147145
return 1
148146
fi
149-
zone=${zone_and_id% *}
150-
domain_id=${zone_and_id#* }
151147

152148
# extract first part of domain
153149
key=${fulldomain%."$zone"}
150+
key=$(echo "$key" | _lower_case)
154151

155-
_debug "zone:$zone id:$domain_id key:$key"
152+
_debug "zone:$zone"
153+
_debug "key:$key"
156154

157155
# find previous record
158-
# shellcheck disable=SC1004
159-
record_id=$(_get "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}\
160-
{/g' | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source_idn":"'"$fulldomain"'".*"target_idn":"'"$txtvalue"'".*/\1/p')
156+
# shellcheck disable=SC2086
157+
response=$(_get "${INFOMANIAK_API_URL}/2/zones/${zone}/records" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}{/g')
158+
record_id=$(echo "$response" | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source":"'"$key"'".*"target":"\\"'"$txtvalue"'\\"".*/\1/p')
159+
_debug "key: $key"
160+
_debug "txtvalue: $txtvalue"
161+
_debug "record_id: $record_id"
162+
161163
if [ -z "$record_id" ]; then
162164
_err "could not find record to delete"
165+
_debug "response: $response"
163166
return 1
164167
fi
165-
_debug "record_id: $record_id"
166168

167169
# API call
168-
response=$(_post "" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record/$record_id" "" DELETE)
170+
response=$(_post "" "${INFOMANIAK_API_URL}/2/zones/${zone}/records/${record_id}" "" DELETE)
169171
if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
170172
_info "Record deleted"
171173
return 0
174+
else
175+
_err "could not delete record"
176+
_debug "response: $response"
177+
return 1
172178
fi
173-
_err "could not delete record"
174-
return 1
175179
}
176180

177181
#################### Private functions below ##################################
178182

179-
_get_domain_id() {
183+
_get_zone() {
180184
domain="$1"
181-
185+
# Whatever the domain is, you can get the fqdn with the following.
182186
# shellcheck disable=SC1004
183-
_get "${INFOMANIAK_API_URL}/1/product?service_name=domain&customer_name=$domain" | sed 's/.*"data":\[{\(.*\)}\]}/\1/; s/,/\
184-
/g' | sed -n 's/^"id":\(.*\)/\1/p'
185-
}
186-
187-
_find_zone() {
188-
zone="$1"
189-
190-
# find domain in list, removing . parts sequentialy
191-
while _contains "$zone" '\.'; do
192-
_debug "testing $zone"
193-
id=$(_get_domain_id "$zone")
194-
if [ -n "$id" ]; then
195-
echo "$zone $id"
196-
return
197-
fi
198-
zone=${zone#*.}
199-
done
187+
response=$(_get "${INFOMANIAK_API_URL}/2/domains/${domain}/zones" | sed 's/.*\[{"fqdn"\:"\(.*\)/\1/')
188+
echo "${response%%\"*}"
200189
}

0 commit comments

Comments
 (0)