Skip to content

Commit

Permalink
track normalized content value to detect drift
Browse files Browse the repository at this point in the history
  • Loading branch information
DXTimer committed Nov 2, 2023
1 parent 4b441b1 commit 1b7cd52
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
32 changes: 21 additions & 11 deletions internal/framework/resources/zone_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ type ZoneRecordResource struct {

// ZoneRecordResourceModel describes the resource data model.
type ZoneRecordResourceModel struct {
ZoneName types.String `tfsdk:"zone_name"`
ZoneId types.String `tfsdk:"zone_id"`
Name types.String `tfsdk:"name"`
QualifiedName types.String `tfsdk:"qualified_name"`
Type types.String `tfsdk:"type"`
Regions types.List `tfsdk:"regions"`
Value types.String `tfsdk:"value"`
TTL types.Int64 `tfsdk:"ttl"`
Priority types.Int64 `tfsdk:"priority"`
Id types.Int64 `tfsdk:"id"`
ZoneName types.String `tfsdk:"zone_name"`
ZoneId types.String `tfsdk:"zone_id"`
Name types.String `tfsdk:"name"`
QualifiedName types.String `tfsdk:"qualified_name"`
Type types.String `tfsdk:"type"`
Regions types.List `tfsdk:"regions"`
Value types.String `tfsdk:"value"`
ValueNormalized types.String `tfsdk:"value_normalized"`
TTL types.Int64 `tfsdk:"ttl"`
Priority types.Int64 `tfsdk:"priority"`
Id types.Int64 `tfsdk:"id"`
}

func (r *ZoneRecordResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -89,6 +90,9 @@ func (r *ZoneRecordResource) Schema(_ context.Context, _ resource.SchemaRequest,
"value": schema.StringAttribute{
Required: true,
},
"value_normalized": schema.StringAttribute{
Computed: true,
},
"ttl": schema.Int64Attribute{
Optional: true,
Computed: true,
Expand Down Expand Up @@ -245,6 +249,12 @@ func (r *ZoneRecordResource) Read(ctx context.Context, req resource.ReadRequest,
record = *response.Data
}

if record.Content != data.ValueNormalized.ValueString() {
// If the record content has changed, we need to update the record in the remote
tflog.Debug(ctx, "DNSimple Zone Record content changed")
data.Value = types.StringValue(record.Content)
}

r.updateModelFromAPIResponse(&record, data)

// Save updated data into Terraform state
Expand Down Expand Up @@ -353,7 +363,7 @@ func (r *ZoneRecordResource) updateModelFromAPIResponse(record *dnsimple.ZoneRec
data.ZoneId = types.StringValue(record.ZoneID)
data.Name = types.StringValue(record.Name)
data.Type = types.StringValue(record.Type)
data.Value = types.StringValue(record.Content)
data.ValueNormalized = types.StringValue(record.Content)
data.TTL = types.Int64Value(int64(record.TTL))
data.Priority = types.Int64Value(int64(record.Priority))

Expand Down
59 changes: 59 additions & 0 deletions internal/framework/resources/zone_record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,54 @@ func TestAccZoneRecordResourceWithPriority(t *testing.T) {
})
}

func TestAccZoneRecordResourceWithTXT(t *testing.T) {
domainName := os.Getenv("DNSIMPLE_DOMAIN")
resourceName := "dnsimple_zone_record.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { test_utils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckZoneRecordResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccZoneRecordResourceTXTConfig(domainName, "test value for TXT record"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "zone_name", domainName),
resource.TestCheckResourceAttr(resourceName, "qualified_name", "terraform."+domainName),
resource.TestCheckResourceAttr(resourceName, "value", "test value for TXT record"),
resource.TestCheckResourceAttr(resourceName, "ttl", "3600"),
resource.TestCheckResourceAttrSet(resourceName, "id"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func TestAccZoneRecordResourceWithNormalizedTXT(t *testing.T) {
domainName := os.Getenv("DNSIMPLE_DOMAIN")
resourceName := "dnsimple_zone_record.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { test_utils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckZoneRecordResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccZoneRecordResourceTXTConfig(domainName, "\"test value for TXT record\""),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "zone_name", domainName),
resource.TestCheckResourceAttr(resourceName, "qualified_name", "terraform."+domainName),
resource.TestCheckResourceAttr(resourceName, "value", "\"test value for TXT record\""),
resource.TestCheckResourceAttr(resourceName, "ttl", "3600"),
resource.TestCheckResourceAttrSet(resourceName, "id"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func TestAccZoneRecordResourceWithPrefetch(t *testing.T) {
domainName := os.Getenv("DNSIMPLE_DOMAIN")
resourceName := "dnsimple_zone_record.test"
Expand Down Expand Up @@ -241,6 +289,17 @@ func testAccCheckZoneRecordExists(n string, record *dnsimple.ZoneRecord) resourc
}
}

func testAccZoneRecordResourceTXTConfig(domainName string, value string) string {
return fmt.Sprintf(`
resource "dnsimple_zone_record" "test" {
zone_name = %[1]q
name = "terraform"
value = %[2]q
type = "TXT"
}`, domainName, value)
}

func testAccZoneRecordResourcePriorityConfig(domainName string) string {
return fmt.Sprintf(`
resource "dnsimple_zone_record" "test" {
Expand Down

0 comments on commit 1b7cd52

Please sign in to comment.