Archived
Fix hostname config decoding for read-only data sources
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
|
||||||
|
"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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user