terraform-plugin-framework icon indicating copy to clipboard operation
terraform-plugin-framework copied to clipboard

Support `Default` in Data Source Schema

Open zliang-akamai opened this issue 2 years ago • 5 comments

Similar to the default support in the resource, https://developer.hashicorp.com/terraform/plugin/framework/resources/default

This is useful because some data sources may have more than one (id) arguments in the config.

zliang-akamai avatar Jun 07 '23 00:06 zliang-akamai

Hi @zliang-akamai 👋 Thank you for raising this feature request.

To provide some additional context here, which I do not believe was copied from our internal design documentation into #668:

Data sources and providers are considered out of scope because Terraform does not have the notion of planning for those concepts. While technically possible to implement similar default value handlers on those concepts, the framework would then need to implement its own details of how to surface that data, which goes against its design principle of surfacing abstractions concisely. For example, it would be incorrect to overwrite null configuration data with default values because provider developers could no longer determine the source of the configuration data. It would also be confusing for provider developers if the merged configuration and default values data was surfaced in a separate data field, which is not present in the protocol. They would need to choose when to use the separate data field, which is error prone.

Because of these considerations, there is some initial hesitation to introducing the abstraction. If some of the design details can be worked out without going against prior design decisions in the framework, we can certainly entertain proposals.

bflad avatar Jun 09 '23 21:06 bflad

+1 it would be nice to get this feature for data sources similar to resources: https://github.com/hashicorp/terraform-plugin-framework/blob/main/resource/schema/string_attribute.go#L154

ATM I am writing a data source that can search for a resource a number of ways. The AWS API only has a list function and also includes a status parameter which... in every conceivable circumstance will be ACTIVE. However, the API allows for searching for an INACTIVE status also so I'd like to include it with a sane default

drewmullen avatar Nov 07 '23 19:11 drewmullen

@drewmullen you can default the value in the data source Read method and check if the attribute value in Config is not null to overwrite that value.

Until there's a reasonable discussion about https://github.com/hashicorp/terraform-plugin-framework/issues/751#issuecomment-1585145090, I wouldn't anticipate this feature request to move forward. It's not that we don't think exposing this sort of functionality is not valuable, its that exposing it is awkward against the design of the framework.

bflad avatar Nov 07 '23 19:11 bflad

Hi @bflad

From #668, it says:

The framework would handle the semantics of only consulting the default value handling when the configuration is null.

Isn't this the same case for data source when its O+C attribute is nil in the configuration, then the default should be applied and accessable from the req.Config?

Apologize if I missed anything, as I didn't quite follow your quotation:

For example, it would be incorrect to overwrite null configuration data with default values because provider developers could no longer determine the source of the configuration data.

What is the case when users want to determine an attribute in the req.Config comes from default or configuration? Or it has special meaning to data source?

magodo avatar Nov 23 '23 08:11 magodo