From 63e31196b2ccb0d4fd3dacbef6830c450352eae8 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Sun, 9 Sep 2018 20:47:01 +0200 Subject: [PATCH] refactoring and fixes --- dnsapi/dns_plesk.sh | 85 ++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/dnsapi/dns_plesk.sh b/dnsapi/dns_plesk.sh index 1a5647b5..963d8bd3 100644 --- a/dnsapi/dns_plesk.sh +++ b/dnsapi/dns_plesk.sh @@ -15,29 +15,16 @@ dns_plesk_add() { fulldomain=$1 txtvalue=$2 - PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" - PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" - PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" - - if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then - PLESK_Host="" - PLESK_User="" - PLESK_Password="" - _err "You didn't specify a plesk credentials yet." - _err "Please create the key and try again." + if ! init_config; then return 1 fi - #save the api key and email to the account conf file. - _saveaccountconf_mutable PLESK_Host "$PLESK_Host" - _saveaccountconf_mutable PLESK_User "$PLESK_User" - _saveaccountconf_mutable PLESK_Password "$PLESK_Password" - _debug "First detect the root zone" if ! _get_root "$fulldomain"; then _err "invalid domain" return 1 fi + _debug _domain_id "$_domain_id" _debug _sub_domain "$_sub_domain" _debug _domain "$_domain" @@ -52,11 +39,31 @@ dns_plesk_rm() { fulldomain=$1 txtvalue=$2 - PLESK_Host="${CF_Key:-$(_readaccountconf_mutable PLESK_Host)}" - PLESK_User="${CF_Key:-$(_readaccountconf_mutable PLESK_User)}" - PLESK_Password="${CF_Key:-$(_readaccountconf_mutable PLESK_Password)}" + if ! init_config; then + return 1 + fi - if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password"]; then + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug _domain_id "$_domain_id" + _debug _sub_domain "$_sub_domain" + _debug _domain "$_domain" + + _info "Remove record" + del_txt_record $_domain_id $fulldomain +} + +#################### Private functions below ################################## +function init_config(){ + PLESK_Host="${PLESK_Host:-$(_readaccountconf_mutable PLESK_Host)}" + PLESK_User="${PLESK_User:-$(_readaccountconf_mutable PLESK_User)}" + PLESK_Password="${PLESK_Password:-$(_readaccountconf_mutable PLESK_Password)}" + + if [ -z "$PLESK_Host" ] || [ -z "$PLESK_User" ] || [ -z "$PLESK_Password" ]; then PLESK_Host="" PLESK_User="" PLESK_Password="" @@ -69,21 +76,9 @@ dns_plesk_rm() { _saveaccountconf_mutable PLESK_Host "$PLESK_Host" _saveaccountconf_mutable PLESK_User "$PLESK_User" _saveaccountconf_mutable PLESK_Password "$PLESK_Password" - - _debug "First detect the root zone" - if ! _get_root "$fulldomain"; then - _err "invalid domain" - return 1 - fi - _debug _domain_id "$_domain_id" - _debug _sub_domain "$_sub_domain" - _debug _domain "$_domain" - - _info "Remove record" - del_txt_record $_domain_id $fulldomain + } - - + function plesk_api() { local request="$1" @@ -106,9 +101,9 @@ function add_txt_record() { plesk_api $request if ! _contains "${response}" 'ok'; then - return 1 + return 1 fi - return 0 + return 0 } function del_txt_record() { @@ -137,6 +132,7 @@ function del_txt_record() { return 0 } +#fetches the domain list for the given account function get_domain_list() { local request='' @@ -148,10 +144,11 @@ function get_domain_list() { _plesk_domain_names=($(echo "${response}" | sed -nr 's_(.*)_\1_p')); _plesk_domain_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); - _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") + _plesk_domain_ids=("${_plesk_domain_ids[@]:1}") #remove first entry because it is the customer id } +#fetches all dns records fo rthe given sit function get_dns_record_list() { local siteid=$1 local request="$siteid" @@ -166,21 +163,7 @@ function get_dns_record_list() { _plesk_dns_ids=($(echo "${response}"| sed -nr 's_(.*)_\1_p')); } - - #urls=($(sed -nr 's_(.*)_\1_p' resp.txt)); echo ${urls[@]} - -#domain.com -#returns -# _plesk_site_id=22 -function get_site_id() { - local site_name=$1 - request="$site_name" - plesk_api $request - echo $resonse - _plesk_site_id="$(echo $response | grep -Po '(?<=).*?(?=)')" - return 0 -} - + #_acme-challenge.www.domain.com #returns # _sub_domain=_acme-challenge.www