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

Don't fetch unneeded DNS records in UpdateDNSRecord. #673

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,20 @@ func (api *API) DNSRecord(ctx context.Context, zoneID, recordID string) (DNSReco
//
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
func (api *API) UpdateDNSRecord(ctx context.Context, zoneID, recordID string, rr DNSRecord) error {
rec, err := api.DNSRecord(ctx, zoneID, recordID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, this looks like a hangover from a80236a where we swapped from HTTP PUT => PATCH but still wanted the option for end users to only supply a single attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Is there something else I should change in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at this stage. I'm in the process of reviewing and checking this as it's pretty critical.

if err != nil {
return err
}
// Populate the record name from the existing one if the update didn't
// specify it.
if rr.Name == "" {
rr.Name = rec.Name
}
if rr.Type == "" {
rr.Type = rec.Type
if rr.Name == "" || rr.Type == "" {
rec, err := api.DNSRecord(ctx, zoneID, recordID)
if err != nil {
return err
}

if rr.Name == "" {
rr.Name = rec.Name
}
if rr.Type == "" {
rr.Type = rec.Type
}
}
uri := fmt.Sprintf("/zones/%s/dns_records/%s", zoneID, recordID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, rr)
Expand Down
172 changes: 171 additions & 1 deletion dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestUpdateDNSRecord(t *testing.T) {
Proxied: &proxied,
}

handleUpdateDNSRecord := func(w http.ResponseWriter, r *http.Request) {
handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)

var v DNSRecord
Expand Down Expand Up @@ -289,6 +289,176 @@ func TestUpdateDNSRecord(t *testing.T) {
}`)
}

dnsRecordID := "372e67954025e0ba6aaa6d586b9e0b59"

mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler)

err := client.UpdateDNSRecord(context.Background(), testZoneID, dnsRecordID, input)
require.NoError(t, err)
}

func TestUpdateDNSRecordWithoutName(t *testing.T) {
setup()
defer teardown()

proxied := false
input := DNSRecord{
Type: "A",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
}

completedInput := DNSRecord{
Name: "example.com",
Type: "A",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
}

handleUpdateDNSRecord := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)

var v DNSRecord
err := json.NewDecoder(r.Body).Decode(&v)
require.NoError(t, err)
assert.Equal(t, completedInput, v)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"type": "A",
"name": "example.com",
"content": "198.51.100.4",
"proxiable": true,
"proxied": false,
"ttl": 120,
"locked": false,
"zone_id": "d56084adb405e0b7e32c52321bf07be6",
"zone_name": "example.com",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"data": {},
"meta": {
"auto_added": true,
"source": "primary"
}
}
}`)
}

handleDNSRecord := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"type": "A",
"name": "example.com",
"content": "198.51.100.4",
"proxiable": true,
"proxied": false,
"ttl": 120,
"locked": false,
"zone_id": "d56084adb405e0b7e32c52321bf07be6",
"zone_name": "example.com",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"data": {},
"meta": {
"auto_added": true,
"source": "primary"
}
}
}`)
}

handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
handleDNSRecord(w, r)
return
}

if r.Method == http.MethodPatch {
handleUpdateDNSRecord(w, r)
return
}

assert.Failf(t, "Expected method 'GET' or `PATCH`, got %s", r.Method)
}

dnsRecordID := "372e67954025e0ba6aaa6d586b9e0b59"

mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler)

err := client.UpdateDNSRecord(context.Background(), testZoneID, dnsRecordID, input)
require.NoError(t, err)
}

func TestUpdateDNSRecordWithoutType(t *testing.T) {
setup()
defer teardown()

proxied := false
input := DNSRecord{
Name: "example.com",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
}

completedInput := DNSRecord{
Name: "example.com",
Type: "A",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
}

handleUpdateDNSRecord := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)

var v DNSRecord
err := json.NewDecoder(r.Body).Decode(&v)
require.NoError(t, err)
assert.Equal(t, completedInput, v)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"type": "A",
"name": "example.com",
"content": "198.51.100.4",
"proxiable": true,
"proxied": false,
"ttl": 120,
"locked": false,
"zone_id": "d56084adb405e0b7e32c52321bf07be6",
"zone_name": "example.com",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"data": {},
"meta": {
"auto_added": true,
"source": "primary"
}
}
}`)
}

handleDNSRecord := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)

Expand Down