3 # API-integration for Simply.com (https://www.simply.com)
5 #SIMPLY_AccountName="accountname"
6 #SIMPLY_ApiKey="apikey"
8 #SIMPLY_Api="https://api.simply.com/2/"
9 SIMPLY_Api_Default
="https://api.simply.com/2"
11 #This is used for determining success of REST call
12 SIMPLY_SUCCESS_CODE
='"status":200'
14 ######## Public functions #####################
15 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
20 if ! _simply_load_config
; then
26 _debug
"First detect the root zone"
27 if ! _get_root
"$fulldomain"; then
32 _debug _sub_domain
"$_sub_domain"
33 _debug _domain
"$_domain"
37 if ! _simply_add_record
"$_domain" "$_sub_domain" "$txtvalue"; then
38 _err
"Could not add DNS record"
48 if ! _simply_load_config
; then
54 _debug
"Find the DNS zone"
56 if ! _get_root
"$fulldomain"; then
61 _debug _sub_domain
"$_sub_domain"
62 _debug _domain
"$_domain"
63 _debug txtvalue
"$txtvalue"
65 _info
"Getting all existing records"
67 if ! _simply_get_all_records
"$_domain"; then
72 records
=$
(echo "$response" |
tr '{' "\n" |
grep 'record_id\|type\|data\|\name' |
sed 's/\"record_id/;\"record_id/' |
tr "\n" ' ' |
tr -d ' ' |
tr ';' ' ')
74 nr_of_deleted_records
=0
75 _info
"Fetching txt record"
77 for record
in $records; do
78 _debug record
"$record"
80 record_data
=$
(echo "$record" |
sed -n "s/.*\"data\":\"\([^\"]*\)\".*/\1/p")
81 record_type
=$
(echo "$record" |
sed -n "s/.*\"type\":\"\([^\"]*\)\".*/\1/p")
83 _debug2 record_data
"$record_data"
84 _debug2 record_type
"$record_type"
86 if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
88 record_id
=$
(echo "$record" | cut
-d "," -f 1 |
grep "record_id" | cut
-d ":" -f 2)
90 _info
"Deleting record $record"
91 _debug2 record_id
"$record_id"
93 if [ "$record_id" -gt 0 ]; then
95 if ! _simply_delete_record
"$_domain" "$_sub_domain" "$record_id"; then
96 _err
"Record with id $record_id could not be deleted"
100 nr_of_deleted_records
=1
103 _err
"Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
110 if [ "$nr_of_deleted_records" -eq 0 ]; then
111 _err
"No record deleted, the DNS record needs to be removed manually."
113 _info
"Deleted $nr_of_deleted_records record"
119 #################### Private functions below ##################################
121 _simply_load_config
() {
122 SIMPLY_Api
="${SIMPLY_Api:-$(_readaccountconf_mutable SIMPLY_Api)}"
123 SIMPLY_AccountName
="${SIMPLY_AccountName:-$(_readaccountconf_mutable SIMPLY_AccountName)}"
124 SIMPLY_ApiKey
="${SIMPLY_ApiKey:-$(_readaccountconf_mutable SIMPLY_ApiKey)}"
126 if [ -z "$SIMPLY_Api" ]; then
127 SIMPLY_Api
="$SIMPLY_Api_Default"
130 if [ -z "$SIMPLY_AccountName" ] ||
[ -z "$SIMPLY_ApiKey" ]; then
131 SIMPLY_AccountName
=""
134 _err
"A valid Simply API account and apikey not provided."
135 _err
"Please provide a valid API user and try again."
143 _simply_save_config
() {
144 if [ "$SIMPLY_Api" != "$SIMPLY_Api_Default" ]; then
145 _saveaccountconf_mutable SIMPLY_Api
"$SIMPLY_Api"
147 _saveaccountconf_mutable SIMPLY_AccountName
"$SIMPLY_AccountName"
148 _saveaccountconf_mutable SIMPLY_ApiKey
"$SIMPLY_ApiKey"
151 _simply_get_all_records
() {
154 if ! _simply_rest GET
"my/products/$domain/dns/records/"; then
166 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
172 if ! _simply_rest GET
"my/products/$h/dns/"; then
176 if ! _contains
"$response" "$SIMPLY_SUCCESS_CODE"; then
177 _debug
"$h not found"
179 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
189 _simply_add_record
() {
194 data
="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}"
196 if ! _simply_rest POST
"my/products/$domain/dns/records/" "$data"; then
197 _err
"Adding record not successfull!"
201 if ! _contains
"$response" "$SIMPLY_SUCCESS_CODE"; then
202 _err
"Call to API not sucessfull, see below message for more details"
210 _simply_delete_record
() {
215 _debug record_id
"Delete record with id $record_id"
217 if ! _simply_rest DELETE
"my/products/$domain/dns/records/$record_id/"; then
218 _err
"Deleting record not successfull!"
222 if ! _contains
"$response" "$SIMPLY_SUCCESS_CODE"; then
223 _err
"Call to API not sucessfull, see below message for more details"
240 basicauth
=$
(printf "%s:%s" "$SIMPLY_AccountName" "$SIMPLY_ApiKey" | _base64
)
242 if [ "$basicauth" ]; then
243 export _H1
="Authorization: Basic $basicauth"
246 export _H2
="Content-Type: application/json"
248 if [ "$m" != "GET" ]; then
249 response
="$(_post "$data" "$SIMPLY_Api/$ep" "" "$m")"
251 response
="$(_get "$SIMPLY_Api/$ep")"
254 if [ "$?" != "0" ]; then
259 response
="$(echo "$response" | _normalizeJson)"
261 _debug2 response
"$response"
263 if _contains
"$response" "Invalid account authorization"; then
264 _err
"It seems that your api key or accountnumber is not correct."