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.

189 lines
4.4 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 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] [TARGET]'
  7. echo ' - list <DOMAIN>'
  8. echo ' - get <DOMAIN> <CNAME>'
  9. echo ' - set <DOMAIN> <CNAME> <TARGET>'
  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 "ERROR"
  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 set_record {
  73. zone="$1"
  74. method="POST"
  75. query="${BASE_URL}/domain/zone/$zone/record"
  76. body="{\"fieldType\":\"CNAME\",\"subDomain\":\"$CNAME\",\"target\":\"$TARGET\",\"ttl\":0}"
  77. tstamp=$(date +%s)
  78. sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1)
  79. signature="\$1\$$sha"
  80. curl -s \
  81. -X $method \
  82. -H "Content-type: application/json" \
  83. -H 'Accept: application/json' \
  84. -H "X-Ovh-Application: $AK" \
  85. -H "X-Ovh-Consumer: $CK" \
  86. -H "X-Ovh-Signature: $signature" \
  87. -H "X-Ovh-Timestamp: $tstamp" \
  88. --data-raw "$body" \
  89. "$query" | yq -o=props
  90. }
  91. function refresh {
  92. zone="$1"
  93. method="POST"
  94. query="${BASE_URL}/domain/zone/$zone/refresh"
  95. body=""
  96. tstamp=$(date +%s)
  97. sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1)
  98. signature="\$1\$$sha"
  99. curl -s \
  100. -X $method \
  101. -H "Content-type: application/json" \
  102. -H 'Accept: application/json' \
  103. -H "X-Ovh-Application: $AK" \
  104. -H "X-Ovh-Consumer: $CK" \
  105. -H "X-Ovh-Signature: $signature" \
  106. -H "X-Ovh-Timestamp: $tstamp" \
  107. "$query" | yq -o=props
  108. }
  109. function action_list {
  110. for record_id in $(list_records "$DOMAIN" CNAME "$CNAME"); do
  111. get_record "$DOMAIN" "$record_id"
  112. done
  113. }
  114. function action_get {
  115. local record_id
  116. #TODO: to delete after testing...
  117. list_records "$DOMAIN" CNAME "$CNAME"
  118. record_id=$(list_records "$DOMAIN" CNAME "$CNAME")
  119. get_record "$DOMAIN" "$record_id"
  120. }
  121. function append_dot_target {
  122. local old_target=$TARGET
  123. [[ $TARGET == *\. ]] || TARGET="$TARGET."
  124. [[ $old_target != "$TARGET" ]] && echo "TARGET has been suffixed with '.' (the dot symbol)!"
  125. }
  126. function action_set {
  127. append_dot_target
  128. echo set_record "$DOMAIN" "$CNAME" "$TARGET"
  129. set_record "$DOMAIN" "$CNAME" "$TARGET"
  130. echo refresh "$DOMAIN"
  131. refresh "$DOMAIN"
  132. }
  133. ##-----------
  134. ## -- MAIN --
  135. ##-----------
  136. . "$MIAOU_BASEDIR/lib/init.sh"
  137. [[ $# -lt 2 ]] && usage && exit 1
  138. ACTION="$1"
  139. DOMAIN="$2"
  140. CNAME="${3:-}"
  141. TARGET="${4:-}"
  142. set_base
  143. case $ACTION in
  144. list)
  145. action_list
  146. ;;
  147. get)
  148. arg3_required "$@" || usage
  149. action_get
  150. ;;
  151. set)
  152. arg4_required "$@" || usage
  153. action_set
  154. ;;
  155. *)
  156. echoerr "unknown action <$ACTION> !" && usage && exit 2
  157. ;;
  158. esac