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

fix: hosting.de API returns different content #64

Merged
merged 1 commit into from
Jul 18, 2024
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
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 = "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",
Expand Down