Implement resource delete
In this tutorial, you will add delete 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:
- Implement resource delete.
This delete method uses the HashiCups API client to invoke aDELETE
request to the/orders/{orderId}
endpoint. After the delete is successful, the framework automatically removes the resource from Terraform's state. - Verify delete functionality.
This ensures that 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 06-update-order
directory
from the example repository.
If you're stuck at any point during this tutorial, refer to the delete-order
branch to see the changes implemented in this tutorial.
Implement delete functionality
The provider uses the Delete
method to delete an existing resource.
The delete method follows these steps:
- Retrieves values from the state. The method will attempt to retrieve values from the state and convert it to an
Order
struct (defined inmodels.go
). - Deletes an existing order. The method invokes the API client's
DeleteOrder
method.
If there are no errors, the framework will automatically remove the resource from Terraform's state.
Open the internal/provider/order_resource.go
file.
Replace your Delete
method with the following.
Build and install the updated provider.
Verify delete functionality
Navigate to the examples/order
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Destroy the configuration. This will delete your order.
Verify that the provider deleted your order by invoking the HashiCups API. Substitute the order number with your order ID and the auth token with your auth token.
Navigate to the terraform-provider-hashicups
directory.
Next steps
Congratulations! You have enhanced the order
resource with delete
capabilities.
If you were stuck during this tutorial, checkout the
07-delete-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.