@@ -12,17 +12,21 @@ import (
1212 "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1313 "github.com/hashicorp/terraform-plugin-framework/diag"
1414 "github.com/hashicorp/terraform-plugin-framework/types"
15+ "github.com/miekg/dns"
1516)
1617
1718var (
18- _ datasource.DataSource = (* dnsARecordSetDataSource )(nil )
19+ _ datasource.DataSource = (* dnsARecordSetDataSource )(nil )
20+ _ datasource.DataSourceWithConfigure = (* dnsARecordSetDataSource )(nil )
1921)
2022
2123func NewDnsARecordSetDataSource () datasource.DataSource {
2224 return & dnsARecordSetDataSource {}
2325}
2426
25- type dnsARecordSetDataSource struct {}
27+ type dnsARecordSetDataSource struct {
28+ client * DNSClient
29+ }
2630
2731func (d * dnsARecordSetDataSource ) Metadata (ctx context.Context , req datasource.MetadataRequest , resp * datasource.MetadataResponse ) {
2832 resp .TypeName = req .ProviderTypeName + "_a_record_set"
@@ -36,6 +40,10 @@ func (d *dnsARecordSetDataSource) Schema(ctx context.Context, req datasource.Sch
3640 Required : true ,
3741 Description : "Host to look up." ,
3842 },
43+ "use_update_server" : schema.BoolAttribute {
44+ Optional : true ,
45+ Description : "Whether to use the configured update DNS server" ,
46+ },
3947 "addrs" : schema.ListAttribute {
4048 ElementType : types .StringType ,
4149 Computed : true ,
@@ -49,6 +57,24 @@ func (d *dnsARecordSetDataSource) Schema(ctx context.Context, req datasource.Sch
4957 }
5058}
5159
60+ func (d * dnsARecordSetDataSource ) Configure (ctx context.Context , req datasource.ConfigureRequest , resp * datasource.ConfigureResponse ) {
61+ if req .ProviderData == nil {
62+ return
63+ }
64+
65+ client , ok := req .ProviderData .(* DNSClient )
66+ if ! ok {
67+ resp .Diagnostics .AddError (
68+ "Unexpected Resource Configure Type" ,
69+ fmt .Sprintf ("Expected *DNSClient, got: %T. Please report this issue to the provider developers." , req .ProviderData ),
70+ )
71+
72+ return
73+ }
74+
75+ d .client = client
76+ }
77+
5278func (d * dnsARecordSetDataSource ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) {
5379 var config aRecordSetConfig
5480
@@ -57,16 +83,42 @@ func (d *dnsARecordSetDataSource) Read(ctx context.Context, req datasource.ReadR
5783 return
5884 }
5985
60- host := config .Host .ValueString ()
61- a , _ , err := lookupIP (host )
62- if err != nil {
63- resp .Diagnostics .AddError (fmt .Sprintf ("error looking up A records for %q: " , host ), err .Error ())
86+ if config .UseUpdateServer .ValueBool () && d .client == nil {
87+ resp .Diagnostics .AddError ("use_update_server enabled, but no update server configured" , "If you set use_update_server to true, an update server needs to be configured for the provider" )
6488 return
6589 }
66- sort .Strings (a )
90+
91+ host := config .Host .ValueString ()
92+
93+ answers := []string {}
94+ if ! config .UseUpdateServer .ValueBool () || d .client == nil {
95+ var err error
96+ answers , _ , err = lookupIP (host )
97+ if err != nil {
98+ resp .Diagnostics .AddError (fmt .Sprintf ("error looking up A records for %q: " , host ), err .Error ())
99+ return
100+ }
101+ } else {
102+ records , diags := resourceDnsRead_framework_flags (dnsConfig {Name : host }, d .client , dns .TypeA , dns.MsgHdr {RecursionDesired : true })
103+ resp .Diagnostics .Append (diags ... )
104+ if diags .HasError () {
105+ return
106+ }
107+
108+ for _ , record := range records {
109+ addr , _ , err := getAVal (record )
110+ if err != nil {
111+ resp .Diagnostics .AddError ("Error querying DNS record:" , err .Error ())
112+ return
113+ }
114+
115+ answers = append (answers , addr )
116+ }
117+ }
118+ sort .Strings (answers )
67119
68120 var convertDiags diag.Diagnostics
69- config .Addrs , convertDiags = types .ListValueFrom (ctx , config .Addrs .ElementType (ctx ), a )
121+ config .Addrs , convertDiags = types .ListValueFrom (ctx , config .Addrs .ElementType (ctx ), answers )
70122 resp .Diagnostics .Append (convertDiags ... )
71123 if resp .Diagnostics .HasError () {
72124 return
@@ -77,7 +129,8 @@ func (d *dnsARecordSetDataSource) Read(ctx context.Context, req datasource.ReadR
77129}
78130
79131type aRecordSetConfig struct {
80- ID types.String `tfsdk:"id"`
81- Host types.String `tfsdk:"host"`
82- Addrs types.List `tfsdk:"addrs"`
132+ ID types.String `tfsdk:"id"`
133+ Host types.String `tfsdk:"host"`
134+ UseUpdateServer types.Bool `tfsdk:"use_update_server"`
135+ Addrs types.List `tfsdk:"addrs"`
83136}
0 commit comments