updated shell_link.sh
713e0145
Hari Sekhon
committed
1 changed file
shell_link.sh
/setup/shell_link.sh+64
/setup/shell_link.sh
Add comment 1 Plus  #!/usr/bin/env bash
Add comment 2 Plus  # vim:ts=4:sts=4:sw=4:et
Add comment 3 Plus  #
Add comment 4 Plus  # Author: Hari Sekhon
Add comment 5 Plus  # Date: 2024-02-13 21:54:58 +0000 (Tue, 13 Feb 2024)
Add comment 6 Plus  #
Add comment 7 Plus  # https://github.com/HariSekhon/DevOps-Bash-tools
Add comment 8 Plus  #
Add comment 9 Plus  # License: see accompanying Hari Sekhon LICENSE file
Add comment 10 Plus  #
Add comment 11 Plus  # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
Add comment 12 Plus  #
Add comment 13 Plus  # https://www.linkedin.com/in/HariSekhon
Add comment 14 Plus  #
Add comment 15 Plus  
Add comment 16 Plus  set -euo pipefail
Add comment 17 Plus  [ -n "${DEBUG:-}" ] && set -x
Add comment 18 Plus  srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Add comment 19 Plus  
Add comment 20 Plus  # shellcheck disable=SC1090,SC1091
Add comment 21 Plus  . "$srcdir/lib/utils.sh"
Add comment 22 Plus  
Add comment 23 Plus  # shellcheck disable=SC2034,SC2154
Add comment 24 Plus  usage_description="
Add comment 25 Plus  Gets details for a DNS record in the given domain
Add comment 26 Plus  
Add comment 27 Plus  Details are given in JSON for further pipe processing
Add comment 28 Plus  
Add comment 29 Plus  Resolves the DNS record and then submits the request to delete the requested record
Add comment 30 Plus  
Add comment 31 Plus  https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-delete-dns-record
Add comment 32 Plus  "
Add comment 33 Plus  
Add comment 34 Plus  # used by usage() in lib/utils.sh
Add comment 35 Plus  # shellcheck disable=SC2034
Add comment 36 Plus  usage_args="<domain> <hostname>"
Add comment 37 Plus  
Add comment 38 Plus  help_usage "$@"
Add comment 39 Plus  
Add comment 40 Plus  num_args 2 "$@"
Add comment 41 Plus  
Add comment 42 Plus  domain="$1"
Add comment 43 Plus  hostname="$2"
Add comment 44 Plus  
Add comment 45 Plus  zone_id="$("$srcdir/cloudflare_zones.sh" |
Add comment 46 Plus   grep -E "^[[:alnum:]]+[[:space:]]+$domain$" |
Add comment 47 Plus   sed 's/[[:space:]].*$//' ||
Add comment 48 Plus   die "Failed to resolved zone id for domain '$domain' - is this the right domain name?")"
Add comment 49 Plus  
Add comment 50 Plus  if [ -z "$zone_id" ]; then
Add comment 51 Plus   die "Zone ID is empty, check code"
Add comment 52 Plus  fi
Add comment 53 Plus  
Add comment 54 Plus  dns_record_id="$(
Add comment 55 Plus   "$srcdir/cloudflare_api.sh" "/zones/$zone_id/dns_records?per_page=50000" |
Add comment 56 Plus   jq -r '.result[] | [.id, .name] | @tsv' |
Add comment 57 Plus   grep -E "^[[:alnum:]]+[[:space:]]+$hostname" |
Add comment 58 Plus   head -n 1 |
Add comment 59 Plus   sed 's/[[:space:]].*$//' ||
Add comment 60 Plus   die "Failed to record DNS record '$hostname' in domain '$domain' - are the hostname and domain name correct?"
Add comment 61 Plus  )"
Add comment 62 Plus  
Add comment 63 Plus  "$srcdir/cloudflare_api.sh" "zones/$zone_id/dns_records/$dns_record_id"
Add comment 64 Plus