Archived
Merge pull request #12 from beatz174-bit/codex/fix-value-conversion-errors-in-terraform-provider
Fix datasource hostname decoding for computed state attributes
This commit is contained in:
@@ -1,41 +1,20 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"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
|
||||
func hostnameFromConfig(ctx context.Context, config tfsdk.Config) (types.String, error) {
|
||||
var hostname types.String
|
||||
diags := config.GetAttribute(ctx, path.Root("hostname"), &hostname)
|
||||
if diags.HasError() {
|
||||
return types.StringNull(), fmt.Errorf("decode hostname from data source config: %s", diags.Errors()[0].Summary())
|
||||
}
|
||||
|
||||
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
|
||||
return hostname, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
|
||||
"github.com/hashicorp/terraform-plugin-go/tftypes"
|
||||
)
|
||||
|
||||
func TestHostnameFromConfigIgnoresComputedAttributesDomain(t *testing.T) {
|
||||
ds := NewDomainDataSource().(*domainDataSource)
|
||||
var schemaResp datasource.SchemaResponse
|
||||
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
|
||||
|
||||
configType := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
|
||||
"hostname": tftypes.String,
|
||||
"domain": tftypes.DynamicPseudoType,
|
||||
}}
|
||||
configValue := tftypes.NewValue(configType, map[string]tftypes.Value{
|
||||
"hostname": tftypes.NewValue(tftypes.String, "www.example.com"),
|
||||
"domain": tftypes.NewValue(tftypes.DynamicPseudoType, tftypes.UnknownValue),
|
||||
})
|
||||
|
||||
hostname, err := hostnameFromConfig(context.Background(), tfsdk.Config{Schema: schemaResp.Schema, Raw: configValue})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if hostname.ValueString() != "www.example.com" {
|
||||
t.Fatalf("unexpected hostname: %q", hostname.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostnameFromConfigIgnoresComputedAttributesDNSRecords(t *testing.T) {
|
||||
ds := NewDNSRecordsDataSource().(*dnsRecordsDataSource)
|
||||
var schemaResp datasource.SchemaResponse
|
||||
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
|
||||
|
||||
configType := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
|
||||
"hostname": tftypes.String,
|
||||
"domain_id": tftypes.Number,
|
||||
"domain_name": tftypes.String,
|
||||
"records": tftypes.DynamicPseudoType,
|
||||
}}
|
||||
configValue := tftypes.NewValue(configType, map[string]tftypes.Value{
|
||||
"hostname": tftypes.NewValue(tftypes.String, "www.example.com"),
|
||||
"domain_id": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
|
||||
"domain_name": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
|
||||
"records": tftypes.NewValue(tftypes.DynamicPseudoType, tftypes.UnknownValue),
|
||||
})
|
||||
|
||||
hostname, err := hostnameFromConfig(context.Background(), tfsdk.Config{Schema: schemaResp.Schema, Raw: configValue})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if hostname.ValueString() != "www.example.com" {
|
||||
t.Fatalf("unexpected hostname: %q", hostname.ValueString())
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (d *dnsRecordsDataSource) Configure(_ context.Context, req datasource.Confi
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
hostname, err := hostnameFromConfig(req.Config)
|
||||
hostname, err := hostnameFromConfig(ctx, req.Config)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Unable to parse data source configuration", err.Error())
|
||||
return
|
||||
|
||||
@@ -73,7 +73,7 @@ func (d *domainDataSource) Configure(_ context.Context, req datasource.Configure
|
||||
}
|
||||
|
||||
func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
hostname, err := hostnameFromConfig(req.Config)
|
||||
hostname, err := hostnameFromConfig(ctx, req.Config)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Unable to parse data source configuration", err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user