provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.8 KiB

7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
7 months ago
6 months ago
  1. #!/bin/bash
  2. #
  3. # GET DOMAIN
  4. # https://eu.api.ovh.com/createToken/?GET=/domain*&POST=/domain*&PUT=/domain*&DELETE=/domain*
  5. function usage {
  6. echo 'usage: <ACTION: list|get|set> <DOMAIN> [CNAME] [destination]'
  7. echo ' - list <DOMAIN>'
  8. echo ' - get <DOMAIN> <CNAME>'
  9. echo ' - set <DOMAIN> <CNAME> <DESTINATION>'
  10. exit 1
  11. }
  12. function set_base {
  13. BASE_URL="https://eu.api.ovh.com/1.0"
  14. BASEDIR=$(dirname "$0")
  15. source "$BASEDIR"/.ovh-credential
  16. }
  17. function build_url_list_records {
  18. zone="$1"
  19. result="${BASE_URL}/domain/zone/$zone/record"
  20. fieldType="${2:-}"
  21. subDomain="${3:-}"
  22. any="$fieldType$subDomain"
  23. [[ -n $any ]] && result+="?"
  24. [[ -n $fieldType ]] && result+="fieldType=$fieldType&"
  25. [[ -n $subDomain ]] && result+="subDomain=$subDomain&"
  26. result=${result::-1}
  27. echo "$result"
  28. }
  29. function list_records {
  30. zone="$1"
  31. fieldType="${2:-}"
  32. subDomain="${3:-}"
  33. query=$(build_url_list_records "$zone" "$fieldType" "$subDomain")
  34. method="GET"
  35. body=""
  36. tstamp=$(date +%s)
  37. sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1)
  38. signature="\$1\$$sha"
  39. echo BEFORE CURL
  40. output=$(curl -s \
  41. -X $method \
  42. -H "Content-type: application/json" \
  43. -H "X-Ovh-Application: $AK" \
  44. -H "X-Ovh-Consumer: $CK" \
  45. -H "X-Ovh-Signature: $signature" \
  46. -H "X-Ovh-Timestamp: $tstamp" \
  47. "$query")
  48. echo ""
  49. echo "output=$output"
  50. echo ""
  51. exit 1
  52. # echo "$output" | yq .[] # values of array
  53. }
  54. function get_record {
  55. zone="$1"
  56. record="$2"
  57. method="GET"
  58. query="${BASE_URL}/domain/zone/$zone/record/$record"
  59. body=""
  60. tstamp=$(date +%s)
  61. sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1)
  62. signature="\$1\$$sha"
  63. curl -s \
  64. -X $method \
  65. -H "Content-type: application/json" \
  66. -H "X-Ovh-Application: $AK" \
  67. -H "X-Ovh-Consumer: $CK" \
  68. -H "X-Ovh-Signature: $signature" \
  69. -H "X-Ovh-Timestamp: $tstamp" \
  70. "$query" | yq -o=props
  71. }
  72. function action_list {
  73. for record_id in $(list_records "$DOMAIN" CNAME "$CNAME"); do
  74. get_record "$DOMAIN" "$record_id"
  75. done
  76. }
  77. function action_get {
  78. local record_id
  79. record_id=$(list_records "$DOMAIN" CNAME "$CNAME")
  80. get_record "$DOMAIN" "$record_id"
  81. }
  82. function action_set {
  83. echo set_record "$DOMAIN" "$CNAME" "$DESTINATION"
  84. }
  85. ##-----------
  86. ## -- MAIN --
  87. ##-----------
  88. . "$MIAOU_BASEDIR/lib/init.sh"
  89. [[ $# -lt 2 ]] && usage && exit 1
  90. ACTION="$1"
  91. DOMAIN="$2"
  92. CNAME="${3:-}"
  93. DESTINATION="${4:-}"
  94. set_base
  95. case $ACTION in
  96. list)
  97. action_list
  98. ;;
  99. get)
  100. arg3_required "$@" || usage
  101. action_get
  102. ;;
  103. set)
  104. arg4_required "$@" || usage
  105. action_set
  106. ;;
  107. *)
  108. echoerr "unknown action <$ACTION> !" && usage && exit 2
  109. ;;
  110. esac