Implement Terraform Provider Resource
Description
This skill implements a new Terraform Resource or Data Source in the internal/provider directory using the hashicorp/terraform-plugin-framework. It includes comprehensive implementation steps, unit tests, and acceptance tests following the project's established patterns.
Input
The user should provide:
- •Type: Resource or Data Source.
- •Name: (e.g.,
lightdash_project_agent). - •Schema: List of attributes (name, type, required/optional/computed).
- •API Client Method: Which
api.Clientmethod(s) to use for CRUD/Read operations.
Workflow
1. Implementation
- •File Creation: Create
internal/provider/resource_<name>.goordata_source_<name>.go. - •Define Model: Create a Go struct with
tfsdktags. Use framework types (types.String,types.Bool, etc.). - •Implement Interface: Implement
resource.Resourceordatasource.DataSource. - •Schema: Define the schema in the
Schemamethod. Use descriptions from documentation. - •Configure: In the
Configuremethod, retrieve the*api.Clientfromreq.ProviderData. - •CRUD/Read: Implement
Create,Read,Update,Delete(for resources) orRead(for data sources).- •Call the appropriate
api.Clientorserviceslayer methods. - •Handle diagnostics (
resp.Diagnostics) for errors.
- •Call the appropriate
2. Unit Testing
- •Test File: Create
internal/provider/resource_<name>_test.goordata_source_<name>_test.go. - •Focus: Test schema validation, custom validators, or helper functions that don't require a live API.
3. Acceptance Testing
- •Setup: Use
isIntegrationTestMode()andtestAccPreCheck(t). - •Test Configurations:
- •Create directory
internal/provider/acc_tests/resources/<name>/orinternal/provider/acc_tests/data_sources/<name>/. - •Add
.tffiles for different test scenarios (e.g.,010_create.tf,020_update.tf).
- •Create directory
- •Test Case: Implement
TestAcc...usingresource.Testfromgithub.com/hashicorp/terraform-plugin-testing/helper/resource.- •Include
ImportState: truefor resource tests. - •Verify attributes using
resource.TestCheckResourceAttr.
- •Include
Example Patterns
Resource Structure
go
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/ubie-oss/terraform-provider-lightdash/internal/lightdash/api"
)
var _ resource.Resource = &exampleResource{}
type exampleResource struct {
client *api.Client
}
type exampleResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
// ... Schema definition ...
}
func (r *exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// ... Create logic ...
}
Acceptance Test
go
func TestAccExampleResource(t *testing.T) {
if !isIntegrationTestMode() {
t.Skip("Skipping acceptance test")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + readAccTestResource("resources/example/010_create.tf"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lightdash_example.test", "name", "value"),
),
},
},
})
}
Reference
- •Terraform Plugin Framework Documentation
- •Provider Implementation Rules
- •Project Structure Rules
- •Testing Setup Reference