configure providers during destroy plan

Now that we can fully evaluate a provider configuration, make sure we
configure the provider before using it during a destroy plan.
This commit is contained in:
James Bardin 2022-06-01 16:03:27 -04:00
parent 93ff27227a
commit 38d70a1c3d
2 changed files with 30 additions and 6 deletions

View File

@ -1042,11 +1042,38 @@ output "out" {
state, diags := ctx.Apply(plan, m)
assertNoErrors(t, diags)
// TODO: extend this to ensure the otherProvider is always properly
// configured during the destroy plan
otherProvider.ConfigureProviderCalled = false
otherProvider.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
// check that our config is complete, even during a destroy plan
expected := cty.ObjectVal(map[string]cty.Value{
"local": cty.ListVal([]cty.Value{cty.StringVal("first-ok"), cty.StringVal("second-ok")}),
"output": cty.ListVal([]cty.Value{cty.StringVal("first-ok"), cty.StringVal("second-ok")}),
"var": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("first"),
"b": cty.StringVal("second"),
}),
})
if !req.Config.RawEquals(expected) {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf(
`incorrect provider config:
expected: %#v
got: %#v`,
expected, req.Config))
}
return resp
}
opts.Mode = plans.DestroyMode
// skip refresh so that we don't configure the provider before the destroy plan
opts.SkipRefresh = true
// destroy only a single instance not included in the moved statements
_, diags = ctx.Plan(m, state, opts)
assertNoErrors(t, diags)
if !otherProvider.ConfigureProviderCalled {
t.Fatal("failed to configure provider during destroy plan")
}
}

View File

@ -37,10 +37,7 @@ func (n *NodeApplyableProvider) Execute(ctx EvalContext, op walkOperation) (diag
case walkValidate:
log.Printf("[TRACE] NodeApplyableProvider: validating configuration for %s", n.Addr)
return diags.Append(n.ValidateProvider(ctx, provider))
case walkPlan, walkApply, walkDestroy:
// walkPlanDestroy is purposely skipped here, since the config is not
// evaluated, and the provider is not needed to create delete actions
// for all instances.
case walkPlan, walkPlanDestroy, walkApply, walkDestroy:
log.Printf("[TRACE] NodeApplyableProvider: configuring %s", n.Addr)
return diags.Append(n.ConfigureProvider(ctx, provider, false))
case walkImport: