Implement Update
Note: We recommend using the Terraform Plugin Framework for new provider development because it offers significant advantages compared to the SDKv2. Refer to the Plugin Framework tutorials to learn how to create providers using the framework.
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:
- Update your schema.
Add alast_updated
attribute to theorder
schema. This attribute will update to the current date time whenever the order resource is updated. - Implement update.
This update function invokes aPUT
request to the/orders/{orderId}
endpoint with the update order items in the request body. After the update function runs, it invokesresourceOrderRead
to retrieve the order's updated values.
Prerequisites
To follow this tutorial, you need:
- a Golang 1.15+ installed and configured.
- the Terraform 0.14+ CLI installed locally.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory. Next, fetch new changes.
Then, checkout the implement-create
branch. This step is optional but recommended to ensure that your code matches the code from the previous tutorials.
Your directory should have the following structure.
If you're stuck at any point during this tutorial, refer to the implement-update
branch to see the changes implemented in this tutorial.
Update schema
In hashicups/resource_order.go
, update your order
schema to include a parameter named last_updated
.
Your order
schema should look like the following.
Implement update
Now that you have defined the order
resource schema, replace the resourceOrderUpdate
function in hashicups/resource_order.go
with the following code snippet. This function will update the order resource if there are any changes to the order items.
Notice the m
(meta) input parameter contains the HashiCups API Client set by the ConfigureContextFunc
defined above. If an unauthenticated API Client is provided (no username/password), this function will fail and return an error message.
The function determines whether there are discrepancies in the items
property between the configuration and the state.
Tip
The HasChange()
function enables you to invoke different APIs or build a targeted request body to update your resource when a specific property changes.
If there are discrepancies, the function will update the order with the new configuration. Then, it will update the last_updated
attribute to the current timestamp. Finally, it will call resourceOrderRead
to update the resource's state.
Note
Update functions rarely update the resource ID. However, if the ID becomes blank during the update process, the provider assumes the resource is destroyed and all state is removed.
Import the time library.
Test the provider
Now that you've added update capabilities to the order
resource, verify that it works.
First, confirm that you are in the terraform-provider-hashicups
root directory.
Next, build the binary and move it into your user Terraform plugins directory. This allows you to sideload and test the custom provider. Select the tab for your operating system for specific instructions.
Tip
The Perform CRUD operations with Providers tutorial explains why and how to sideload custom providers. Refer to it to learn more about where to install custom providers and how to reference them in your configuration.
Navigate to the terraform-provider-hashicups/examples
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Update your hashicups_order.edu
resource in main.tf
.
Initialize your workspace to refresh your HashiCups provider.
Apply the configuration to update your order and set the last_updated
attribute.
Verify that the provider updated your order by invoking the HashiCups API. Substitute the order number with your order ID and the auth token with your auth token.
Next steps
In this tutorial, you added update capabilities to the order
resource.
If you were stuck during this tutorial, checkout the implement-update
branch to see the changes implemented in this tutorial.
- The Terraform Provider Scaffold is a quick-start repository for creating a Terraform provider. Use this GitHub template when you're ready to create a custom provider.
- To learn more about the Terraform Plugin SDK, refer to the Terraform Plugin SDK Documentation.