2018-05-31 22:03:03 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2019-03-07 14:07:13 -06:00
|
|
|
"context"
|
2018-05-31 22:03:03 -05:00
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2018-05-31 22:03:03 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
)
|
|
|
|
|
2019-10-08 14:08:27 -05:00
|
|
|
// Input asks for input to fill unset required arguments in provider
|
|
|
|
// configurations.
|
|
|
|
//
|
2018-05-31 22:03:03 -05:00
|
|
|
// This modifies the configuration in-place, so asking for Input twice
|
|
|
|
// may result in different UI output showing different current values.
|
|
|
|
func (c *Context) Input(mode InputMode) tfdiags.Diagnostics {
|
2019-10-08 14:08:27 -05:00
|
|
|
// 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.)
|
|
|
|
|
2018-05-31 22:03:03 -05:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
defer c.acquireRun("input")()
|
|
|
|
|
|
|
|
if c.uiInput == nil {
|
|
|
|
log.Printf("[TRACE] Context.Input: uiInput is nil, so skipping")
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:07:13 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-31 22:03:03 -05:00
|
|
|
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. At the time of writing that is an arbitrary
|
|
|
|
// restriction, but we have future plans to support "count" and
|
|
|
|
// "for_each" on modules that will then prevent us from supporting
|
|
|
|
// input for child module configurations anyway (since we'd need to
|
|
|
|
// dynamic-expand first), and provider configurations in child modules
|
|
|
|
// are not recommended since v0.11 anyway, so this restriction allows
|
|
|
|
// us to keep this relatively simple without significant hardship.
|
|
|
|
|
|
|
|
pcs := make(map[string]*configs.Provider)
|
Initial steps towards AbsProviderConfig/LocalProviderConfig separation (#23978)
* Introduce "Local" terminology for non-absolute provider config addresses
In a future change AbsProviderConfig and LocalProviderConfig are going to
become two entirely distinct types, rather than Abs embedding Local as
written here. This naming change is in preparation for that subsequent
work, which will also include introducing a new "ProviderConfig" type
that is an interface that AbsProviderConfig and LocalProviderConfig both
implement.
This is intended to be largely just a naming change to get started, so
we can deal with all of the messy renaming. However, this did also require
a slight change in modeling where the Resource.DefaultProviderConfig
method has become Resource.DefaultProvider returning a Provider address
directly, because this method doesn't have enough information to construct
a true and accurate LocalProviderConfig -- it would need to refer to the
configuration to know what this module is calling the provider it has
selected.
In order to leave a trail to follow for subsequent work, all of the
changes here are intended to ensure that remaining work will become
obvious via compile-time errors when all of the following changes happen:
- The concept of "legacy" provider addresses is removed from the addrs
package, including removing addrs.NewLegacyProvider and
addrs.Provider.LegacyString.
- addrs.AbsProviderConfig stops having addrs.LocalProviderConfig embedded
in it and has an addrs.Provider and a string alias directly instead.
- The provider-schema-handling parts of Terraform core are updated to
work with addrs.Provider to identify providers, rather than legacy
strings.
In particular, there are still several codepaths here making legacy
provider address assumptions (in order to limit the scope of this change)
but I've made sure each one is doing something that relies on at least
one of the above changes not having been made yet.
* addrs: ProviderConfig interface
In a (very) few special situations in the main "terraform" package we need
to make runtime decisions about whether a provider config is absolute
or local.
We currently do that by exploiting the fact that AbsProviderConfig has
LocalProviderConfig nested inside of it and so in the local case we can
just ignore the wrapping AbsProviderConfig and use the embedded value.
In a future change we'll be moving away from that embedding and making
these two types distinct in order to represent that mapping between them
requires consulting a lookup table in the configuration, and so here we
introduce a new interface type ProviderConfig that can represent either
AbsProviderConfig or LocalProviderConfig decided dynamically at runtime.
This also includes the Config.ResolveAbsProviderAddr method that will
eventually be responsible for that local-to-absolute translation, so
that callers with access to the configuration can normalize to an
addrs.AbsProviderConfig given a non-nil addrs.ProviderConfig. That's
currently unused because existing callers are still relying on the
simplistic structural transform, but we'll switch them over in a later
commit.
* rename LocalType to LocalName
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
2020-01-31 07:23:07 -06:00
|
|
|
pas := make(map[string]addrs.LocalProviderConfig)
|
2018-05-31 22:03:03 -05:00
|
|
|
for _, pc := range c.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 c.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 c.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,
|
|
|
|
}
|
|
|
|
|
2020-02-14 09:19:24 -06:00
|
|
|
providerFqn := c.config.Module.ProviderForLocalConfig(pa)
|
2020-02-03 07:18:04 -06:00
|
|
|
schema := c.schemas.ProviderConfig(providerFqn)
|
2018-05-31 22:03:03 -05:00
|
|
|
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.
|
Initial steps towards AbsProviderConfig/LocalProviderConfig separation (#23978)
* Introduce "Local" terminology for non-absolute provider config addresses
In a future change AbsProviderConfig and LocalProviderConfig are going to
become two entirely distinct types, rather than Abs embedding Local as
written here. This naming change is in preparation for that subsequent
work, which will also include introducing a new "ProviderConfig" type
that is an interface that AbsProviderConfig and LocalProviderConfig both
implement.
This is intended to be largely just a naming change to get started, so
we can deal with all of the messy renaming. However, this did also require
a slight change in modeling where the Resource.DefaultProviderConfig
method has become Resource.DefaultProvider returning a Provider address
directly, because this method doesn't have enough information to construct
a true and accurate LocalProviderConfig -- it would need to refer to the
configuration to know what this module is calling the provider it has
selected.
In order to leave a trail to follow for subsequent work, all of the
changes here are intended to ensure that remaining work will become
obvious via compile-time errors when all of the following changes happen:
- The concept of "legacy" provider addresses is removed from the addrs
package, including removing addrs.NewLegacyProvider and
addrs.Provider.LegacyString.
- addrs.AbsProviderConfig stops having addrs.LocalProviderConfig embedded
in it and has an addrs.Provider and a string alias directly instead.
- The provider-schema-handling parts of Terraform core are updated to
work with addrs.Provider to identify providers, rather than legacy
strings.
In particular, there are still several codepaths here making legacy
provider address assumptions (in order to limit the scope of this change)
but I've made sure each one is doing something that relies on at least
one of the above changes not having been made yet.
* addrs: ProviderConfig interface
In a (very) few special situations in the main "terraform" package we need
to make runtime decisions about whether a provider config is absolute
or local.
We currently do that by exploiting the fact that AbsProviderConfig has
LocalProviderConfig nested inside of it and so in the local case we can
just ignore the wrapping AbsProviderConfig and use the embedded value.
In a future change we'll be moving away from that embedding and making
these two types distinct in order to represent that mapping between them
requires consulting a lookup table in the configuration, and so here we
introduce a new interface type ProviderConfig that can represent either
AbsProviderConfig or LocalProviderConfig decided dynamically at runtime.
This also includes the Config.ResolveAbsProviderAddr method that will
eventually be responsible for that local-to-absolute translation, so
that callers with access to the configuration can normalize to an
addrs.AbsProviderConfig given a non-nil addrs.ProviderConfig. That's
currently unused because existing callers are still relying on the
simplistic structural transform, but we'll switch them over in a later
commit.
* rename LocalType to LocalName
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
2020-01-31 07:23:07 -06:00
|
|
|
log.Printf("[TRACE] Context.Input: No schema available for provider type %q", pa.LocalName)
|
2018-05-31 22:03:03 -05:00
|
|
|
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)
|
2019-03-07 14:07:13 -06:00
|
|
|
rawVal, err := input.Input(ctx, &InputOpts{
|
2018-05-31 22:03:03 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:32:58 -06:00
|
|
|
absConfigAddr := addrs.AbsProviderConfig{
|
|
|
|
Provider: providerFqn,
|
|
|
|
Alias: pa.Alias,
|
|
|
|
Module: c.Config().Path.UnkeyedInstanceShim(),
|
|
|
|
}
|
|
|
|
c.providerInputConfig[absConfigAddr.String()] = vals
|
|
|
|
|
2018-05-31 22:03:03 -05:00
|
|
|
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
|
|
|
|
}
|