Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems updating DNS record #97

Closed
mwarkentin opened this issue Dec 20, 2016 · 8 comments
Closed

Problems updating DNS record #97

mwarkentin opened this issue Dec 20, 2016 · 8 comments
Assignees

Comments

@mwarkentin
Copy link

mwarkentin commented Dec 20, 2016

Hey there, I'm looking into hashicorp/terraform#7690, and trying to determine if the issue is in the terraform code or in the Cloudflare library.

I'm having issues updating a record using cloudflare-go directly (I can create it ok). Wondering if someone can point out if I'm doing something incorrectly, or if this is actually a bug in the library.

I'm pretty new to golang, so not surprised if I'm doing something incorrectly here.

cloudflare.go

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/cloudflare/cloudflare-go"
)

func main() {
	// Construct a new API object
	api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
	if err != nil {
		log.Fatal(err)
	}

	domain := os.Getenv("CF_DOMAIN")

	// Fetch the zone ID
	zoneId, err := api.ZoneIDByName(domain)
	if err != nil {
		log.Fatal(err)
	}

	newRecord := cloudflare.DNSRecord{
		Type:     "A",
		Name:     "CFAPI",
		Content:  "127.0.0.1",
		Proxied:  false,
		ZoneName: domain,
		ZoneID:   zoneId,
		TTL:      3600,
	}

	log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord)

	r, err := api.CreateDNSRecord(zoneId, newRecord)
	if err != nil {
		fmt.Errorf("Failed to create record: %s", err)
	}

	recordId := r.Result.ID

	// In the Event that the API returns an empty DNS Record, we verify that the
	// ID returned is not the default ""
	if recordId == "" {
		fmt.Errorf("Failed to find record in Create response; Record was empty")
	}

	log.Printf("[INFO] CloudFlare Record ID: %s", recordId)

	updatedRecord := cloudflare.DNSRecord{
		ID:       recordId,
		Type:     "A",
		Name:     "CFAPI-modified",
		Content:  "192.168.10.10",
		Proxied:  true,
		ZoneName: domain,
		ZoneID:   zoneId,
		TTL:      120,
	}

	log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updatedRecord)

	err = api.UpdateDNSRecord(zoneId, recordId, updatedRecord)
	if err != nil {
		fmt.Errorf("Failed to update record: %s", err)
	}
}

Output

CF_API_KEY=*** CF_API_EMAIL=*** CF_DOMAIN=wave-juno.com go run cloudflare.go
2016/12/20 15:30:28 [DEBUG] CloudFlare Record create configuration: cloudflare.DNSRecord{ID:"", Type:"A", Name:"CFAPI", Content:"127.0.0.1", Proxiable:false, Proxied:false, TTL:3600, Locked:false, ZoneID:"5058354498babe04ef58c4168949b099", ZoneName:"wave-juno.com", CreatedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, ModifiedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, Data:interface {}(nil), Meta:interface {}(nil), Priority:0}
2016/12/20 15:30:29 [INFO] CloudFlare Record ID: 2dc525087c2ff8b02289eef8a0ec1d68
2016/12/20 15:30:29 [DEBUG] CloudFlare Record update configuration: cloudflare.DNSRecord{ID:"2dc525087c2ff8b02289eef8a0ec1d68", Type:"A", Name:"CFAPI-modified", Content:"192.168.10.10", Proxiable:false, Proxied:true, TTL:120, Locked:false, ZoneID:"5058354498babe04ef58c4168949b099", ZoneName:"wave-juno.com", CreatedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, ModifiedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, Data:interface {}(nil), Meta:interface {}(nil), Priority:0}

Cloudflare Web Console

Expected result

  • Name=cfapi-modified
  • Content=192.168.10.10
  • TTL=120s
@mwarkentin
Copy link
Author

I was able to update the record when hitting the API directly:

$ curl -X PUT "https://api.cloudflare.com/client/v4/zones/5058354498babe04ef58c4168949b099/dns_records/2dc525087c2ff8b02289eef8a0ec1d68" \
>      -H "X-Auth-Email: ***" \
>      -H "X-Auth-Key: ***" \
>      -H "Content-Type: application/json" \
>      --data '{"type":"A","name":"CFAPI-modified","content":"192.168.10.10","ttl":120,"proxied":false}'
{"result":{"id":"2dc525087c2ff8b02289eef8a0ec1d68","type":"A","name":"cfapi-modified.wave-juno.com","content":"192.168.10.10","proxiable":false,"proxied":false,"ttl":120,"locked":false,"zone_id":"5058354498babe04ef58c4168949b099","zone_name":"wave-juno.com","modified_on":"2016-12-20T20:45:04.595545Z","created_on":"2016-12-20T20:45:04.595545Z","meta":{"auto_added":false}},"success":true,"errors":[],"messages":[]}

@jamesog
Copy link
Contributor

jamesog commented Dec 20, 2016

Ah, there is indeed a very stupid bug here. I think when it was originally written an assumption was made that anything but the label name would be changed, so UpdateDNSRecord is actually fetching the existing record (based on the recordID) and overwriting the name :-(

I'm not sure why the TTL isn't being updated, though. Maybe an artefact of the entire record not being updated?

@jamesog jamesog added the bug label Dec 20, 2016
@jamesog jamesog self-assigned this Dec 20, 2016
@jamesog
Copy link
Contributor

jamesog commented Dec 20, 2016

Hopefully the PR fixes this for you. Are you able to test it before merging?

@mwarkentin
Copy link
Author

mwarkentin commented Dec 20, 2016

@jamesog I'd be happy to test, but not sure how.. I tried checking out origin/jamesog/97-updatednsrecord in $GOPATH/src/github/cloudflare/cloudflare-go and then re-ran my script, however I'm seeing the same result (just a record named cfapi).

Not sure if this is an issue with how I did things or if there's still an issue with the library. Maybe you could add some debug output so I'm sure that I'm actually running your code? 😄

Output

2016/12/20 17:04:26 [DEBUG] CloudFlare Record create configuration: cloudflare.DNSRecord{ID:"", Type:"A", Name:"CFAPI", Content:"127.0.0.1", Proxiable:false, Proxied:false, TTL:3600, Locked:false, ZoneID:"5058354498babe04ef58c4168949b099", ZoneName:"wave-juno.com", CreatedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, ModifiedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, Data:interface {}(nil), Meta:interface {}(nil), Priority:0}
2016/12/20 17:04:27 [INFO] CloudFlare Record ID: be0df701e59406ea97c8dc818813b8b5
2016/12/20 17:04:27 [DEBUG] CloudFlare Record update configuration: cloudflare.DNSRecord{ID:"be0df701e59406ea97c8dc818813b8b5", Type:"A", Name:"CFAPI-modified", Content:"192.168.10.10", Proxiable:false, Proxied:true, TTL:120, Locked:false, ZoneID:"5058354498babe04ef58c4168949b099", ZoneName:"wave-juno.com", CreatedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, ModifiedOn:time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)}, Data:interface {}(nil), Meta:interface {}(nil), Priority:0}

Web Console

@mwarkentin
Copy link
Author

@jamesog I added some extra debugging logs, and noticed I was trying to update the record to proxied = true which wasn't valid and causing the update to fail. After I resolved this, I was able to successfully update the name:

@jamesog
Copy link
Contributor

jamesog commented Dec 21, 2016

Cool, thanks!

@mwarkentin
Copy link
Author

@jamesog Awesome, thanks for the quick response on this! 👍

@jamesog
Copy link
Contributor

jamesog commented Dec 21, 2016

Welcome :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants