Fix hostname config decoding in read-only data sources

This commit is contained in:
beatz174-bit
2026-04-23 12:21:50 +10:00
parent 4cd0267433
commit 22ff9f01a5
2 changed files with 10 additions and 14 deletions
+5 -7
View File
@@ -100,20 +100,18 @@ 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 config struct { var hostname types.String
Hostname types.String `tfsdk:"hostname"` resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("hostname"), &hostname)...)
}
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
if config.Hostname.IsUnknown() || config.Hostname.IsNull() { if hostname.IsUnknown() || hostname.IsNull() {
resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.") resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.")
return return
} }
domainID, domainName, err := d.clientProvider.client.GetRootDomain(ctx, config.Hostname.ValueString()) domainID, domainName, err := d.clientProvider.client.GetRootDomain(ctx, hostname.ValueString())
if err != nil { if err != nil {
resp.Diagnostics.AddError(diagnosticSummary("Unable to resolve Dynu domain from hostname", err), err.Error()) resp.Diagnostics.AddError(diagnosticSummary("Unable to resolve Dynu domain from hostname", err), err.Error())
return return
@@ -128,7 +126,7 @@ func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequ
sortDNSRecords(records) sortDNSRecords(records)
state := dnsRecordsDataSourceModel{ state := dnsRecordsDataSourceModel{
Hostname: config.Hostname, Hostname: hostname,
DomainID: types.Int64Value(domainID), DomainID: types.Int64Value(domainID),
DomainName: types.StringValue(domainName), DomainName: types.StringValue(domainName),
Records: make([]dnsRecordStateItem, 0, len(records)), Records: make([]dnsRecordStateItem, 0, len(records)),
+5 -7
View File
@@ -73,20 +73,18 @@ 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 config struct { var hostname types.String
Hostname types.String `tfsdk:"hostname"` resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("hostname"), &hostname)...)
}
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
} }
if config.Hostname.IsUnknown() || config.Hostname.IsNull() { if hostname.IsUnknown() || hostname.IsNull() {
resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.") resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.")
return return
} }
domainID, _, err := d.clientProvider.client.GetRootDomain(ctx, config.Hostname.ValueString()) domainID, _, err := d.clientProvider.client.GetRootDomain(ctx, hostname.ValueString())
if err != nil { if err != nil {
resp.Diagnostics.AddError(diagnosticSummary("Unable to resolve Dynu domain from hostname", err), err.Error()) resp.Diagnostics.AddError(diagnosticSummary("Unable to resolve Dynu domain from hostname", err), err.Error())
return return
@@ -104,7 +102,7 @@ func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return return
} }
state := domainDataSourceModel{ state := domainDataSourceModel{
Hostname: config.Hostname, Hostname: hostname,
Domain: domainObject, Domain: domainObject,
} }