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, 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.13+ installed and configured.
- the Terraform 0.14+ CLI installed locally. The Terraform binaries are located here.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory. Then, checkout the implement-update
branch. This step is optional but recommended to insure that you've accurately completed the previous steps.
$ git checkout implement-update
Your directory should have the following structure.
$ tree -L 2
.
├── Makefile
├── README.md
├── docker_compose
│ ├── conf.json
│ └── docker-compose.yml
├── examples
│ ├── coffee
│ ├── main.tf
│ ├── terraform.tfstate
│ └── terraform.tfstate.backup
├── go.mod
├── go.sum
├── hashicups
│ ├── data_source_coffee.go
│ ├── data_source_order.go
│ ├── provider.go
│ └── resource_order.go
├── main.go
└── vendor
If you’re stuck, refer to the implement-delete
branch to see the changes implemented in this tutorial.
»Implement delete
Replace the resourceOrderDelete
function in resource_order.go
with the code snippet below. This function will delete the HashiCups order and Terraform resource.
func resourceOrderDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*hc.Client)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
orderID := d.Id()
err := c.DeleteOrder(orderID)
if err != nil {
return diag.FromErr(err)
}
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
d.SetId("")
return diags
}
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, navigate to the terraform-provider-hashicups
root directory.
Then, build the binary and move it into your user Terraform plugins directory. This allows you to sideload and test your custom providers.
$ make install
go build -o terraform-provider-hashicups
mv terraform-provider-hashicups ~/.terraform.d/plugins/hashicorp.com/edu/hashicups/0.2/darwin_amd64
Navigate to the examples
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
$ cd examples
Initialize your workspace to refresh your HashiCups provider then destroy the configuration. This should delete your HashiCups order.
$ terraform init && terraform destroy --auto-approve
Destroy complete! Resources: 1 destroyed.
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.
$ curl -X GET -H "Authorization: ${HASHICUPS_TOKEN}" localhost:19090/orders/1
»Next steps
Congratulations! You have 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.
Over the course of these tutorials, you re-created the HashiCups provider and learned how to create data sources, authenticate the provider to the HashiCups client, and create resources with CRUD functionality.
The final HashiCups provider can be found on the master branch. This is similar to the implement-delete
branch with an additional ingredients
data resource and automated testing.
To learn how to publish a Terraform provider to the Terraform Registry, refer to the Publishing Providers to the Terraform Registry documentation.
A full list of official, verified and community Terraform providers can be found on the Terraform Provider Registry. In addition, the terraform-providers GitHub organization contains repositories for popular Terraform providers. We encourage you to find provider you're interested in and start contributing!