Skip to content

Commit 3884c20

Browse files
authored
Merge pull request #7 from nrkno/fix-ptr-diff
fix #5 - handle diff on ptr
2 parents e5d5789 + 4bfcf30 commit 3884c20

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/masterzen/winrm v0.0.0-20220917170901-b07f6cb0598d
99
github.com/melbahja/goph v1.3.1
1010
golang.org/x/crypto v0.7.0
11+
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
1112
golang.org/x/vuln v0.0.0-20230325131008-9550759f8614
1213
honnef.co/go/tools v0.4.3
1314
mvdan.cc/gofumpt v0.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU
242242
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
243243
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
244244
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
245+
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
246+
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
245247
golang.org/x/exp/typeparams v0.0.0-20230321023759-10a507213a29 h1:e7LhZmJ631l59keHP9ssC3sgSn3/oiEHKHKXDkimURY=
246248
golang.org/x/exp/typeparams v0.0.0-20230321023759-10a507213a29/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
247249
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=

internal/provider/resource_win_dns_record.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

8+
"golang.org/x/exp/slices"
9+
710
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
912
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -41,9 +44,10 @@ func resourceDNSRecord() *schema.Resource {
4144
Description: "The type of the dns records.",
4245
},
4346
"records": {
44-
Type: schema.TypeSet,
45-
Required: true,
46-
Description: "A list of records.",
47+
Type: schema.TypeSet,
48+
Required: true,
49+
Description: "A list of records.",
50+
DiffSuppressFunc: suppressRecordDiff,
4751
Elem: &schema.Schema{
4852
Type: schema.TypeString,
4953
},
@@ -131,8 +135,40 @@ func resourceDNSRecordDelete(ctx context.Context, d *schema.ResourceData, meta i
131135
return nil
132136
}
133137

134-
func suppressCaseDiff(k, old, new string, d *schema.ResourceData) bool {
138+
func suppressCaseDiff(key, old, new string, d *schema.ResourceData) bool {
135139
// k is ignored here, but wee need to include it in the function's
136140
// signature in order to match the one defined for DiffSuppressFunc
137141
return strings.EqualFold(old, new)
138142
}
143+
144+
func suppressRecordDiff(key, old, new string, d *schema.ResourceData) bool {
145+
if d.Get("type") == dnshelper.RecordTypePTR {
146+
var (
147+
oldRecords []string
148+
newRecords []string
149+
)
150+
151+
// For a list, the key is path to the element, rather than the list.
152+
// E.g. "windns_record.2.records.0"
153+
lastDotIndex := strings.LastIndex(key, ".")
154+
if lastDotIndex != -1 {
155+
key = key[:lastDotIndex]
156+
}
157+
158+
oldData, newData := d.GetChange(key)
159+
if oldData == nil || newData == nil {
160+
return false
161+
}
162+
163+
for _, v := range oldData.(*schema.Set).List() {
164+
oldRecords = append(oldRecords, fmt.Sprintf("%s", v))
165+
}
166+
// Get-DNSResourceRecord always adds a `.` after the PTR record.
167+
// to avoid change if the user did not add it, we need to add it before we compare.
168+
for _, v := range newData.(*schema.Set).List() {
169+
newRecords = append(newRecords, fmt.Sprintf("%s.", v))
170+
}
171+
return slices.Equal(oldRecords, newRecords)
172+
}
173+
return strings.EqualFold(old, new)
174+
}

internal/provider/resource_win_dns_record_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ resource "windns_record" "r1" {
3131
}
3232
`
3333

34+
const testAccResourceDNSRecordConfigBasicPTRwithoutDot = `
35+
resource "windns_record" "r1" {
36+
name = "12.113"
37+
zone_name = "10.10.in-addr.arpa"
38+
type = "PTR"
39+
records = ["example-host.example.com"]
40+
}
41+
`
42+
3443
const testAccResourceDNSRecordConfigBasicA = `
3544
variable "windns_record_name" {}
3645
@@ -139,6 +148,31 @@ func TestAccResourceDNSRecord_BasicPTR(t *testing.T) {
139148
})
140149
}
141150

151+
func TestAccResourceDNSRecord_BasicPTRwithoutDot(t *testing.T) {
152+
envVars := []string{"TF_VAR_windns_record_name"}
153+
154+
resource.Test(t, resource.TestCase{
155+
PreCheck: func() { testAccPreCheck(t, envVars) },
156+
ProviderFactories: testAccProviderFactories,
157+
CheckDestroy: resource.ComposeTestCheckFunc(
158+
testAccResourceDNSRecordExists("windns_record.r1", "example-host.example.com.", dnshelper.RecordTypePTR, false),
159+
),
160+
Steps: []resource.TestStep{
161+
{
162+
Config: testAccResourceDNSRecordConfigBasicPTRwithoutDot,
163+
Check: resource.ComposeTestCheckFunc(
164+
testAccResourceDNSRecordExists("windns_record.r1", "example-host.example.com.", dnshelper.RecordTypePTR, true),
165+
),
166+
},
167+
{
168+
ResourceName: "windns_record.r1",
169+
ImportState: true,
170+
ImportStateVerify: true,
171+
},
172+
},
173+
})
174+
}
175+
142176
func TestAccResourceDNSRecord_BasicA(t *testing.T) {
143177
envVars := []string{"TF_VAR_windns_record_name"}
144178

0 commit comments

Comments
 (0)