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.
Add the required libraries by replacing the import
statement at the beginning of the file with the following.
Replace the var
statement with the following to ensure that your resource implements the ResourceWithImportState
interface from the Framework.
Build and install the updated provider.
Verify import functionality
Navigate to the examples/order
directory. This contains a sample Terraform configuration for the Terraform HashiCups provider.
Apply this configuration to ensure that the HashiCups API contains an order.
Retrieve the order ID from the Terraform state. You will use this order ID to import the order in the next step.
Remove the existing order from Terraform's state. The order will still exist in the HashiCups API.
Verify that the Terraform state no longer contains the order resource. The
previous edu_order
output value will still remain.
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.
Import the existing HashiCups API order into Terraform. Replace the order ID with your order ID.
Verify that the Terraform state contains the order again.
Navigate to the terraform-provider-hashicups
directory.
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.