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

dns: UpdateDNSRecord: clean up params and drop record lookup #1170

Merged
merged 5 commits into from
Jan 10, 2023
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
7 changes: 7 additions & 0 deletions .changelog/1170.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:note
dns: remove additional lookup from `Update` operations when `Name` or `Type` was omitted
```

```release-note:breaking-change
dns: remove these read-only fields from `UpdateDNSRecordParams`: `CreatedOn`, `ModifiedOn`, `Meta`, `ZoneID`, `ZoneName`, `Proxiable`, and `Locked`
```
14 changes: 6 additions & 8 deletions cmd/flarectl/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ func dnsCreateOrUpdate(c *cli.Context) error {
}
resp = &cloudflare.DNSRecordResponse{
Result: cloudflare.DNSRecord{
ID: rr.ID,
Name: rr.Name,
Type: rr.Type,
Content: rr.Content,
TTL: rr.TTL,
Proxiable: rr.Proxiable,
Proxied: &proxy,
Locked: rr.Locked,
ID: rr.ID,
Name: rr.Name,
Type: rr.Type,
Content: rr.Content,
TTL: rr.TTL,
Proxied: &proxy,
},
}
}
Expand Down
54 changes: 21 additions & 33 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -63,23 +64,16 @@ type ListDNSRecordsParams struct {
}

type UpdateDNSRecordParams struct {
CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"`
Type string `json:"type,omitempty" url:"type,omitempty"`
Name string `json:"name,omitempty" url:"name,omitempty"`
Content string `json:"content,omitempty" url:"content,omitempty"`
Meta interface{} `json:"meta,omitempty"`
Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
ZoneName string `json:"zone_name,omitempty"`
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"`
Proxiable bool `json:"proxiable,omitempty"`
Locked bool `json:"locked,omitempty"`
Comment string `json:"comment,omitempty" url:"comment,omitempty"`
Tags []string `json:"tags,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Data interface{} `json:"data,omitempty"` // data for: SRV, LOC
ID string `json:"-"`
Priority *uint16 `json:"-"` // internal use only
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Comment string `json:"comment,omitempty"`
Tags []string `json:"tags,omitempty"`
}

// DNSListResponse represents the response from the list DNS records endpoint.
Expand Down Expand Up @@ -199,6 +193,9 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, param
return records, &listResponse.ResultInfo, nil
}

// ErrMissingDNSRecordID is for when DNS record ID is needed but not given.
var ErrMissingDNSRecordID = errors.New("required DNS record ID missing")

// GetDNSRecord returns a single DNS record for the given zone & record
// identifiers.
//
Expand All @@ -207,6 +204,10 @@ func (api *API) GetDNSRecord(ctx context.Context, rc *ResourceContainer, recordI
if rc.Identifier == "" {
return DNSRecord{}, ErrMissingZoneID
}
if recordID == "" {
return DNSRecord{}, ErrMissingDNSRecordID
}

uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, recordID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -228,25 +229,12 @@ func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, para
if rc.Identifier == "" {
return ErrMissingZoneID
}
if params.ID == "" {
return ErrMissingDNSRecordID
}

params.Name = toUTS46ASCII(params.Name)

// Populate the record name from the existing one if the update didn't
// specify it.
if params.Name == "" || params.Type == "" {
favonia marked this conversation as resolved.
Show resolved Hide resolved
rec, err := api.GetDNSRecord(ctx, rc, params.ID)
if err != nil {
return err
}

if params.Name == "" {
params.Name = rec.Name
}
if params.Type == "" {
params.Type = rec.Type
}
}

uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, params.ID)
favonia marked this conversation as resolved.
Show resolved Hide resolved
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
Expand Down
223 changes: 2 additions & 221 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func TestUpdateDNSRecord(t *testing.T) {
input := DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Type: "A",
Name: "example.com",
Name: "xn--138h.example.com",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
Expand All @@ -434,6 +434,7 @@ func TestUpdateDNSRecord(t *testing.T) {
var v DNSRecord
err := json.NewDecoder(r.Body).Decode(&v)
require.NoError(t, err)
v.ID = "372e67954025e0ba6aaa6d586b9e0b59"
assert.Equal(t, input, v)

w.Header().Set("content-type", "application/json")
Expand Down Expand Up @@ -473,226 +474,6 @@ func TestUpdateDNSRecord(t *testing.T) {
err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Type: "A",
Name: "example.com",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
})
require.NoError(t, err)
}

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

proxied := false

asciiInput := DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Name: "xn--138h.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, asciiInput, v)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"type": "A",
"name": "xn--138h.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": "xn--138h.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(), ZoneIdentifier(""), UpdateDNSRecordParams{})
assert.Equal(t, ErrMissingZoneID, err)

err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Type: "A",
Name: "xn--138h.example.com",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
})
require.NoError(t, err)
}

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

proxied := false

completedASCIIInput := DNSRecord{
Name: "xn--138h.example.com",
Type: "A",
Content: "198.51.100.4",
TTL: 120,
Proxied: &proxied,
ID: "372e67954025e0ba6aaa6d586b9e0b59",
}

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, completedASCIIInput, 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(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Name: "😺.example.com",
Content: "198.51.100.4",
TTL: 120,
Expand Down