Implement Terraform Resources and Data Sources in internal/provider with comprehensive testing.
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.
The user should provide:
lightdash_project_agent).api.Client method(s) to use for CRUD/Read operations.internal/provider/resource_<name>.go or data_source_<name>.go.tfsdk tags. Use framework types (types.String, types.Bool, etc.).resource.Resource or datasource.DataSource.Schema method. Use descriptions from documentation.Configure method, retrieve the *api.Client from req.ProviderData.Create, Read, Update, Delete (for resources) or Read (for data sources).api.Client or services layer methods.resp.Diagnostics) for errors.internal/provider/resource_<name>_test.go or data_source_<name>_test.go.isIntegrationTestMode() and testAccPreCheck(t).internal/provider/acc_tests/resources/<name>/ or internal/provider/acc_tests/data_sources/<name>/..tf files for different test scenarios (e.g., 010_create.tf, 020_update.tf).TestAcc... using resource.Test from github.com/hashicorp/terraform-plugin-testing/helper/resource.ImportState: true for resource tests.resource.TestCheckResourceAttr.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 ...
}
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"),
),
},
},
})
}