diff --git a/hostingde/record_resource.go b/hostingde/record_resource.go index 304a410..43b2130 100644 --- a/hostingde/record_resource.go +++ b/hostingde/record_resource.go @@ -2,6 +2,7 @@ package hostingde import ( "context" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -21,6 +22,11 @@ var ( _ resource.ResourceWithImportState = &recordResource{} ) +func normalizeRecordContent(content string) string { + newContent := strings.ReplaceAll(content, "\" \"", ""); + return strings.ReplaceAll(newContent, "\"", ""); +} + // NewRecordResource is a helper function to simplify the provider implementation. func NewRecordResource() resource.Resource { return &recordResource{} @@ -130,9 +136,19 @@ func (r *recordResource) Create(ctx context.Context, req resource.CreateRequest, } var returnedRecord DNSRecord - for _, r := range recordResp.Response.Records { - if r.Name == record.Name && r.Type == record.Type && r.Content == record.Content && r.TTL == record.TTL { - returnedRecord = r + for _, responseRecord := range recordResp.Response.Records { + if responseRecord.Name == record.Name && responseRecord.Type == record.Type && responseRecord.TTL == record.TTL { + if responseRecord.Content == record.Content { + returnedRecord = responseRecord + break; + } + + normalizedContent := normalizeRecordContent(responseRecord.Content); + if normalizedContent == record.Content { + returnedRecord = responseRecord + returnedRecord.Content = normalizedContent + break; + } } } @@ -189,7 +205,7 @@ func (r *recordResource) Read(ctx context.Context, req resource.ReadRequest, res state.ID = types.StringValue(returnedRecord.ID) state.Name = types.StringValue(returnedRecord.Name) state.Type = types.StringValue(returnedRecord.Type) - state.Content = types.StringValue(returnedRecord.Content) + state.Content = types.StringValue(normalizeRecordContent(returnedRecord.Content)) state.TTL = types.Int64Value(int64(returnedRecord.TTL)) state.Priority = types.Int64Value(int64(returnedRecord.Priority)) @@ -238,9 +254,19 @@ func (r *recordResource) Update(ctx context.Context, req resource.UpdateRequest, } var returnedRecord DNSRecord - for _, r := range recordResp.Response.Records { - if r.Name == record.Name && r.Type == record.Type && r.Content == record.Content && r.TTL == record.TTL { - returnedRecord = r + for _, responseRecord := range recordResp.Response.Records { + if responseRecord.Name == record.Name && responseRecord.Type == record.Type && responseRecord.TTL == record.TTL { + if responseRecord.Content == record.Content { + returnedRecord = responseRecord + break; + } + + normalizedContent := normalizeRecordContent(responseRecord.Content); + if normalizedContent == record.Content { + returnedRecord = responseRecord + returnedRecord.Content = normalizedContent + break; + } } } diff --git a/hostingde/record_resource_test.go b/hostingde/record_resource_test.go index b88d651..5a248a3 100644 --- a/hostingde/record_resource_test.go +++ b/hostingde/record_resource_test.go @@ -66,6 +66,33 @@ resource "hostingde_record" "test_mx" { resource.TestCheckResourceAttrSet("hostingde_record.test_mx", "id"), ), }, + // Create and read TXT testing + { + Config: providerConfig + ` +resource "hostingde_zone" "test" { + name = "example2.test" + type = "NATIVE" + email = "hostmaster@example2.test" +} +resource "hostingde_record" "test_dkim" { + zone_id = hostingde_zone.test.id + name = "default._domainkey.example2.test" + type = "TXT" + content = "v=DKIM1;k=rsa;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyla9hW3TvoXvZQxwzaJ4SZ9ict1HU3E6+FwLWniGe6TiPtcYrjTIsiudQb8tltibOXiS+qqbxzI+quI3aGU6osy2rIv0eWo8+oOOqOD9pERftc/aqe51cXuv4kPqwvpXEBwrXFWVM+VxivEubUJ7eKkFyXJpelv0LslXv/MmYbUyed6dF+reOGZCsvnbiRv74qdxbAL/25j62E8WrnxzJwhUtx/JhdBOjsHBvuw9hy6rZsVJL9eXayWyGRV6qmsLRzsRSBs+mDrgmKk4dugADd11+A03ics3i8hplRoWDkqnNKz1qy4f5TsV6v9283IANrAzRfHwX8EvNiFsBz+ZCQIDAQAB" + ttl = 300 +} +`, + Check: resource.ComposeAggregateTestCheckFunc( + // Verify name attribute. + resource.TestCheckResourceAttr("hostingde_record.test_dkim", "name", "default._domainkey.example2.test"), + // Verify type attribute. + resource.TestCheckResourceAttr("hostingde_record.test_dkim", "type", "TXT"), + // Verify email attribute. + resource.TestCheckResourceAttr("hostingde_record.test_dkim", "content", "v=DKIM1;k=rsa;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyla9hW3TvoXvZQxwzaJ4SZ9ict1HU3E6+FwLWniGe6TiPtcYrjTIsiudQb8tltibOXiS+qqbxzI+quI3aGU6osy2rIv0eWo8+oOOqOD9pERftc/aqe51cXuv4kPqwvpXEBwrXFWVM+VxivEubUJ7eKkFyXJpelv0LslXv/MmYbUyed6dF+reOGZCsvnbiRv74qdxbAL/25j62E8WrnxzJwhUtx/JhdBOjsHBvuw9hy6rZsVJL9eXayWyGRV6qmsLRzsRSBs+mDrgmKk4dugADd11+A03ics3i8hplRoWDkqnNKz1qy4f5TsV6v9283IANrAzRfHwX8EvNiFsBz+ZCQIDAQAB"), + // Verify dynamic values have any value set in the state. + resource.TestCheckResourceAttrSet("hostingde_record.test_dkim", "id"), + ), + }, // ImportState testing { ResourceName: "hostingde_zone.test",