|
| 1 | +--- |
| 2 | +page_title: Data sources |
| 3 | +description: >- |
| 4 | + Data sources allow Terraform to reference external data. Learn how the |
| 5 | + framework can help you implement data sources. |
| 6 | +--- |
| 7 | + |
| 8 | +# Data sources |
| 9 | + |
| 10 | +[Data sources](/terraform/language/data-sources) are an abstraction that allow Terraform to reference external data. Unlike [managed resources](/terraform/language/resources), Terraform does not manage the lifecycle of the resource or data. Data sources are intended to have no side-effects. |
| 11 | + |
| 12 | +This page describes the basic implementation details required for supporting a data source within the provider. Further documentation is available for deeper data source concepts: |
| 13 | + |
| 14 | +- [Configure](/terraform/plugin/framework/data-sources/configure) data sources with provider-level data types or clients. |
| 15 | +- [Validate](/terraform/plugin/framework/data-sources/validate-configuration) practitioner configuration against acceptable values. |
| 16 | +- [Timeouts](/terraform/plugin/framework/data-sources/timeouts) in practitioner configuration for use in a data source read function. |
| 17 | + |
| 18 | +## Define Data Source Type |
| 19 | + |
| 20 | +Implement the [`datasource.DataSource` interface](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource). Each of the methods is described in more detail below. |
| 21 | + |
| 22 | +In this example, a data source named `examplecloud_thing` with hardcoded behavior is defined: |
| 23 | + |
| 24 | +```go |
| 25 | +// Ensure the implementation satisfies the desired interfaces. |
| 26 | +var _ datasource.DataSource = &ThingDataSource{} |
| 27 | + |
| 28 | +type ThingDataSource struct {} |
| 29 | + |
| 30 | +type ThingDataSourceModel struct { |
| 31 | + ExampleAttribute types.String `tfsdk:"example_attribute"` |
| 32 | + ID types.String `tfsdk:"id"` |
| 33 | +} |
| 34 | + |
| 35 | +func (d *ThingDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 36 | + resp.TypeName = "examplecloud_thing" |
| 37 | +} |
| 38 | + |
| 39 | +func (d *ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 40 | + resp.Schema = schema.Schema{ |
| 41 | + Attributes: map[string]schema.Attribute{ |
| 42 | + "example_attribute": schema.StringAttribute{ |
| 43 | + Required: true, |
| 44 | + }, |
| 45 | + "id": schema.StringAttribute{ |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (d *ThingDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 53 | + var data ThingDataSourceModel |
| 54 | + |
| 55 | + // Read Terraform configuration data into the model |
| 56 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 57 | + |
| 58 | + // Typically data sources will make external calls, however this example |
| 59 | + // hardcodes setting the id attribute to a specific value for brevity. |
| 60 | + data.ID = types.StringValue("example-id") |
| 61 | + |
| 62 | + // Save data into Terraform state |
| 63 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Metadata Method |
| 68 | + |
| 69 | +The [`datasource.DataSource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.Metadata) defines the data source name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the data source specific name. For example, a provider named `examplecloud` and a data source that reads "thing" resources would be named `examplecloud_thing`. Ensure the [Add Data Source To Provider](#add-data-source-to-provider) documentation is followed so the data source becomes part of the provider implementation, and therefore available to practitioners. |
| 70 | + |
| 71 | +In this example, the data source name in an `examplecloud` provider that reads "thing" resources is hardcoded to `examplecloud_thing`: |
| 72 | + |
| 73 | +```go |
| 74 | +// With the datasource.DataSource implementation |
| 75 | +func (d *ThingDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 76 | + resp.TypeName = "examplecloud_thing" |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +To simplify data source implementations, the [`provider.MetadataResponse.TypeName` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#MetadataResponse.TypeName) from the [`provider.Provider` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#Provider.Metadata) can set the provider name so it is available in the [`datasource.MetadataRequest.ProviderTypeName` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#MetadataRequest.ProviderTypeName). |
| 81 | + |
| 82 | +In this example, the provider defines the `examplecloud` name for itself, and the data source is named `examplecloud_thing`: |
| 83 | + |
| 84 | +```go |
| 85 | +// With the provider.Provider implementation |
| 86 | +func (p *ExampleCloudProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { |
| 87 | + resp.TypeName = "examplecloud" |
| 88 | +} |
| 89 | + |
| 90 | +// With the datasource.DataSource implementation |
| 91 | +func (d *ThingDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 92 | + resp.TypeName = req.ProviderTypeName + "_thing" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Schema Method |
| 97 | + |
| 98 | +The [`datasource.DataSource` interface `Schema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.Schema) defines a [schema](/terraform/plugin/framework/schemas) describing what data is available in the data source's configuration and state. |
| 99 | + |
| 100 | +### Read Method |
| 101 | + |
| 102 | +The [`datasource.DataSource` interface `Read` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.Read) defines how the data source updates Terraform's state to reflect the retrieved data. There is no plan or prior state to work with in `Read` requests, only configuration. |
| 103 | + |
| 104 | +During the [`terraform apply`](/terraform/cli/commands/apply), [`terraform plan`](/terraform/cli/commands/plan), and [`terraform refresh`](/terraform/cli/commands/refresh) commands, Terraform calls the provider [`ReadDataSource`](/terraform/plugin/framework/internals/rpcs#readdatasource-rpc) RPC, in which the framework calls the [`datasource.DataSource` interface `Read` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.Read). |
| 105 | + |
| 106 | +Implement the `Read` method by: |
| 107 | + |
| 108 | +1. [Accessing configuration data](/terraform/plugin/framework/accessing-values) from the [`datasource.ReadRequest.Config` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#ReadRequest.Config). |
| 109 | +1. Retriving any additional data, such as remote system information. |
| 110 | +1. [Writing state data](/terraform/plugin/framework/writing-state) into the [`datasource.ReadResponse.State` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#ReadResponse.State). |
| 111 | + |
| 112 | +If the logic needs to return [warning or error diagnostics](/terraform/plugin/framework/diagnostics), they can added into the [`datasource.ReadResponse.Diagnostics` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#ReadResponse.Diagnostics). |
| 113 | + |
| 114 | +## Add Data Source to Provider |
| 115 | + |
| 116 | +Data sources become available to practitioners when they are included in the [provider](/terraform/plugin/framework/providers) implementation via the [`provider.ProviderWithDataSources` interface `DataSources` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#ProviderWithDataSources.DataSources). |
| 117 | + |
| 118 | +In this example, the `ThingDataSource` type, which implements the `datasource.DataSource` interface, is added to the provider implementation: |
| 119 | + |
| 120 | +```go |
| 121 | +// With the provider.Provider implementation |
| 122 | +func (p *ExampleCloudProvider) DataSources(_ context.Context) []func() datasource.DataSource { |
| 123 | + return []func() datasource.DataSource{ |
| 124 | + func() datasource.DataSource { |
| 125 | + return &ThingDataSource{}, |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +To simplify provider implementations, a named function can be created with the data source implementation. |
| 132 | + |
| 133 | +In this example, the `ThingDataSource` code includes an additional `NewThingDataSource` function, which simplifies the provider implementation: |
| 134 | + |
| 135 | +```go |
| 136 | +// With the provider.Provider implementation |
| 137 | +func (p *ExampleCloudProvider) DataSources(_ context.Context) []func() datasource.DataSource { |
| 138 | + return []func() datasource.DataSource{ |
| 139 | + NewThingDataSource, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// With the datasource.DataSource implementation |
| 144 | +func NewThingDataSource() datasource.DataSource { |
| 145 | + return &ThingDataSource{} |
| 146 | +} |
| 147 | +``` |
0 commit comments