opentofu/internal/terraform/context_input.go
Martin Atkins 89b05050ec core: Functional-style API for terraform.Context
Previously terraform.Context was built in an unfortunate way where all of
the data was provided up front in terraform.NewContext and then mutated
directly by subsequent operations. That made the data flow hard to follow,
commonly leading to bugs, and also meant that we were forced to take
various actions too early in terraform.NewContext, rather than waiting
until a more appropriate time during an operation.

This (enormous) commit changes terraform.Context so that its fields are
broadly just unchanging data about the execution context (current
workspace name, available plugins, etc) whereas the main data Terraform
works with arrives via individual method arguments and is returned in
return values.

Specifically, this means that terraform.Context no longer "has-a" config,
state, and "planned changes", instead holding on to those only temporarily
during an operation. The caller is responsible for propagating the outcome
of one step into the next step so that the data flow between operations is
actually visible.

However, since that's a change to the main entry points in the "terraform"
package, this commit also touches every file in the codebase which
interacted with those APIs. Most of the noise here is in updating tests
to take the same actions using the new API style, but this also affects
the main-code callers in the backends and in the command package.

My goal here was to refactor without changing observable behavior, but in
practice there are a couple externally-visible behavior variations here
that seemed okay in service of the broader goal:
 - The "terraform graph" command is no longer hooked directly into the
   core graph builders, because that's no longer part of the public API.
   However, I did include a couple new Context functions whose contract
   is to produce a UI-oriented graph, and _for now_ those continue to
   return the physical graph we use for those operations. There's no
   exported API for generating the "validate" and "eval" graphs, because
   neither is particularly interesting in its own right, and so
   "terraform graph" no longer supports those graph types.
 - terraform.NewContext no longer has the responsibility for collecting
   all of the provider schemas up front. Instead, we wait until we need
   them. However, that means that some of our error messages now have a
   slightly different shape due to unwinding through a differently-shaped
   call stack. As of this commit we also end up reloading the schemas
   multiple times in some cases, which is functionally acceptable but
   likely represents a performance regression. I intend to rework this to
   use caching, but I'm saving that for a later commit because this one is
   big enough already.

The proximal reason for this change is to resolve the chicken/egg problem
whereby there was previously no single point where we could apply "moved"
statements to the previous run state before creating a plan. With this
change in place, we can now do that as part of Context.Plan, prior to
forking the input state into the three separate state artifacts we use
during planning.

However, this is at least the third project in a row where the previous
API design led to piling more functionality into terraform.NewContext and
then working around the incorrect order of operations that produces, so
I intend that by paying the cost/risk of this large diff now we can in
turn reduce the cost/risk of future projects that relate to our main
workflow actions.
2021-08-30 13:59:14 -07:00

207 lines
6.8 KiB
Go

package terraform
import (
"context"
"log"
"sort"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// Input asks for input to fill unset required arguments in provider
// configurations.
//
// Unlike the other better-behaved operation methods, this one actually
// modifies some internal state inside the receving context so that the
// captured values will be implicitly available to a subsequent call to Plan,
// or to some other operation entry point. Hopefully a future iteration of
// this will change design to make that data flow more explicit.
//
// Because Input saves the results inside the Context object, asking for
// input twice on the same Context is invalid and will lead to undefined
// behavior.
//
// Once you've called Input with a particular config, it's invalid to call
// any other Context method with a different config, because the aforementioned
// modified internal state won't match. Again, this is an architectural wart
// that we'll hopefully resolve in future.
func (c *Context) Input(config *configs.Config, mode InputMode) tfdiags.Diagnostics {
// This function used to be responsible for more than it is now, so its
// interface is more general than its current functionality requires.
// It now exists only to handle interactive prompts for provider
// configurations, with other prompts the responsibility of the CLI
// layer prior to calling in to this package.
//
// (Hopefully in future the remaining functionality here can move to the
// CLI layer too in order to avoid this odd situation where core code
// produces UI input prompts.)
var diags tfdiags.Diagnostics
defer c.acquireRun("input")()
schemas, moreDiags := c.Schemas(config, nil)
diags = diags.Append(moreDiags)
if moreDiags.HasErrors() {
return diags
}
if c.uiInput == nil {
log.Printf("[TRACE] Context.Input: uiInput is nil, so skipping")
return diags
}
ctx := context.Background()
if mode&InputModeProvider != 0 {
log.Printf("[TRACE] Context.Input: Prompting for provider arguments")
// We prompt for input only for provider configurations defined in
// the root module. Provider configurations in other modules are a
// legacy thing we no longer recommend, and even if they weren't we
// can't practically prompt for their inputs here because we've not
// yet done "expansion" and so we don't know whether the modules are
// using count or for_each.
pcs := make(map[string]*configs.Provider)
pas := make(map[string]addrs.LocalProviderConfig)
for _, pc := range config.Module.ProviderConfigs {
addr := pc.Addr()
pcs[addr.String()] = pc
pas[addr.String()] = addr
log.Printf("[TRACE] Context.Input: Provider %s declared at %s", addr, pc.DeclRange)
}
// We also need to detect _implied_ provider configs from resources.
// These won't have *configs.Provider objects, but they will still
// exist in the map and we'll just treat them as empty below.
for _, rc := range config.Module.ManagedResources {
pa := rc.ProviderConfigAddr()
if pa.Alias != "" {
continue // alias configurations cannot be implied
}
if _, exists := pcs[pa.String()]; !exists {
pcs[pa.String()] = nil
pas[pa.String()] = pa
log.Printf("[TRACE] Context.Input: Provider %s implied by resource block at %s", pa, rc.DeclRange)
}
}
for _, rc := range config.Module.DataResources {
pa := rc.ProviderConfigAddr()
if pa.Alias != "" {
continue // alias configurations cannot be implied
}
if _, exists := pcs[pa.String()]; !exists {
pcs[pa.String()] = nil
pas[pa.String()] = pa
log.Printf("[TRACE] Context.Input: Provider %s implied by data block at %s", pa, rc.DeclRange)
}
}
for pk, pa := range pas {
pc := pcs[pk] // will be nil if this is an implied config
// Wrap the input into a namespace
input := &PrefixUIInput{
IdPrefix: pk,
QueryPrefix: pk + ".",
UIInput: c.uiInput,
}
providerFqn := config.Module.ProviderForLocalConfig(pa)
schema := schemas.ProviderConfig(providerFqn)
if schema == nil {
// Could either be an incorrect config or just an incomplete
// mock in tests. We'll let a later pass decide, and just
// ignore this for the purposes of gathering input.
log.Printf("[TRACE] Context.Input: No schema available for provider type %q", pa.LocalName)
continue
}
// For our purposes here we just want to detect if attrbutes are
// set in config at all, so rather than doing a full decode
// (which would require us to prepare an evalcontext, etc) we'll
// use the low-level HCL API to process only the top-level
// structure.
var attrExprs hcl.Attributes // nil if there is no config
if pc != nil && pc.Config != nil {
lowLevelSchema := schemaForInputSniffing(hcldec.ImpliedSchema(schema.DecoderSpec()))
content, _, diags := pc.Config.PartialContent(lowLevelSchema)
if diags.HasErrors() {
log.Printf("[TRACE] Context.Input: %s has decode error, so ignoring: %s", pa, diags.Error())
continue
}
attrExprs = content.Attributes
}
keys := make([]string, 0, len(schema.Attributes))
for key := range schema.Attributes {
keys = append(keys, key)
}
sort.Strings(keys)
vals := map[string]cty.Value{}
for _, key := range keys {
attrS := schema.Attributes[key]
if attrS.Optional {
continue
}
if attrExprs != nil {
if _, exists := attrExprs[key]; exists {
continue
}
}
if !attrS.Type.Equals(cty.String) {
continue
}
log.Printf("[TRACE] Context.Input: Prompting for %s argument %s", pa, key)
rawVal, err := input.Input(ctx, &InputOpts{
Id: key,
Query: key,
Description: attrS.Description,
})
if err != nil {
log.Printf("[TRACE] Context.Input: Failed to prompt for %s argument %s: %s", pa, key, err)
continue
}
vals[key] = cty.StringVal(rawVal)
}
absConfigAddr := addrs.AbsProviderConfig{
Provider: providerFqn,
Alias: pa.Alias,
Module: config.Path,
}
c.providerInputConfig[absConfigAddr.String()] = vals
log.Printf("[TRACE] Context.Input: Input for %s: %#v", pk, vals)
}
}
return diags
}
// schemaForInputSniffing returns a transformed version of a given schema
// that marks all attributes as optional, which the Context.Input method can
// use to detect whether a required argument is set without missing arguments
// themselves generating errors.
func schemaForInputSniffing(schema *hcl.BodySchema) *hcl.BodySchema {
ret := &hcl.BodySchema{
Attributes: make([]hcl.AttributeSchema, len(schema.Attributes)),
Blocks: schema.Blocks,
}
for i, attrS := range schema.Attributes {
ret.Attributes[i] = attrS
ret.Attributes[i].Required = false
}
return ret
}