Implement resource import
In this tutorial, you will add import 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 import.
This import method takes the given order ID from theterraform import
command and enables Terraform to begin managing the existing order. - Verify import functionality.
This ensures that resource import functionality 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 07-delete-order
directory
from the example repository.
If you're stuck at any point during this tutorial, refer to the import-order
branch to see the changes implemented in this tutorial.
Implement import functionality
The provider uses the ImportState
method to import an existing resource.
The import method implements a single step:
- Retrieves import identifier and saves to attribute state. The method will use the
resource.ImportStatePassthroughID()
function to retrieve the ID value from theterraform import
command and save it to theid
attribute.
If there are no errors, Terraform will automatically call the resource's Read
method to import the rest of the Terraform state. Since the id
attribute is all that is necessary for the Read
method to work, no additional implementation is required.
Open the internal/provider/order_resource.go
file.
Add a new ImportState
method to implement importing the resource in
order_resource.go
with the following.
internal/provider/order_resource.go
func (r *orderResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { // Retrieve import ID and save to id attribute resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)}
Add the required libraries by replacing the import
statement at the beginning of the file with the following.
internal/provider/order_resource.go
import ( "context" "fmt" "strconv" "time" "github.com/hashicorp-demoapp/hashicups-client-go" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types")
Replace the var
statement with the following to ensure that your resource implements the ResourceWithImportState
interface from the Framework.
internal/provider/order_resource.go
// Ensure the implementation satisfies the expected interfaces.var ( _ resource.Resource = &orderResource{} _ resource.ResourceWithConfigure = &orderResource{} _ resource.ResourceWithImportState = &orderResource{})
Build and install the updated provider.
$ go install .
Verify import functionality
Navigate to the examples/order
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
$ cd examples/order
Apply this configuration to ensure that the HashiCups API contains an order.
$ terraform apply -auto-approve##...Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: edu_order = { "id" = "2" "items" = tolist([ {##...
Retrieve the order ID from the Terraform state. You will use this order ID to import the order in the next step.
$ terraform show# hashicups_order.edu:resource "hashicups_order" "edu" { id = "2" items = [ # (2 unchanged elements hidden) ] last_updated = "Wednesday, 14-Dec-22 11:18:20 CST"}##...
Remove the existing order from Terraform's state. The order will still exist in the HashiCups API.
$ terraform state rm hashicups_order.eduRemoved hashicups_order.eduSuccessfully removed 1 resource instance(s).
Verify that the Terraform state no longer contains the order resource. The
previous edu_order
output value will still remain.
$ terraform show Outputs: edu_order = { id = "2" items = [##...
Verify that the HashiCups API still has your order. If needed, replace 2
with
the order ID from the output of the terraform show
command.
$ curl -X GET -H "Authorization: ${HASHICUPS_TOKEN}" localhost:19090/orders/2
Import the existing HashiCups API order into Terraform. Replace the order ID with your order ID.
$ terraform import hashicups_order.edu 2hashicups_order.edu: Importing from ID "2"...hashicups_order.edu: Import prepared! Prepared hashicups_order for importhashicups_order.edu: Refreshing state... [id=2] Import successful! The resources that were imported are shown above. These resources are now inyour Terraform state and will henceforth be managed by Terraform.
Verify that the Terraform state contains the order again.
$ terraform show# hashicups_order.edu:resource "hashicups_order" "edu" { id = "2" items = [ # (2 unchanged elements hidden) ]} ##...
Navigate to the terraform-provider-hashicups
directory.
$ cd ../..
Next steps
Congratulations! You have enhanced the order
resource with import
capabilities.
If you were stuck during this tutorial, checkout the
08-import-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.