Browse Source

cleanup dns in dnsimple api integration

Implement the `_rm()` method for the DNSimple integration. This also
required some changes and cleanup to DRY up the code.
Matthew Turney 8 years ago
parent
commit
f9b419d1e4
1 changed files with 72 additions and 24 deletions
  1. 72 24
      dnsapi/dns_dnsimple.sh

+ 72 - 24
dnsapi/dns_dnsimple.sh

@@ -26,49 +26,35 @@ dns_dnsimple_add() {
   # save the oauth token for later
   # save the oauth token for later
   _saveaccountconf DNSimple_OAUTH_TOKEN "$DNSimple_OAUTH_TOKEN"
   _saveaccountconf DNSimple_OAUTH_TOKEN "$DNSimple_OAUTH_TOKEN"
 
 
-  _debug "Retrive account ID"
   if ! _get_account_id; then
   if ! _get_account_id; then
     _err "failed to retrive account id"
     _err "failed to retrive account id"
     return 1
     return 1
   fi
   fi
-  _debug _account_id "$_account_id"
 
 
   if ! _get_root "$fulldomain"; then
   if ! _get_root "$fulldomain"; then
     _err "invalid domain"
     _err "invalid domain"
     return 1
     return 1
   fi
   fi
-  _debug _domain "$_domain"
-  _debug _sub_domain "$_sub_domain"
-
-  _debug "Getting txt records"
-  _dnsimple_rest GET "$_account_id/zones/$_domain/records?per_page=100"
-
-  if ! _contains "$response" "\"id\":"; then
-    _err "Error"
-    return 1
-  fi
 
 
-  count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$_sub_domain\"" | wc -l | _egrep_o "[0-9]+")
-  _debug count "$count"
+  _get_records $_account_id $_domain $_sub_domain
 
 
-  if [ "$count" = "0" ]; then
+  if [ "$_records_count" = "0" ]; then
     _info "Adding record"
     _info "Adding record"
     if _dnsimple_rest POST "$_account_id/zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
     if _dnsimple_rest POST "$_account_id/zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
       if printf -- "%s" "$response" | grep "\"name\":\"$_sub_domain\"" >/dev/null; then
       if printf -- "%s" "$response" | grep "\"name\":\"$_sub_domain\"" >/dev/null; then
         _info "Added"
         _info "Added"
         return 0
         return 0
       else
       else
-        _err "Add txt record error."
+        _err "Unexpected response while adding text record."
         return 1
         return 1
       fi
       fi
     fi
     fi
     _err "Add txt record error."
     _err "Add txt record error."
   else
   else
     _info "Updating record"
     _info "Updating record"
-    record_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
-    _debug "record_id" "$record_id"
+    _extract_record_id $_records $_sub_domain
 
 
-    _dnsimple_rest PATCH "$_account_id/zones/$_domain/records/$record_id" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"
+    _dnsimple_rest PATCH "$_account_id/zones/$_domain/records/$_record_id" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"
     if [ "$?" = "0" ]; then
     if [ "$?" = "0" ]; then
       _info "Updated!"
       _info "Updated!"
       #todo: check if the record takes effect
       #todo: check if the record takes effect
@@ -83,6 +69,31 @@ dns_dnsimple_add() {
 dns_dnsimple_rm() {
 dns_dnsimple_rm() {
   fulldomain=$1
   fulldomain=$1
 
 
+  if ! _get_account_id; then
+    _err "failed to retrive account id"
+    return 1
+  fi
+
+  if ! _get_root "$fulldomain"; then
+    _err "invalid domain"
+    return 1
+  fi
+
+  _get_records $_account_id $_domain $_sub_domain
+  _extract_record_id $_records $_sub_domain
+
+  if [ "$_record_id" ]; then
+    _dnsimple_rest DELETE "$_account_id/zones/$_domain/records/$_record_id"
+
+    if [ "$?" = "0" ]; then
+      _info "removed record" "$_record_id"
+      return 0
+    fi
+  fi
+
+  _err "failed to remove record" "$_record_id"
+  return 1
+
 }
 }
 
 
 ####################  Private functions bellow ##################################
 ####################  Private functions bellow ##################################
@@ -93,7 +104,7 @@ dns_dnsimple_rm() {
 _get_root() {
 _get_root() {
   domain=$1
   domain=$1
   i=2
   i=2
-  p=1
+  previous=1
   while true; do
   while true; do
     h=$(printf "%s" "$domain" | cut -d . -f $i-100)
     h=$(printf "%s" "$domain" | cut -d . -f $i-100)
     if [ -z "$h" ]; then
     if [ -z "$h" ]; then
@@ -108,17 +119,24 @@ _get_root() {
     if _contains "$response" 'not found'; then
     if _contains "$response" 'not found'; then
       _debug "$h not found"
       _debug "$h not found"
     else
     else
-      _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
+      _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$previous)
       _domain="$h"
       _domain="$h"
+
+      _debug _domain "$_domain"
+      _debug _sub_domain "$_sub_domain"
+
       return 0
       return 0
     fi
     fi
-    p="$i"
+
+    previous="$i"
     i=$(_math "$i" + 1)
     i=$(_math "$i" + 1)
   done
   done
   return 1
   return 1
 }
 }
 
 
+# returns _account_id
 _get_account_id() {
 _get_account_id() {
+  _debug "retrive account id"
   if ! _dnsimple_rest GET "whoami"; then
   if ! _dnsimple_rest GET "whoami"; then
     return 1
     return 1
   fi
   fi
@@ -129,14 +147,44 @@ _get_account_id() {
   fi
   fi
 
 
   if _contains "$response" "timeout"; then
   if _contains "$response" "timeout"; then
-    _err "timeout retrieving account_id"
+    _err "timeout retrieving account id"
     return 1
     return 1
   fi
   fi
 
 
   _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
   _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
+  _debug _account_id "$_account_id"
+
   return 0
   return 0
 }
 }
 
 
+# returns
+#   _records
+#   _records_count
+_get_records() {
+  account_id=$1
+  domain=$2
+  sub_domain=$3
+
+  _debug "fetching txt records"
+  _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=100"
+
+  if ! _contains "$response" "\"id\":"; then
+    _err "failed to retrieve records"
+    return 1
+  fi
+
+  _records_count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$sub_domain\"" | wc -l | _egrep_o "[0-9]+")
+  _records=$response
+  _debug _records_count "$_records_count"
+}
+
+# returns _record_id
+_extract_record_id() {
+  _record_id=$(printf "%s" "$_records" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
+  _debug "_record_id" "$_record_id"
+}
+
+# returns response
 _dnsimple_rest() {
 _dnsimple_rest() {
   method=$1
   method=$1
   path="$2"
   path="$2"
@@ -151,7 +199,7 @@ _dnsimple_rest() {
     _debug data "$data"
     _debug data "$data"
     response="$(_post "$data" "$request_url" "" "$method")"
     response="$(_post "$data" "$request_url" "" "$method")"
   else
   else
-    response="$(_get "$request_url")"
+    response="$(_request "$request_url" "" "" "$method")"
   fi
   fi
 
 
   if [ "$?" != "0" ]; then
   if [ "$?" != "0" ]; then