From f3d2d461ce1ec5145bfc090e7a6ca44ec4f72e2c Mon Sep 17 00:00:00 2001 From: Cyb3r-Jak3 Date: Sun, 24 Nov 2024 15:41:03 -0500 Subject: [PATCH] Add DNS settings --- .changelog/3670.txt | 3 ++ dns.go | 83 +++++++++++++++++++++------------------ dns_test.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 38 deletions(-) create mode 100644 .changelog/3670.txt diff --git a/.changelog/3670.txt b/.changelog/3670.txt new file mode 100644 index 00000000000..f964b5db0a2 --- /dev/null +++ b/.changelog/3670.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +dns: Add settings to DNSRecord +``` diff --git a/dns.go b/dns.go index 0c36efd429a..93e8b250558 100644 --- a/dns.go +++ b/dns.go @@ -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. @@ -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. @@ -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. diff --git a/dns_test.go b/dns_test.go index 01af5c1b702..11f24a7111d 100644 --- a/dns_test.go +++ b/dns_test.go @@ -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) +}