Implement delete
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 delete capabilities to the order
resource for a Terraform provider that interacts with the API of a fictional coffee-shop application called Hashicups. To do this, you will implement delete. This delete function invokes a DELETE
request to the /orders/{orderId}
endpoint.
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-update
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-delete
branch to see the changes implemented in this tutorial.
Implement delete
Replace the resourceOrderDelete
function in hashicups/resource_order.go
with the code snippet below. This function will delete the HashiCups order and Terraform resource.
Format your code.
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 retrieves the order ID using d.ID()
and deletes the order.
If the
destroy
callback returns without an error, the provider assumes the resource is destroyed and all state is removed.If the
destroy
callback returns with an error, the provider assumes the resource still exists and all prior state is preserved.
The destroy
callback should never update any state on the resource. In addition, the destroy
callback should always handle the case where the resource might already be destroyed. If the resource is already destroyed, the destroy function should not return an error. If the target API doesn't have this functionality, the destroy function should verify the resource exists; if the resource does not exist, set the resource ID to "". This behavior allows Terraform users to manually delete resources without breaking Terraform.
Test the provider
Now that you've added delete 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.
Initialize your workspace to refresh your HashiCups provider then destroy the configuration. This should delete your HashiCups order.
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 delete capabilities to the order
resource.
If you were stuck during this tutorial, checkout the implement-delete
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.
- To learn how to publish a Terraform provider to the Terraform Registry, refer to the Publishing Providers to the Terraform Registry documentation.