Implement data source
In this tutorial, you will implement a data source to read the list of coffees from the HashiCups API and save it in Terraform’s state. To do this, you will:
- Define the initial data source type.
This prepares the data source to be added to the provider. - Add data source to provider.
This enables the data source for testing and Terraform configuration usage. - Implement the HashiCups client in the data source.
This retrieves the configured HashiCups client from the provider and makes it available for data source operations. - Define the data source schema.
This prepares the data source to set Terraform state with the list of coffees. - Define the data source data model.
This models the data source schema as a Go type so the data is accessible for other Go code. - Define the data source read logic.
This handles calling the HashiCups API using the configured client and setting the Terraform state with the data. - Verify data source behavior.
This ensures the expected data source behavior.
Prerequisites
To follow this tutorial, you need:
- Go 1.21+ installed and configured.
- Terraform v1.8+ installed locally.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory.
Your code should match the 02-provider-configure
directory
from the example repository.
Implement initial data source type
To add a new data source to your provider, start by creating a type that
implements the datasource.DataSource
interface.
This interface requires the following:
- Metadata method. This defines the data source type name, which is how the data source is referred to in Terraform configurations.
- Schema method. This defines the schema for any data source configuration and state data.
- Read method. This defines the logic which sets the Terraform state for the data source.
Add a new data source that will load data about coffee drinks from the HashiCups
API by creating a internal/provider/coffees_data_source.go
file with the following.
Add data source to provider
Providers use the DataSources
method to define the data sources they
implement.
Open the internal/provider/provider.go
file.
Add your new data source by replacing the DataSources
method with the
following.
Implement data source client functionality
Data sources use the optional Configure
method to fetch configured clients from the provider. The provider configures the HashiCups client and the data source can save a reference to that client for its operations.
Open the internal/provider/coffees_data_source.go
file.
Allow your data source type to store a reference to the HashiCups client by
replacing the coffeesDataSource
type with the following.
Replace the import
statement at the beginning of the file with the following.
Ensure that your data source implements the DataSourceWithConfigure
interface
by replacing the var ( ... )
statement with the following.
Fetch the HashiCups client from the provider by adding the Configure
method to
your data source type.
Implement data source schema
The data source uses the Schema
method to define the acceptable configuration and state attribute names and types. The coffees data source will need to save a list of coffees with various attributes to the state.
Replace your data source's Schema
method with the following.
Implement data source data models
Add data model types to your data source with the following.
Replace the import
statement at the beginning of the file with the following.
Implement read functionality
The data source uses the Read
method to refresh the Terraform state based on the schema data. The hashicups_coffees
data source will use the configured HashiCups client to call the HashiCups API coffee listing endpoint and save this data to the Terraform state.
The read method follows these steps:
- Reads coffees list. The method invokes the API client's
GetCoffees
method. - Maps response body to schema attributes. After the method reads the coffees, it maps the
[]hashicups.Coffee
response tocoffeesModel
so the data source can set the Terraform state. - Sets state with coffees list.
Replace your data source's Read
method with the following.
Build and install the updated provider.
Verify data source
Navigate to the examples/coffees
directory.
The main.tf
Terraform configuration file in this
directory reads data from your new hashicups_coffees
data source.
Add an output value to have Terraform print out that data.
Run a Terraform plan. Terraform will report the data it retrieved from the HashiCups API.
Navigate to the terraform-provider-hashicups
directory.
Next steps
Congratulations! You have implemented a data source in the provider that uses the configured API client to save Terraform state.
If you were stuck during this tutorial, checkout the
03-read-coffees
directory in the example repository to see the code implemented in this
tutorial.
- To learn more about the Terraform Plugin Framework, refer to the Terraform Plugin Framework documentation.
- For a full capability comparison between the SDKv2 and the Plugin Framework, refer to the Which SDK Should I Use? documentation.
- The example repository contains directories corresponding to each tutorial in this collection.
- Submit any Terraform Plugin Framework bug reports or feature requests to the development team in the Terraform Plugin Framework Github repository.
- Submit any Terraform Plugin Framework questions in the Terraform Plugin Framework Discuss forum.