Implement resource update
In this tutorial, you will add update capabilities to the order
resource of a provider that interacts with the API of a fictional coffee-shop application called Hashicups. To do this, you will:
- Verify your schema and model.
Verify thelast_updated
attribute is in theorder
resource schema and model. The provider will update this attribute to the current date time whenever the order resource is updated. - Implement resource update.
This update method uses the HashiCups client library to invoke aPUT
request to the/orders/{orderId}
endpoint with the updated order items in the request body. After the update is successful, it updates the resource's state. - Enhance plan output with plan modifier.
This clarifies the plan output of theid
attribute to remove its difference by keeping the existing state value on updates. - Verify update functionality.
This ensures the resource is working as expected.
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 05-create-read-order
directory
from the example repository.
If you're stuck at any point during this tutorial, refer to the update-order
branch to see the changes implemented in this tutorial.
Verify schema and model
Verify your Schema
method includes an attribute named last_updated
.
Verify that your orderResourceModel
type includes a field to handle the last_updated
attribute.
Implement update functionality
The provider uses the Update
method to update an existing resource based on the schema data.
The update method follows these steps:
- Retrieves values from the plan. The method will attempt to retrieve values from the plan and convert it to an
orderResourceModel
. The model includes the order'sid
attribute, which specifies which order to update. - Generates an API request body from the plan values. The method loops through each plan item and maps it to a
hashicups.OrderItem
. This is what the API client needs to update an existing order. - Updates the order. The method invokes the API client's
UpdateOrder
method with the order's ID and OrderItems. - Maps the response body to resource schema attributes. After the method updates the order, it maps the
hashicups.Order
response to[]OrderItem
so the provider can update the Terraform state. - Sets the LastUpdated attribute. The method sets the Order's LastUpdated attribute to the current system time.
- Sets Terraform's state with the updated order.
Open the internal/provider/order_resource.go
file.
Replace your Order resource's Update
method in order_resource.go
with the following.
Build and install the updated provider.
Verify update functionality
Navigate to the examples/order
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Replace your hashicups_order.edu
resource in examples/order/main.tf
. This configuration changes the drinks and quantities in the order.
Plan the configuration.
Note that the id
attribute is showing a plan difference where the value is
going from the known value to an unknown value ((known after apply)
).
During updates, this attribute value is not expected to change. To prevent
practitioner confusion, in the next section you will update the id
attribute's
definition to keep the existing state value on update.
Enhance plan output
Terraform Plugin Framework attributes which are not configurable and that should
not show updates from the existing state value should implement the
UseStateForUnknown()
plan modifier.
Open the internal/provider/order_resource.go
file.
Replace the id
attribute definition in the Schema
method with the following.
Replace the import
statement with the following.
In your terminal, navigate to the terraform-provider-hashicups
directory.
Build and install the updated provider.
Verify update functionality
Navigate back to the examples/order
directory.
Run a Terraform apply to update your order. The plan will not mark the id
attribute as (known after apply)
any longer. Your provider will update your order and set a new value for the last_updated
attribute.
Verify that the provider updated your order by invoking the HashiCups API.
This is the same API call that your Terraform provider made to update your order's state.
Navigate back to the terraform-provider-hashicups
directory.
Next steps
Congratulations! You have enhanced the order
resource with update
capabilities.
If you were stuck during this tutorial, checkout the
06-update-order
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.