Configure provider client
In this tutorial, you will configure the HashiCups API client via Terraform provider configuration or environment variables. A configured client will then be available for any data source or resource to use. To do this, you will:
- Define the provider schema.
This prepares the provider to accept Terraform configuration for client authentication and host information. - Define the provider data model.
This models the provider schema as a Go type so the data is accessible for other Go code. - Define the provider configure method.
This reads the Terraform configuration using the data model or checks environment variables if data is missing from the configuration. It raises errors if any necessary client configuration is missing. The configured client is then created and made available for data sources and resources. - Verify configuration behaviors.
This ensures the expected provider configuration behaviors.
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 01-provider
directory
from the example repository.
Implement provider schema
The Plugin Framework uses a provider's Schema
method to define the acceptable configuration attribute names and types. The HashiCups client needs a host, username, and password to be properly configured. The Terraform Plugin Framework types
package contains schema and data model types that can work with Terraform's null, unknown, or known values.
Open the internal/provider/provider.go
file.
Replace your Schema
method with the following.
Implement provider data model
The Terraform Plugin Framework uses Go struct types with tfsdk
struct field tags to map schema definitions into Go types with the actual data. The types within the struct must align with the types in the schema.
Add your provider data model type definition to internal/provider/provider.go
with the following.
Implement client configuration functionality
The provider uses the Configure
method to read API client configuration values from the Terraform configuration or environment variables. After verifying the values should be acceptable, the API client is created and made available for data source and resource usage.
The configure method follows these steps:
- Retrieves values from the configuration. The method will attempt to retrieve values from the provider configuration and convert it to an
providerModel
struct. - Checks for unknown configuration values. The method prevents an unexpectedly misconfigured client, if Terraform configuration values are only known after another resource is applied.
- Retrieves values from environment variables. The method retrieves values from environment variables, then overrides them with any set Terraform configuration values.
- Creates API client. The method invokes the HashiCups API client's
NewClient
function. - Stores configured client for data source and resource usage. The method sets the
DataSourceData
andResourceData
fields of the response, so the client is available for usage by data source and resource implementations.
Replace your Configure
method in internal/provider/provider.go
with the following.
Replace the import
statement at the top of the provider/provider.go
file with the following.
Download the new HashiCups client dependency.
Ensure all dependencies are correctly updated.
Build and install the updated provider.
Start HashiCups locally
Your HashiCups provider requires a running instance of HashiCups.
In another terminal window, navigate to the docker_compose
directory.
Run docker-compose up
to spin up a local instance of HashiCups on port 19090
.
Leave this process running in your terminal window. The HashiCups service will print out log messages in this terminal.
In the original terminal window, verify that HashiCups is running by sending a request to its health check endpoint. The HashiCups service will respond with ok
.
Create a HashiCups user
HashiCups requires a username and password to generate a JSON web token (JWT) which is used to authenticate against protected endpoints. You will use this user to authenticate to the HashiCups provider to manage your orders.
Create a user on HashiCups named education
with the password test123
.
Set the HASHICUPS_TOKEN
environment variable to the token you retrieved from
invoking the /signup
endpoint. You will use this in later tutorials.
The terminal containing your HashiCups logs will record the sign up operation.
Now that the HashiCups app is running, you are ready to start verifying the Terraform provider configuration behaviors.
Implement temporary data source
Provider configuration only occurs if there is a valid data source or resource supported by the provider and used in a Terraform configuration. For now, create a temporary data source implementation so you can verify the provider configuration behaviors. Later tutorials will guide you through the concepts and implementation details of real data sources and resources.
Add the temporary data source by creating a file named internal/provider/coffees_data_source.go
with the following.
Open the internal/provider/provider.go
file.
Add the temporary data source to your provider by replacing the DataSources
method with the following.
Build and install the updated provider from within your
terraform-provider-hashicups
directory.
Verify provider configuration
Navigate to the examples/provider-install-verification
directory.
The main.tf
Terraform configuration file in this directory has no provider configuration values in the Terraform configuration.
Run a Terraform plan with missing provider configuration. Terraform will report errors for the missing provider configuration values.
The provider configuration method you added earlier in this tutorial loads
configuration data either from environment variables, or from the provider block
in Terraform configuration. Verify the environment variable behavior by setting
the provider-defined HASHICUPS_HOST
, HASHICUPS_USERNAME
, and
HASHICUPS_PASSWORD
environment variables when executing a Terraform plan.
Terraform will configure the HashiCups client via these environment variables.
Run a Terraform plan with environment variables.
Terraform will report that it is able to read from the
hashicups_coffees.example
data source and that the configuration does not
include any changes to your infrastructure.
The terminal containing your HashiCups logs will record the sign in operation.
Verify the Terraform configuration behavior by setting the provider
schema-defined host
, username
, and password
values in a Terraform
configuration.
Create an examples/coffees
directory and navigate to it.
Create a main.tf
Terraform configuration file in this directory that sets provider configuration values in the Terraform configuration.
Run a Terraform plan. Terraform will authenticate with your HashiCups instance
using the values from the provider block and once again report that it is able
to read from the hashicups_coffees.example
data source.
Navigate to the terraform-provider-hashicups
directory.
Remove temporary data source
Before you move on to the next tutorial, remove the code for the temporary data source.
Remove the internal/provider/coffees_data_source.go
file.
Open the internal/provider/provider.go
file.
Remove the data source from your provider's schema by replacing the DataSources
method with the following.
Build and install the updated provider.
Next steps
Congratulations! You have prepared the provider to communicate with an API client. Later tutorials will show you how to implement data source and resource functionality.
If you were stuck during this tutorial, checkout the
02-provider-configure
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.