2015-02-03 03:43:18 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-31 22:03:03 -05:00
|
|
|
"log"
|
2015-02-13 19:59:54 -06:00
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2015-02-03 03:43:18 -06:00
|
|
|
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 12:40:40 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
)
|
2015-02-13 19:59:54 -06:00
|
|
|
|
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
|
|
|
func buildProviderConfig(ctx EvalContext, addr addrs.AbsProviderConfig, config *configs.Provider) hcl.Body {
|
2018-05-31 22:03:03 -05:00
|
|
|
var configBody hcl.Body
|
|
|
|
if config != nil {
|
|
|
|
configBody = config.Config
|
|
|
|
}
|
2015-02-13 19:59:54 -06:00
|
|
|
|
2018-05-31 22:03:03 -05:00
|
|
|
var inputBody hcl.Body
|
|
|
|
inputConfig := ctx.ProviderInput(addr)
|
|
|
|
if len(inputConfig) > 0 {
|
|
|
|
inputBody = configs.SynthBody("<input-prompt>", inputConfig)
|
2015-02-13 19:59:54 -06:00
|
|
|
}
|
2015-02-10 01:32:28 -06:00
|
|
|
|
2018-05-31 22:03:03 -05:00
|
|
|
switch {
|
|
|
|
case configBody != nil && inputBody != nil:
|
|
|
|
log.Printf("[TRACE] buildProviderConfig for %s: merging explicit config and input", addr)
|
2021-06-25 07:48:47 -05:00
|
|
|
return hcl.MergeBodies([]hcl.Body{inputBody, configBody})
|
2018-05-31 22:03:03 -05:00
|
|
|
case configBody != nil:
|
|
|
|
log.Printf("[TRACE] buildProviderConfig for %s: using explicit config only", addr)
|
|
|
|
return configBody
|
|
|
|
case inputBody != nil:
|
|
|
|
log.Printf("[TRACE] buildProviderConfig for %s: using input only", addr)
|
|
|
|
return inputBody
|
|
|
|
default:
|
|
|
|
log.Printf("[TRACE] buildProviderConfig for %s: no configuration at all", addr)
|
|
|
|
return hcl.EmptyBody()
|
|
|
|
}
|
2015-03-25 18:28:52 -05:00
|
|
|
}
|
|
|
|
|
2020-12-10 08:55:50 -06:00
|
|
|
// getProvider returns the providers.Interface and schema for a given provider.
|
|
|
|
func getProvider(ctx EvalContext, addr addrs.AbsProviderConfig) (providers.Interface, *ProviderSchema, error) {
|
2020-09-08 09:58:59 -05:00
|
|
|
if addr.Provider.Type == "" {
|
|
|
|
// Should never happen
|
2020-12-04 11:44:40 -06:00
|
|
|
panic("GetProvider used with uninitialized provider configuration address")
|
2020-09-08 09:58:59 -05:00
|
|
|
}
|
|
|
|
provider := ctx.Provider(addr)
|
|
|
|
if provider == nil {
|
|
|
|
return nil, &ProviderSchema{}, fmt.Errorf("provider %s not initialized", addr)
|
|
|
|
}
|
|
|
|
// Not all callers require a schema, so we will leave checking for a nil
|
|
|
|
// schema to the callers.
|
2021-08-31 17:44:07 -05:00
|
|
|
schema, err := ctx.ProviderSchema(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ProviderSchema{}, fmt.Errorf("failed to read schema for provider %s: %w", addr, err)
|
|
|
|
}
|
2020-09-08 09:58:59 -05:00
|
|
|
return provider, schema, nil
|
|
|
|
}
|