diff --git a/dnsapi/dns_bluemix.sh b/dnsapi/dns_bluemix.sh index ee2c173f..8d8bc545 100755 --- a/dnsapi/dns_bluemix.sh +++ b/dnsapi/dns_bluemix.sh @@ -8,6 +8,7 @@ # ######## Public functions ##################### +BLUEMIX_API_URL="https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3" domainId= domain= host= @@ -26,13 +27,13 @@ dns_bluemix_add() { fi # BLUEMIX_USER is required - if [[ "${BLUEMIX_USER}" == "" ]]; then + if [[ -z "${BLUEMIX_USER}" ]]; then _err "Environment variable BLUEMIX_USER not defined" return 1 fi # BLUEMIX_KEY is required - if [[ "${BLUEMIX_KEY}" == "" ]]; then + if [[ -z "${BLUEMIX_KEY}" ]]; then _err "Environment variable BLUEMIX_KEY not defined" return 1 fi @@ -41,14 +42,14 @@ dns_bluemix_add() { getDomain ${fulldomain} # Did we find domain? - if [[ "${domain}" == "" ]]; then + if [[ -z "${domain}" ]]; then return 1 fi # Check if this DNS entry already exists getRecordId "${domainId}" "${host}" - if [[ "${recordId}" == "" ]]; then + if [[ -z "${recordId}" ]]; then # Create record if it doesn't exist createTxtRecord "${domainId}" "${host}" "${txtvalue}" else @@ -74,13 +75,13 @@ dns_bluemix_rm() { fi # BLUEMIX_USER is required - if [[ "${BLUEMIX_USER}" == "" ]]; then + if [[ -z "${BLUEMIX_USER}" ]]; then _err "Environment variable BLUEMIX_USER not defined" return 1 fi # BLUEMIX_KEY is required - if [[ "${BLUEMIX_KEY}" == "" ]]; then + if [[ -z "${BLUEMIX_KEY}" ]]; then _err "Environment variable BLUEMIX_KEY not defined" return 1 fi @@ -88,14 +89,14 @@ dns_bluemix_rm() { # Get Domain ID getDomain ${fulldomain} - if [[ "${domain}" == "" ]]; then + if [[ -z "${domain}" ]]; then return 1 fi # Get DNS entry in this Domain getRecordId "${domainId}" "${host}" - if [[ "${recordId}" == "" ]]; then + if [[ -z "${recordId}" ]]; then _info "recordId for ${fulldomain} not found." return 1 fi @@ -112,26 +113,43 @@ dns_bluemix_rm() { function getDomain { fulldomain=$1 - output=$(curl -s -X GET "https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3/SoftLayer_Account/getDomains") + output=$(curl -s -X GET "${BLUEMIX_API_URL}/SoftLayer_Account/getDomains") - if echo "${output}" | grep '"error":"Access Denied. "' >/dev/null; then + if [[ "${output}" =~ '"error":"Access Denied. "' ]]; then _err "Access Denied, check BLUEMIX_USER and BLUEMIX_KEY environment variables. Details: ${output}" return 1 fi for domain_item in $(echo "${output}" | awk 'BEGIN{RS=","}/"name"/' | cut -f4 -d'"'); do - if echo "${fulldomain}" | grep "${domain_item}$" >/dev/null; then + if [[ "${fulldomain}" =~ ${domain_item}$ ]]; then domain="${domain_item}" break fi done - if [[ "${domain}" == "" ]]; then + if [[ -z "${domain}" ]]; then _err "Domain for ${fulldomain} was not found in this Bluemix account" return 1 fi - domainId=$(echo "${output}" | awk -v DOMAIN=${domain} 'BEGIN{RS=",";FS=":"}{if($1~"\"id\""){id=$2}else if($1~"\"name\""){split($2,d,"\"");domain=d[2];} if($0~/\}$/ && domain==DOMAIN){print id}}') + domainId=$(echo "${output}" | \ + awk -v DOMAIN=${domain} ' + BEGIN { + RS=","; + FS=":"; + } + { + if($1~"\"id\"") { + id=$2; + } else if($1~"\"name\"") { + split($2,d,"\""); + domain=d[2]; + } + if($0~/\}$/ && domain==DOMAIN) { + print id; + } + } + ') host=$(echo "${fulldomain}" | sed "s/\.${domain}\$//g") @@ -143,9 +161,25 @@ function getRecordId { domainId=$1 host=$2 - output=$(curl -s -X GET "https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3/SoftLayer_Dns_Domain/${domainId}/getResourceRecords") - - recordId=$(echo "${output}" | awk -v HOST=${host} 'BEGIN{RS=",";FS=":"}{if($1=="\"host\""){host=$2}else if($1=="\"id\""){id=$2} if($0~/[\}|\]]$/ && host==("\"" HOST "\"")){print id}}') + output=$(curl -s -X GET "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain/${domainId}/getResourceRecords") + + recordId=$(echo "${output}" | \ + awk -v HOST=${host} ' + BEGIN { + RS=","; + FS=":"; + } + { + if($1=="\"host\"") { + host=$2; + } else if($1=="\"id\"") { + id=$2; + } + if($0~/[\}|\]]$/ && host==("\"" HOST "\"")) { + print id; + } + } + ') _debug "RecordId is ${recordId}" @@ -156,14 +190,15 @@ function createTxtRecord { host=$2 txtvalue=$3 - output=$(curl -s -X POST -d "{\"parameters\":[{\"host\":\"${host}\",\"data\":\"${txtvalue}\",\"ttl\":\"900\",\"type\":\"txt\",\"domainId\":\"${domainId}\"}]}" \ - "https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3/SoftLayer_Dns_Domain_ResourceRecord") + payload="{\"parameters\":[{\"host\":\"${host}\",\"data\":\"${txtvalue}\",\"ttl\":\"900\",\"type\":\"txt\",\"domainId\":\"${domainId}\"}]}" + output=$(curl -s -X POST -d "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord") + rc=$? - if echo "${output}" | grep '^\{"error"' >/dev/null; then - _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}" - else + if [[ "${rc}" == "0" && "${output}" =~ \"host\":\"${host}\" ]]; then _info "${fulldomain} added into Bluemix's DNS." _debug ${output} + else + _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}" fi } @@ -171,22 +206,24 @@ function updateTxtRecord { recordId=$1 txtvalue=$2 - output=$(curl -s -X PUT -d "{\"parameters\":[{\"data\":\"${txtvalue}\"}]}" \ - "https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3/SoftLayer_Dns_Domain_ResourceRecord/${recordId}") + payload="{\"parameters\":[{\"data\":\"${txtvalue}\"}]}" + output=$(curl -s -X PUT -d "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}") + rc=$? - if echo "${output}" | grep '^\{"error"' >/dev/null; then - _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}" - else + if [[ "${rc}" == "0" && "${output}" == "true" ]]; then _info "${fulldomain} updated in Bluemix's DNS." + else + _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}" fi } function deleteRecordId { recordId=$1 - output=$(curl -s -X DELETE "https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3/SoftLayer_Dns_Domain_ResourceRecord/${recordId}") + output=$(curl -s -X DELETE "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}") + rc=$? - if [[ "${output}" == "true" ]]; then + if [[ "${rc}" == "0" && "${output}" == "true" ]]; then _info "${fulldomain} deleted from Bluemix's DNS." else _err "Error deleting ${fulldomain}. Details: ${output}."