Hi @hQnVyLRx
One option you have available to you is to use the ExpectResourceAction plan check.
For example, with the following provider code:
var _ resource.Resource = (*playgroundResource)(nil)
type playgroundResource struct {
}
func NewResource() resource.Resource {
return &playgroundResource{}
}
func (e *playgroundResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_resource"
}
func (e *playgroundResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"configurable_attribute": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(), // Triggers destroy-create whenever this value is altered.
},
},
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
type playgroundResourceData struct {
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
Id types.String `tfsdk:"id"`
}
func (e *playgroundResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data playgroundResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
data.Id = types.StringValue(time.Now().String())
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (e *playgroundResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data playgroundResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (e *playgroundResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data playgroundResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (e *playgroundResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data playgroundResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
You could use the ExpectResourceAction
plan check as follows:
func TestAccExampleResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccExampleResourceConfig("one"),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("playground_resource.test", plancheck.ResourceActionCreate),
},
},
},
{
Config: testAccExampleResourceConfig("two"),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("playground_resource.test", plancheck.ResourceActionDestroyBeforeCreate),
},
},
},
},
})
}
func testAccExampleResourceConfig(configurableAttribute string) string {
return fmt.Sprintf(`
resource "playground_resource" "test" {
configurable_attribute = %[1]q
}
`, configurableAttribute)
}