Created
April 5, 2016 18:07
-
-
Save chorn/e70608c04de8330fdd3029ef666521f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare API | |
API="https://api.dnsimple.com/v2" | |
declare _zone | |
_zone="$1" | |
declare _record | |
_record="$2" | |
declare _ip | |
_ip="$3" | |
if [[ -z "$DNSIMPLE_ACCOUNT_TOKEN" ]] ; then | |
echo "You must create a 'User access token' on your dnsimple.com User Settings page." | |
echo "The new ACCOUNT token must be defined as \$DNSIMPLE_ACCOUNT_TOKEN in your environment." | |
exit 1 | |
fi | |
if ! command -v jq >&/dev/null ; then | |
echo "You need to have 'jq' installed and in your $PATH." | |
echo "https://stedolan.github.io/jq/" | |
exit 1 | |
fi | |
if [[ -z "$_zone" ]] ; then | |
echo "Specify which zone (domain name) you want to use." | |
exit 1 | |
fi | |
if [[ -z "$_record" ]] ; then | |
echo "Specify which hostname you want to create/update." | |
exit 1 | |
fi | |
if [[ -z "$_ip" ]] ; then | |
_ip=$(dig +short myip.opendns.com @resolver1.opendns.com || curl -f4L http://ifconfig.me/ || curl -f4L http://icanhazip.com/) | |
fi | |
if [[ -z "$_ip" ]] ; then | |
echo "No IP to use has been defined, setting it automatically doesn't work right now." | |
exit 1 | |
fi | |
declare _config=$(mktemp -t dnsimple) || exit 1 | |
declare _json_data=$(mktemp -t dnsimple_json) || exit 1 | |
dnsimple_api() { | |
local _resource="$1" | |
local _method="$2" | |
local -a _args=( | |
[1]="silent" | |
[2]="header = \"Authorization: Bearer ${DNSIMPLE_ACCOUNT_TOKEN}\"" | |
[3]="header = \"Accept: application/json\"" | |
) | |
[[ -n "$_method" ]] && _args+=([10]="header = \"Content-Type: application/json\"" [11]="request = \"${_method}\"") | |
[[ -s "$_json_data" ]] && _args+=([20]="data = \"@${_json_data}\"") | |
_args+=([30]="url = \"${API}/${_resource}\"") | |
$(IFS=$'\n' ; echo "${_args[*]}" > "$_config") | |
curl --config "$_config" | |
} | |
# DNSIMPLE_ACCOUNT_ID=$(dnsimple_api "whoami" | jq '.data.account.id') | |
DNSIMPLE_ACCOUNT_ID="_" | |
zones() { | |
dnsimple_api "${DNSIMPLE_ACCOUNT_ID}/domains" | jq '.' | |
} | |
zone_info() { | |
dnsimple_api "${DNSIMPLE_ACCOUNT_ID}/zones/${_zone}" | jq '.' | |
} | |
zone_records() { | |
dnsimple_api "${DNSIMPLE_ACCOUNT_ID}/zones/${_zone}/records" | jq '.' | |
} | |
get_zone_record() { | |
dnsimple_api "${DNSIMPLE_ACCOUNT_ID}/zones/${_zone}/records" | jq -cMj ".data[] | select(.name == \"${_record}\")" | |
} | |
zone_record_id() { | |
echo "$_zone_record" | jq -cMj ".id" | |
} | |
declare _zone_record | |
_zone_record=$(get_zone_record) | |
declare _id | |
_id=$(zone_record_id) | |
send_zone_record() { | |
local _method="$1" | |
echo "{\"name\":\"${_record}\",\"type\":\"A\",\"content\":\"${_ip}\"}" > "$_json_data" | |
dnsimple_api "${DNSIMPLE_ACCOUNT_ID}/zones/${_zone}/records/${_id}" "${_method}" | jq '.' | |
rm "$_json_data" | |
} | |
if [[ -z "$_id" ]] ; then | |
send_zone_record POST | |
else | |
send_zone_record PATCH | |
fi | |
rm "$_config" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment