cf-ddns.sh icon indicating copy to clipboard operation
cf-ddns.sh copied to clipboard

Doesn't add record

Open bighippo999 opened this issue 7 years ago • 1 comments

Hi, great script thanks for writing it. I need to automatically add dns records for new servers, but I was getting update failed error when running. Looking through the code and Cloudflares API guide it looks like the script can only update an existing record (it uses PUT), I added an if check to see if it's adding/updating then use either POST or PUT depending on the action.

I genuinely dont understand GIT to submit my changes back :) so I'll just put it below:

# Perform record update
api_dns_update=`${curl_command} -s -X PUT "${cf_api_url}/zones/${zone_id}/dns_records/${record_id}" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`

Changed to:

# Perform record update
 if [ -z $record_id ]; then
     api_dns_update=`${curl_command} -s -X POST "${cf_api_url}/zones/${zone_id}/dns_records" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`
 else
     api_dns_update=`${curl_command} -s -X PUT "${cf_api_url}/zones/${zone_id}/dns_records/${record_id}" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`
 fi

Thanks again for writing it, I hope this helps someone.

bighippo999 avatar Jul 09 '17 12:07 bighippo999

This script is great, but on Mac OSX updating a record was failing with the error below, even after I manually created the record on the cloudflare site: Record update failed. {"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}

Updating this line has fixed the error and it's now working! Thanks @bighippo999 I've submitted a pull request to add this.

shimco avatar Aug 04 '18 12:08 shimco