Merge pull request #11 from beatz174-bit/codex/fix-value-conversion-errors-in-provider

Fix hostname config decoding for read-only data sources
This commit is contained in:
beatz174-bit
2026-04-23 12:46:19 +10:00
committed by GitHub
3 changed files with 47 additions and 6 deletions
+41
View File
@@ -0,0 +1,41 @@
package provider
import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func hostnameFromConfig(config tfsdk.Config) (types.String, error) {
if !config.Raw.IsKnown() {
return types.StringUnknown(), nil
}
if config.Raw.IsNull() {
return types.StringNull(), nil
}
attributes := map[string]tftypes.Value{}
if err := config.Raw.As(&attributes); err != nil {
return types.StringNull(), fmt.Errorf("decode data source config object: %w", err)
}
hostnameValue, ok := attributes["hostname"]
if !ok {
return types.StringNull(), fmt.Errorf("hostname is missing from data source config")
}
if !hostnameValue.IsKnown() {
return types.StringUnknown(), nil
}
if hostnameValue.IsNull() {
return types.StringNull(), nil
}
var hostname string
if err := hostnameValue.As(&hostname); err != nil {
return types.StringNull(), fmt.Errorf("decode hostname: %w", err)
}
return types.StringValue(hostname), nil
}
+3 -3
View File
@@ -100,9 +100,9 @@ func (d *dnsRecordsDataSource) Configure(_ context.Context, req datasource.Confi
} }
func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var hostname types.String hostname, err := hostnameFromConfig(req.Config)
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("hostname"), &hostname)...) if err != nil {
if resp.Diagnostics.HasError() { resp.Diagnostics.AddError("Unable to parse data source configuration", err.Error())
return return
} }
+3 -3
View File
@@ -73,9 +73,9 @@ func (d *domainDataSource) Configure(_ context.Context, req datasource.Configure
} }
func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var hostname types.String hostname, err := hostnameFromConfig(req.Config)
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("hostname"), &hostname)...) if err != nil {
if resp.Diagnostics.HasError() { resp.Diagnostics.AddError("Unable to parse data source configuration", err.Error())
return return
} }