diff --git a/dnsapi/dns_sotoon.sh b/dnsapi/dns_sotoon.sh index 8cddba02..e50c6332 100644 --- a/dnsapi/dns_sotoon.sh +++ b/dnsapi/dns_sotoon.sh @@ -63,7 +63,7 @@ dns_sotoon_add() { # First, GET the current domain zone to check for existing TXT records # This is needed for wildcard certs which require multiple TXT values _info_sotoon "Checking for existing TXT records" - if ! _sotoon_rest GET "$_domain"; then + if ! _sotoon_rest GET "$_domain_id"; then _err_sotoon "Failed to get domain zone" return 1 fi @@ -103,7 +103,7 @@ dns_sotoon_add() { # Use PATCH to update/add the record to the domain zone _info_sotoon "Updating domain zone $_domain with TXT record" - if _sotoon_rest PATCH "$_domain" "$_dns_record"; then + if _sotoon_rest PATCH "$_domain_id" "$_dns_record"; then if _contains "$response" "$txtvalue" || _contains "$response" "\"$_sub_domain\""; then _info_sotoon "Added, OK" return 0 @@ -144,7 +144,7 @@ dns_sotoon_rm() { _info_sotoon "Removing TXT record" # First, GET the current domain zone to check for existing TXT records - if ! _sotoon_rest GET "$_domain"; then + if ! _sotoon_rest GET "$_domain_id"; then _err_sotoon "Failed to get domain zone" return 1 fi @@ -178,7 +178,7 @@ dns_sotoon_rm() { _debug_sotoon "Remove record payload: $_dns_record" # Use PATCH to remove the record from the domain zone - if _sotoon_rest PATCH "$_domain" "$_dns_record"; then + if _sotoon_rest PATCH "$_domain_id" "$_dns_record"; then _info_sotoon "Record removed, OK" return 0 else @@ -219,23 +219,27 @@ _get_root() { _debug2_sotoon "API Response: $response" # Check if the response contains our domain - # Sotoon API uses Kubernetes CRD format with spec.origin or metadata.name - if _contains "$response" "\"origin\":\"$h\"" || _contains "$response" "\"name\":\"$h\""; then - _debug_sotoon "Found domain: $h" + if _contains "$response" "\"origin\":\"$h\""; then + _debug_sotoon "Found domain with origin: $h" # In Kubernetes CRD format, the metadata.name IS the resource identifier - # Extract metadata.name which serves as the domain ID - _domain_id="$h" + _domain_id=$(echo "$response" | _egrep_o "\"origin\":\"$h\"[^}]*\"name\":\"[^\"]*\"" | _egrep_o "\"name\":\"[^\"]*\"" | cut -d'"' -f4) + + # If the above didn't work, try to find it in the metadata section more broadly + if [ -z "$_domain_id" ]; then + # Look for the item containing this origin and extract its metadata.name + _domain_id=$(echo "$response" | sed 's/},{/}\n{/g' | grep "\"origin\":\"$h\"" | _egrep_o "\"name\":\"[^\"]*\"" | head -n 1 | cut -d'"' -f4) + fi if [ "$_domain_id" ]; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p") _domain=$h _debug_sotoon "Domain ID (metadata.name): $_domain_id" _debug_sotoon "Sub domain: $_sub_domain" - _debug_sotoon "Domain: $_domain" + _debug_sotoon "Domain (origin): $_domain" return 0 fi - _err_sotoon "Found domain $h but could not extract domain ID" + _err_sotoon "Found domain $h but could not extract domain ID (metadata.name)" return 1 fi p=$i