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.Client method(s) to use for CRUD/Read operations.
Workflow
1. Implementation
- File Creation: Create
internal/provider/resource_<name>.go or data_source_<name>.go.
- Define Model: Create a Go struct with
tfsdk tags. Use framework types (types.String, types.Bool, etc.).
- Implement Interface: Implement
resource.Resource or datasource.DataSource.
- Schema: Define the schema in the
Schema method. Use descriptions from documentation.
- Configure: In the
Configure method, retrieve the *api.Client from req.ProviderData.
- CRUD/Read: Implement
Create, Read, Update, Delete (for resources) or Read (for data sources).
- Call the appropriate
api.Client or services layer methods.
- Handle diagnostics (
resp.Diagnostics) for errors.
2. Unit Testing
- Test File: Create
internal/provider/resource_<name>_test.go or data_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() and testAccPreCheck(t).
- Test Configurations:
- Create directory
internal/provider/acc_tests/resources/<name>/ or internal/provider/acc_tests/data_sources/<name>/.
- Add
.tf files for different test scenarios (e.g., 010_create.tf, 020_update.tf).
- Test Case: Implement
TestAcc... using resource.Test from github.com/hashicorp/terraform-plugin-testing/helper/resource.
- Include
ImportState: true for resource tests.
- Verify attributes using
resource.TestCheckResourceAttr.
Example Patterns
Resource Structure
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
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
Assets
- Resource Boilerplate
- Data Source Boilerplate
- Acceptance Test Boilerplate
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: implement-terraform-provider-resource3description: Implement Terraform Resources and Data Sources in `internal/provider` with comprehensive testing. Use when this capability is needed.4---56# Implement Terraform Provider Resource78## Description910This 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.1112## Input1314The user should provide:15161. **Type**: Resource or Data Source.172. **Name**: (e.g., `lightdash_project_agent`).183. **Schema**: List of attributes (name, type, required/optional/computed).194. **API Client Method**: Which `api.Client` method(s) to use for CRUD/Read operations.2021## Workflow2223### 1. Implementation2425- **File Creation**: Create `internal/provider/resource_<name>.go` or `data_source_<name>.go`.26- **Define Model**: Create a Go struct with `tfsdk` tags. Use framework types (`types.String`, `types.Bool`, etc.).27- **Implement Interface**: Implement `resource.Resource` or `datasource.DataSource`.28- **Schema**: Define the schema in the `Schema` method. Use descriptions from documentation.29- **Configure**: In the `Configure` method, retrieve the `*api.Client` from `req.ProviderData`.30- **CRUD/Read**: Implement `Create`, `Read`, `Update`, `Delete` (for resources) or `Read` (for data sources).31 - Call the appropriate `api.Client` or `services` layer methods.32 - Handle diagnostics (`resp.Diagnostics`) for errors.3334### 2. Unit Testing3536- **Test File**: Create `internal/provider/resource_<name>_test.go` or `data_source_<name>_test.go`.37- **Focus**: Test schema validation, custom validators, or helper functions that don't require a live API.3839### 3. Acceptance Testing4041- **Setup**: Use `isIntegrationTestMode()` and `testAccPreCheck(t)`.42- **Test Configurations**:43 - Create directory `internal/provider/acc_tests/resources/<name>/` or `internal/provider/acc_tests/data_sources/<name>/`.44 - Add `.tf` files for different test scenarios (e.g., `010_create.tf`, `020_update.tf`).45- **Test Case**: Implement `TestAcc...` using `resource.Test` from `github.com/hashicorp/terraform-plugin-testing/helper/resource`.46 - Include `ImportState: true` for resource tests.47 - Verify attributes using `resource.TestCheckResourceAttr`.4849## Example Patterns5051### Resource Structure5253```go54package provider5556import (57 "context"58 "github.com/hashicorp/terraform-plugin-framework/resource"59 "github.com/hashicorp/terraform-plugin-framework/types"60 "github.com/ubie-oss/terraform-provider-lightdash/internal/lightdash/api"61)6263var _ resource.Resource = &exampleResource{}6465type exampleResource struct {66 client *api.Client67}6869type exampleResourceModel struct {70 ID types.String `tfsdk:"id"`71 Name types.String `tfsdk:"name"`72}7374func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {75 // ... Schema definition ...76}7778func (r *exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {79 // ... Create logic ...80}81```8283### Acceptance Test8485```go86func TestAccExampleResource(t *testing.T) {87 if !isIntegrationTestMode() {88 t.Skip("Skipping acceptance test")89 }9091 resource.Test(t, resource.TestCase{92 PreCheck: func() { testAccPreCheck(t) },93 ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,94 Steps: []resource.TestStep{95 {96 Config: providerConfig + readAccTestResource("resources/example/010_create.tf"),97 Check: resource.ComposeTestCheckFunc(98 resource.TestCheckResourceAttr("lightdash_example.test", "name", "value"),99 ),100 },101 },102 })103}104```105106## Reference107108- [Terraform Plugin Framework Documentation](https://developer.hashicorp.com/terraform/plugin/framework)109- [Provider Implementation Rules](.cursor/rules/provider-implementation.mdc)110- [Project Structure Rules](.cursor/rules/project-structure.mdc)111- [Testing Setup Reference](references/testing_setup.md)112113## Assets114115- [Resource Boilerplate](assets/resource_boilerplate.go)116- [Data Source Boilerplate](assets/data_source_boilerplate.go)117- [Acceptance Test Boilerplate](assets/test_boilerplate.go)118119---120> Converted and distributed by [TomeVault](https://tomevault.io/claim/ubie-oss) — claim your Tome and manage your conversions.121<!-- tomevault:4.0:skill_md:2026-04-11 -->