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