Data sources may require provider-level data or remote system clients to operate correctly. The framework supports the ability to configure this data and/or clients once within the provider, then pass that information to data sources by adding the Configure method.
Implement the provider.ConfigureResponse.DataSourceData field in the Provider interface Configure method. This value can be set to any type, whether an existing client or vendor SDK type, a provider-defined custom type, or the provider implementation itself. It is recommended to use pointer types so that data sources can determine if this value was configured before attempting to use it.
In this example, the Go standard library net/http.Client is configured in the provider, and made available for data sources:
// With the provider.Provider implementationfunc(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){
resp.DataSourceData =&http.Client{/* ... */}}
// With the provider.Provider implementationfunc(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){ resp.DataSourceData =&http.Client{/* ... */}}
In this example, the code defines an ExampleClient type that is made available for data sources:
type ExampleClient struct{/* ... */}// With the provider.Provider implementationfunc(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){
resp.DataSourceData =&ExampleClient{/* ... */}}
type ExampleClient struct{/* ... */}// With the provider.Provider implementationfunc(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){ resp.DataSourceData =&ExampleClient{/* ... */}}
In this example, the ExampleCloudProvider type itself is made available for data sources:
// With the provider.Provider implementationtype ExampleCloudProvider struct{/* ... */}func(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){
resp.DataSourceData = p
}
// With the provider.Provider implementationtype ExampleCloudProvider struct{/* ... */}func(p *ExampleCloudProvider)Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse){ resp.DataSourceData = p
}
In this example, the provider configured the Go standard library net/http.Client which the data source uses during Read:
// With the datasource.DataSource implementationtype ThingDataSource struct{
client *http.Client
}func(d *ThingDataSource)Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse){// Prevent panic if the provider has not been configured.if req.ProviderData ==nil{return}
client, ok := req.ProviderData.(*http.Client)if!ok {
resp.Diagnostics.AddError("Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),)return}
d.client = client
}func(d *ThingDataSource)Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse){// Prevent panic if the provider has not been configured.if d.client ==nil{
resp.Diagnostics.AddError("Unconfigured HTTP Client","Expected configured HTTP client. Please report this issue to the provider developers.",)return}
httpResp, err := d.client.Get("https://example.com")/* ... */}
// With the datasource.DataSource implementationtype ThingDataSource struct{ client *http.Client
}func(d *ThingDataSource)Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse){// Prevent panic if the provider has not been configured.if req.ProviderData ==nil{return} client, ok := req.ProviderData.(*http.Client)if!ok { resp.Diagnostics.AddError("Unexpected Data Source Configure Type", fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),)return} d.client = client
}func(d *ThingDataSource)Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse){// Prevent panic if the provider has not been configured.if d.client ==nil{ resp.Diagnostics.AddError("Unconfigured HTTP Client","Expected configured HTTP client. Please report this issue to the provider developers.",)return} httpResp, err := d.client.Get("https://example.com")/* ... */}