Skip to content

Commit

Permalink
deferred actions: fix support for config generation when importing an…
Browse files Browse the repository at this point in the history
… unknown resource (#35319)
  • Loading branch information
liamcervante committed Jun 12, 2024
1 parent 5172cb5 commit f715c6e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/terraform/context_apply_deferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,36 @@ import {
},
}

unknownImportDefersConfigGeneration = deferredActionsTest{
configs: map[string]string{
"main.tf": `
variable "id" {
type = string
}
import {
id = var.id
to = test.a
}
`,
},
stages: []deferredActionsTestStage{
{
buildOpts: func(opts *PlanOpts) {
opts.GenerateConfigPath = "generated.tf"
},
inputs: map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
},
wantPlanned: make(map[string]cty.Value),
wantActions: make(map[string]plans.Action),
wantDeferred: map[string]ExpectedDeferred{
"test.a": {Reason: providers.DeferredReasonResourceConfigUnknown, Action: plans.NoOp},
},
},
},
}

unknownImportTo = deferredActionsTest{
configs: map[string]string{
"main.tf": `
Expand Down Expand Up @@ -2929,6 +2959,7 @@ func TestContextApply_deferredActions(t *testing.T) {
"module_deferred_for_each_value": moduleDeferredForEachValue,
"module_inner_resource_instance_deferred": moduleInnerResourceInstanceDeferred,
"unknown_import_id": unknownImportId,
"unknown_import_defers_config_generation": unknownImportDefersConfigGeneration,
"unknown_import_to": unknownImportTo,
"unknown_import_to_existing_state": unknownImportToExistingState,
"unknown_import_to_partial_existing_state": unknownImportToPartialExistingState,
Expand Down
31 changes: 31 additions & 0 deletions internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
deferred = &providers.Deferred{
Reason: providers.DeferredReasonResourceConfigUnknown,
}
if n.Config == nil && len(n.generateConfigPath) > 0 {
// Then we're supposed to be generating configuration for this
// resource, but we can't because the configuration is unknown.
//
// Normally, the rest of this function would just be about
// planning the known configuration to make sure everything we
// do know about it is correct, but we can't even do that here.
//
// What we'll do is write out the address as being deferred with
// an entirely unknown value. Then we'll skip the rest of this
// function. (a) We're going to panic later when it complains
// about having no configuration, and (b) the rest of the
// function isn't doing anything as there is no configuration
// to validate.

impliedType := providerSchema.ResourceTypes[addr.Resource.Resource.Type].Block.ImpliedType()
ctx.Deferrals().ReportResourceInstanceDeferred(addr, providers.DeferredReasonResourceConfigUnknown, &plans.ResourceInstanceChange{
Addr: addr,
PrevRunAddr: addr,
ProviderAddr: n.ResolvedProvider,
Change: plans.Change{
Action: plans.NoOp, // assume we'll get the config generation correct.
Before: cty.NullVal(impliedType),
After: cty.UnknownVal(impliedType),
Importing: &plans.Importing{
ID: n.importTarget,
},
},
})
return diags
}
}
} else {
var readDiags tfdiags.Diagnostics
Expand Down

0 comments on commit f715c6e

Please sign in to comment.