Skip to content

Commit

Permalink
Merge pull request #1393 from favonia/zeros-keep-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored Sep 12, 2023
2 parents e1a7432 + 2ce6023 commit a2982e7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/1393.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
dns: keep comments when calling UpdateDNSRecord with zero values of UpdateDNSRecordParams
```
10 changes: 5 additions & 5 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type DNSRecord struct {
Proxied *bool `json:"proxied,omitempty"`
Proxiable bool `json:"proxiable,omitempty"`
Locked bool `json:"locked,omitempty"`
Comment string `json:"comment,omitempty"`
Comment string `json:"comment,omitempty"` // the server will omit the comment field when the comment is empty
Tags []string `json:"tags,omitempty"`
}

Expand All @@ -57,8 +57,8 @@ type ListDNSRecordsParams struct {
Name string `url:"name,omitempty"`
Content string `url:"content,omitempty"`
Proxied *bool `url:"proxied,omitempty"`
Comment string `url:"comment,omitempty"`
Tags []string `url:"tag,omitempty"` // potentially multiple `tag=`
Comment string `url:"comment,omitempty"` // currently, the server does not support searching for records with an empty comment
Tags []string `url:"tag,omitempty"` // potentially multiple `tag=`
TagMatch string `url:"tag-match,omitempty"`
Order string `url:"order,omitempty"`
Direction ListDirection `url:"direction,omitempty"`
Expand All @@ -77,7 +77,7 @@ type UpdateDNSRecordParams struct {
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Comment string `json:"comment"`
Comment *string `json:"comment,omitempty"` // nil will keep the current comment, while StringPtr("") will empty it
Tags []string `json:"tags"`
}

Expand Down Expand Up @@ -193,7 +193,7 @@ type CreateDNSRecordParams struct {
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"`
Comment string `json:"comment,omitempty" url:"comment,omitempty"` // to the server, there's no difference between "no comment" and "empty comment"
Tags []string `json:"tags,omitempty"`
}

Expand Down
58 changes: 57 additions & 1 deletion dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,63 @@ func TestUpdateDNSRecord_ClearComment(t *testing.T) {

_, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Comment: "",
Comment: StringPtr(""),
})
require.NoError(t, err)
}

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

input := DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
}

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
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")
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",
"comment":null,
"tags":[],
"data": {},
"meta": {
"auto_added": true,
"source": "primary"
}
}
}`)
}

dnsRecordID := "372e67954025e0ba6aaa6d586b9e0b59"

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

_, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
})
require.NoError(t, err)
}
Expand Down

0 comments on commit a2982e7

Please sign in to comment.