From 096cddb4b794c0592640be736f6dc89cd3e08510 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 10 Dec 2021 16:27:40 -0800 Subject: [PATCH] command/format: Limitation of plans.ResourceInstanceDeleteBecauseNoModule This is an explicit technical debt note that our plan renderer isn't able to give a fully-specific hint in this particular case of deletion reason. This reason code means that at least one of the module instance keys in the resource's module path doesn't match an instance declared in the configuration, but the plan data structure doesn't retain enough information to know which is the first step in the path which refers to a missing instance, and so we just always return the whole thing. This would be confusing if we return module.foo[0].module.bar not being in the configuration as a result of module.foo not using "count"; it would be better to say "module.foo[0] is not in the configuration" instead. It would be most ideal to handle all of the different situations that ResourceInstanceDeleteBecauseWrongRepetition's rendering does, so that we can go further and explain exactly _why_ that module instance isn't declared anymore. We can do neither of those things today because only the Terraform Core "expander" component knows that information, and we've discarded that by the time we get to rendering a plan. To fix this one day would require preserving in the plan information about which module instances are declared, as a separate sidecar data structure from which resource instances we're taking actions on, and then using that to identify which step in addr.Module here first selects an invalid instance. --- internal/command/format/diff.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/command/format/diff.go b/internal/command/format/diff.go index 3d08109e54..1c1da14f48 100644 --- a/internal/command/format/diff.go +++ b/internal/command/format/diff.go @@ -107,6 +107,11 @@ func ResourceChange( case plans.ResourceInstanceDeleteBecauseNoResourceConfig: buf.WriteString(fmt.Sprintf("\n # (because %s is not in configuration)", addr.Resource.Resource)) case plans.ResourceInstanceDeleteBecauseNoModule: + // FIXME: Ideally we'd truncate addr.Module to reflect the earliest + // step that doesn't exist, so it's clearer which call this refers + // to, but we don't have enough information out here in the UI layer + // to decide that; only the "expander" in Terraform Core knows + // which module instance keys are actually declared. buf.WriteString(fmt.Sprintf("\n # (because %s is not in configuration)", addr.Module)) case plans.ResourceInstanceDeleteBecauseWrongRepetition: // We have some different variations of this one