Skip to content

Commit

Permalink
fix(dns): clean up UpdateDNSRecordParams
Browse files Browse the repository at this point in the history
Also avoid record lookup for name and type; they are not needed
for the patch method. PS: This commit does not mean all the
remaining fields should be there---I did not touch the ones
I do not understand.
  • Loading branch information
favonia committed Jan 8, 2023
1 parent 63d7aa8 commit 92dfa18
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 254 deletions.
43 changes: 10 additions & 33 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,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 @@ -231,22 +224,6 @@ func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, para

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 == "" {
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)
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

0 comments on commit 92dfa18

Please sign in to comment.