- zone-list, record-list, record-create, record-update, record-delete - Named instance support (-a flag) with configurable default - Zone name-to-ID auto-resolution in shared _lib.sh - Updated credentials loader with cloudflare/cloudflare-<name> services - TOOLS.md and INFRASTRUCTURE.md guide documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
2.5 KiB
Bash
Executable File
87 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# record-update.sh — Update a DNS record in a Cloudflare zone
|
|
#
|
|
# Usage: record-update.sh -z <zone> -r <record-id> -t <type> -n <name> -c <content> [-a instance] [-l ttl] [-p] [-P priority]
|
|
#
|
|
# Options:
|
|
# -z zone Zone name or ID (required)
|
|
# -r record-id DNS record ID (required)
|
|
# -t type Record type: A, AAAA, CNAME, MX, TXT, etc. (required)
|
|
# -n name Record name (required)
|
|
# -c content Record value/content (required)
|
|
# -a instance Cloudflare instance name (default: uses credentials default)
|
|
# -l ttl TTL in seconds (default: 1 = auto)
|
|
# -p Enable Cloudflare proxy (orange cloud)
|
|
# -P priority MX/SRV priority
|
|
# -h Show this help
|
|
set -euo pipefail
|
|
|
|
MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
|
|
source "$MOSAIC_HOME/tools/_lib/credentials.sh"
|
|
source "$(dirname "$0")/_lib.sh"
|
|
|
|
ZONE=""
|
|
INSTANCE=""
|
|
RECORD_ID=""
|
|
TYPE=""
|
|
NAME=""
|
|
CONTENT=""
|
|
TTL=1
|
|
PROXIED=false
|
|
PRIORITY=""
|
|
|
|
while getopts "z:a:r:t:n:c:l:pP:h" opt; do
|
|
case $opt in
|
|
z) ZONE="$OPTARG" ;;
|
|
a) INSTANCE="$OPTARG" ;;
|
|
r) RECORD_ID="$OPTARG" ;;
|
|
t) TYPE="$OPTARG" ;;
|
|
n) NAME="$OPTARG" ;;
|
|
c) CONTENT="$OPTARG" ;;
|
|
l) TTL="$OPTARG" ;;
|
|
p) PROXIED=true ;;
|
|
P) PRIORITY="$OPTARG" ;;
|
|
h) head -18 "$0" | grep "^#" | sed 's/^# \?//'; exit 0 ;;
|
|
*) echo "Usage: $0 -z <zone> -r <record-id> -t <type> -n <name> -c <content> [-a instance]" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
if [[ -z "$ZONE" || -z "$RECORD_ID" || -z "$TYPE" || -z "$NAME" || -z "$CONTENT" ]]; then
|
|
echo "Error: -z, -r, -t, -n, and -c are all required" >&2
|
|
exit 1
|
|
fi
|
|
|
|
cf_load_instance "$INSTANCE"
|
|
ZONE_ID=$(cf_resolve_zone "$ZONE") || exit 1
|
|
|
|
payload=$(jq -n \
|
|
--arg type "$TYPE" \
|
|
--arg name "$NAME" \
|
|
--arg content "$CONTENT" \
|
|
--argjson ttl "$TTL" \
|
|
--argjson proxied "$PROXIED" \
|
|
'{type: $type, name: $name, content: $content, ttl: $ttl, proxied: $proxied}')
|
|
|
|
if [[ -n "$PRIORITY" ]]; then
|
|
payload=$(echo "$payload" | jq --argjson priority "$PRIORITY" '. + {priority: $priority}')
|
|
fi
|
|
|
|
response=$(curl -s -w "\n%{http_code}" \
|
|
-X PUT \
|
|
-H "Authorization: $(cf_auth)" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload" \
|
|
"${CF_API}/zones/${ZONE_ID}/dns_records/${RECORD_ID}")
|
|
|
|
http_code=$(echo "$response" | tail -n1)
|
|
body=$(echo "$response" | sed '$d')
|
|
|
|
if [[ "$http_code" != "200" ]]; then
|
|
echo "Error: Failed to update record (HTTP $http_code)" >&2
|
|
echo "$body" | jq -r '.errors[]?.message // empty' 2>/dev/null >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "Updated $TYPE record: $NAME → $CONTENT (ID: $RECORD_ID)"
|