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

Add DNS settings #3670

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changelog/3670.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
dns: Add settings to DNSRecord
```
83 changes: 45 additions & 38 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ import (
// ErrMissingBINDContents is for when the BIND file contents is required but not set.
var ErrMissingBINDContents = errors.New("required BIND config contents missing")

type DNSRecordSettings struct {
FlattenCNAME bool `json:"flatten_cname,omitempty"`
}

// DNSRecord represents a DNS record in a zone.
type DNSRecord struct {
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Meta interface{} `json:"meta,omitempty"`
Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC
ID string `json:"id,omitempty"`
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Proxiable bool `json:"proxiable,omitempty"`
Comment string `json:"comment,omitempty"` // the server will omit the comment field when the comment is empty
Tags []string `json:"tags,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Meta interface{} `json:"meta,omitempty"`
Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC
ID string `json:"id,omitempty"`
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Proxiable bool `json:"proxiable,omitempty"`
Comment string `json:"comment,omitempty"` // the server will omit the comment field when the comment is empty
Tags []string `json:"tags,omitempty"`
Settings DNSRecordSettings `json:"settings,omitempty"`
}

// DNSRecordResponse represents the response from the DNS endpoint.
Expand Down Expand Up @@ -66,16 +71,17 @@ type ListDNSRecordsParams struct {
}

type UpdateDNSRecordParams struct {
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:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Comment *string `json:"comment,omitempty"` // nil will keep the current comment, while StringPtr("") will empty it
Tags []string `json:"tags"`
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:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty"`
Comment *string `json:"comment,omitempty"` // nil will keep the current comment, while StringPtr("") will empty it
Tags []string `json:"tags"`
Settings DNSRecordSettings `json:"settings,omitempty"`
}

// DNSListResponse represents the response from the list DNS records endpoint.
Expand Down Expand Up @@ -175,20 +181,21 @@ type ImportDNSRecordsParams struct {
}

type CreateDNSRecordParams 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"`
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"`
Proxiable bool `json:"proxiable,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"`
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"`
Priority *uint16 `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"`
Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"`
Proxiable bool `json:"proxiable,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"`
Settings DNSRecordSettings `json:"settings,omitempty"`
}

// CreateDNSRecord creates a DNS record for the zone identifier.
Expand Down
96 changes: 96 additions & 0 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,99 @@ func TestDeleteDNSRecord(t *testing.T) {
err = client.DeleteDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID)
require.NoError(t, err)
}

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

priority := uint16(10)
proxied := false
FlattenInput := DNSRecord{
Type: "CNAME",
Name: "example.com",
Content: "example1.com",
TTL: 120,
Priority: &priority,
Proxied: &proxied,
Settings: DNSRecordSettings{
FlattenCNAME: true,
},
}

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

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

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

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

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Type: "CNAME",
Name: "example.com",
Content: "example1.com",
Proxiable: true,
Proxied: FlattenInput.Proxied,
TTL: FlattenInput.TTL,
CreatedOn: createdOn,
ModifiedOn: modifiedOn,
Data: map[string]interface{}{},
Meta: map[string]interface{}{
"auto_added": true,
"source": "primary",
},
Settings: DNSRecordSettings{
FlattenCNAME: true,
},
}

_, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(""), CreateDNSRecordParams{})
assert.ErrorIs(t, err, ErrMissingZoneID)

actual, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), CreateDNSRecordParams{
Type: "CNAME",
Name: "example.com",
Content: "example1.com",
TTL: 120,
Priority: &priority,
Proxied: &proxied,
Settings: DNSRecordSettings{
FlattenCNAME: true,
},
})
require.NoError(t, err)

assert.Equal(t, want, actual)
}
Loading