Skip to content

Commit

Permalink
fix: hosting.de API returns different content
Browse files Browse the repository at this point in the history
When sending data for a long TXT record (like a DKIM record) to the
hosting.de API, the record contents get changed. This caused the
terraform state to always be incorrect.

Example:

When sending the following record contents for a TXT record to be
created or updated:

```
v=DKIM1;k=rsa;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyla9hW3TvoXvZQxwzaJ4SZ9ict1HU3E6+FwLWniGe6TiPtcYrjTIsiudQb8tltibOXiS+qqbxzI+quI3aGU6osy2rIv0eWo8+oOOqOD9pERftc/aqe51cXuv4kPqwvpXEBwrXFWVM+VxivEubUJ7eKkFyXJpelv0LslXv/MmYbUyed6dF+reOGZCsvnbiRv74qdxbAL/25j62E8WrnxzJwhUtx/JhdBOjsHBvuw9hy6rZsVJL9eXayWyGRV6qmsLRzsRSBs+mDrgmKk4dugADd11+A03ics3i8hplRoWDkqnNKz1qy4f5TsV6v9283IANrAzRfHwX8EvNiFsBz+ZCQIDAQAB
```

The API afterwards returned this strings as the contents:

```
\"v=DKIM1;k=rsa;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyla9hW3TvoXvZQxwzaJ4SZ9ict1HU3E6+FwLWniGe6TiPtcYrjTIsiudQb8tltibOXiS+qqbxzI+quI3aGU6osy2rIv0eWo8+oOOqOD9pERftc/aqe51cXuv4kPqwvpXEBwrXFWVM+VxivEubUJ7eKkFyXJpelv0LslXv/MmYbUyed6dF+reOGZCsvnbiRv74qd\" \"xbAL/25j62E8WrnxzJwhUtx/JhdBOjsHBvuw9hy6rZsVJL9eXayWyGRV6qmsLRzsRSBs+mDrgmKk4dugADd11+A03ics3i8hplRoWDkqnNKz1qy4f5TsV6v9283IANrAzRfHwX8EvNiFsBz+ZCQIDAQAB\"
```

Notice the added `/"` and `/" /"` parts. This commit removes this extra
string from the terraform resource so terraform thinks they're exactly
the same. It's a relatively crude fix, but it appears to work.
  • Loading branch information
b12f committed Jul 17, 2024
1 parent 0ee3bf0 commit d203996
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
40 changes: 33 additions & 7 deletions hostingde/record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hostingde

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand All @@ -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{}
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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;
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions hostingde/record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
}
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",
Expand Down

0 comments on commit d203996

Please sign in to comment.