Browse Source

Implemented dns_dynu_rm()

Implemented dns_dynu_rm() method.
shar0119 8 years ago
parent
commit
9a61d6293d
1 changed files with 86 additions and 7 deletions
  1. 86 7
      dnsapi/dns_dynu.sh

+ 86 - 7
dnsapi/dns_dynu.sh

@@ -35,7 +35,7 @@ dns_dynu_add() {
   _saveaccountconf Dynu_Secret "$Dynu_Secret"
 
   if [ -z "$Dynu_Token" ]; then
-    _info "Getting Dynu token"
+    _info "Getting Dynu token."
     if ! _dynu_authentication; then
       _err "Can not get token."
     fi
@@ -43,29 +43,76 @@ dns_dynu_add() {
 
   _debug "Detect root zone"
   if ! _get_root "$fulldomain"; then
-    _err "Invalid domain"
+    _err "Invalid domain."
     return 1
   fi
 
   _debug _node "$_node"
   _debug _domain_name "$_domain_name"
 
-  _info "Creating TXT record"
+  _info "Creating TXT record."
   if ! _dynu_rest POST "dns/record/add" "{\"domain_name\":\"$_domain_name\",\"node_name\":\"$_node\",\"record_type\":\"TXT\",\"text_data\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
     return 1
   fi
 
   if ! _contains "$response" "text_data"; then
-    _err "Could not add TXT record"
+    _err "Could not add TXT record."
     return 1
   fi
 
   return 0
 }
 
-#fulldomain
+#Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
 dns_dynu_rm() {
   fulldomain=$1
+  txtvalue=$2
+
+  if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
+    Dynu_ClientId=""
+    Dynu_Secret=""
+    _err "Dynu client id and secret is not specified."
+    _err "Please create you API client id and secret and try again."
+    return 1
+  fi
+
+  #save the client id and secret to the account conf file.
+  _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
+  _saveaccountconf Dynu_Secret "$Dynu_Secret"
+
+  if [ -z "$Dynu_Token" ]; then
+    _info "Getting Dynu token."
+    if ! _dynu_authentication; then
+      _err "Can not get token."
+    fi
+  fi
+
+  _debug "Detect root zone."
+  if ! _get_root "$fulldomain"; then
+    _err "Invalid domain."
+    return 1
+  fi
+
+  _debug _node "$_node"
+  _debug _domain_name "$_domain_name"
+
+  _info "Checking for TXT record."
+  if ! _get_recordid "$fulldomain" "$txtvalue"; then
+    _err "Could not get TXT record id."
+    return 1
+  fi
+
+  if [ "$_dns_record_id" = "" ]; then
+    _err "TXT record not found."
+    return 1
+  fi
+
+  _info "Removing TXT record."
+  if ! _delete_txt_record "$_dns_record_id"; then
+    _err "Could not remove TXT record $_dns_record_id."
+  fi
+
+  return 0  
 }
 
 ########  Private functions below ##################################
@@ -80,7 +127,7 @@ _get_root() {
   fi
 
   if ! _contains "$response" "domain_name"; then
-    _debug "Domain name not found"
+    _debug "Domain name not found."
     return 1
   fi
 
@@ -89,6 +136,38 @@ _get_root() {
   return 0
 }
 
+_get_recordid() {
+  fulldomain=$1
+  txtvalue=$2
+
+  if ! _dynu_rest GET "dns/record/get?hostname=$fulldomain&rrtype=TXT"; then
+    return 1
+  fi
+
+  if ! _contains "$response" "$txtvalue"; then
+    _dns_record_id=0
+    return 0
+  fi
+
+  _dns_record_id=$(printf "%s" "$response" grep -o -e "{[^}]*}" | grep '"text_data":"This is another TXT record."' | grep -o -e ",[^,]*," | grep ',"id":' | tr -d ",," | cut -d : -f 2)
+
+  return 0
+}
+
+_delete_txt_record() {
+  _dns_record_id=$1
+
+  if ! _dynu_rest GET "dns/record/delete/$_dns_record_id"; then
+    return 1
+  fi
+
+  if ! _contains "$response" "true"; then
+    return 1
+  fi
+
+  return 0
+}
+
 _dynu_rest() {
   m=$1
   ep="$2"
@@ -102,7 +181,7 @@ _dynu_rest() {
     _debug data "$data"
     response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
   else
-    echo "Getting $Dynu_EndPoint/$ep"
+    _info "Getting $Dynu_EndPoint/$ep"
     response="$(_get "$Dynu_EndPoint/$ep")"
   fi