provider-resources은(는) 무엇을 하나요?
Plugin Framework를 사용하여 Terraform Provider 리소스 및 데이터 소스를 구현합니다. Provider 리소스의 CRUD 작업, 스키마 설계, 상태 관리 및 승인 테스트 개발 시 사용합니다.
개요
이 문서는 Terraform Provider를 Plugin Framework를 기반으로 개발할 때 필요한 리소스와 데이터 소스 구현 방법을 설명합니다. CRUD 작업, 스키마 설계, 상태 동기화, 그리고 승인 테스트 작성에 대한 가이드를 제공합니다.
주요 개념
- 리소스(Resource): Terraform이 관리하는 인프라 객체를 나타냅니다. Create, Read, Update, Delete 작업을 정의합니다.
- 데이터 소스(Data Source): 읽기 전용 작업으로, 외부 시스템의 데이터를 조회하여 Terraform 구성에서 사용할 수 있게 합니다.
- 스키마(Schema): 리소스나 데이터 소스의 속성(attribute)을 정의합니다. 타입, 필수 여부, 유효성 검증 등을 지정합니다.
- 상태 관리(State Management): Terraform 상태 파일에 리소스의 현재 상태를 저장하고 동기화합니다.
- 승인 테스트(Acceptance Testing): 실제 Provider와 통신하여 리소스 생성, 수정, 삭제 등이 정상 동작하는지 검증합니다.
구현 단계
1. 리소스 구현
package resources
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
)
var _ resource.Resource = &ExampleResource{}
type ExampleResource struct{}
func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_example"
}
func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
},
"description": schema.StringAttribute{
Optional: true,
},
},
}
}
func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// CRUD 구현
}
func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// 상태 읽기
}
func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// 업데이트
}
func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// 삭제
}
2. 데이터 소스 구현
package datasources
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)
var _ datasource.DataSource = &ExampleDataSource{}
type ExampleDataSource struct{}
func (d *ExampleDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_example"
}
func (d *ExampleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Computed: true,
},
},
}
}
func (d *ExampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// 데이터 조회
}
승인 테스트 작성
package provider
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestAccExampleResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccExampleResourceConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("example_example.test", "name"),
),
},
},
})
}