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

Handle normalized record content value #167

Merged
merged 2 commits into from
Nov 2, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/resources/zone_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The following arguments are supported:
* `id` - The record ID
* `zone_id` - The zone ID of the record
* `qualified_name` - The FQDN of the record
* `value_normalized` - The normalized value of the record

## Import

Expand Down
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