2018-06-29 02:26:06 +02:00
|
|
|
package configupgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-01 01:51:45 +01:00
|
|
|
"log"
|
2018-12-06 20:42:33 +01:00
|
|
|
"strings"
|
2018-06-29 02:26:06 +02:00
|
|
|
|
|
|
|
hcl1 "github.com/hashicorp/hcl"
|
|
|
|
hcl1ast "github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
hcl1parser "github.com/hashicorp/hcl/hcl/parser"
|
2018-12-01 18:58:39 +01:00
|
|
|
hcl1token "github.com/hashicorp/hcl/hcl/token"
|
2018-06-29 02:26:06 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-07-05 19:33:29 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-06-29 02:26:06 +02:00
|
|
|
"github.com/hashicorp/terraform/moduledeps"
|
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// analysis is a container for the various different information gathered
|
2018-11-29 02:00:33 +01:00
|
|
|
// by Upgrader.analyze.
|
2018-06-29 02:26:06 +02:00
|
|
|
type analysis struct {
|
|
|
|
ProviderSchemas map[string]*terraform.ProviderSchema
|
|
|
|
ProvisionerSchemas map[string]*configschema.Block
|
|
|
|
ResourceProviderType map[addrs.Resource]string
|
|
|
|
ResourceHasCount map[addrs.Resource]bool
|
2018-12-06 20:42:33 +01:00
|
|
|
VariableTypes map[string]string
|
2019-03-13 19:17:14 +01:00
|
|
|
ModuleDir string
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// analyze processes the configuration files included inside the receiver
|
|
|
|
// and returns an assortment of information required to make decisions during
|
|
|
|
// a configuration upgrade.
|
|
|
|
func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) {
|
|
|
|
ret := &analysis{
|
|
|
|
ProviderSchemas: make(map[string]*terraform.ProviderSchema),
|
|
|
|
ProvisionerSchemas: make(map[string]*configschema.Block),
|
|
|
|
ResourceProviderType: make(map[addrs.Resource]string),
|
|
|
|
ResourceHasCount: make(map[addrs.Resource]bool),
|
2018-12-06 20:42:33 +01:00
|
|
|
VariableTypes: make(map[string]string),
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m := &moduledeps.Module{
|
|
|
|
Providers: make(moduledeps.Providers),
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is heavily based on terraform.ModuleTreeDependencies but
|
|
|
|
// differs in that it works directly with the HCL1 AST rather than
|
|
|
|
// the legacy config structs (and can thus outlive those) and that
|
|
|
|
// it only works on one module at a time, and so doesn't need to
|
|
|
|
// recurse into child calls.
|
|
|
|
for name, src := range ms {
|
|
|
|
if ext := fileExt(name); ext != ".tf" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:51:45 +01:00
|
|
|
log.Printf("[TRACE] configupgrade: Analyzing %q", name)
|
|
|
|
|
2018-06-29 02:26:06 +02:00
|
|
|
f, err := hcl1parser.Parse(src)
|
|
|
|
if err != nil {
|
|
|
|
// If we encounter a syntax error then we'll just skip for now
|
|
|
|
// and assume that we'll catch this again when we do the upgrade.
|
|
|
|
// If not, we'll break the upgrade step of renaming .tf files to
|
|
|
|
// .tf.json if they seem to be JSON syntax.
|
2018-12-01 01:51:45 +01:00
|
|
|
log.Printf("[ERROR] Failed to parse %q: %s", name, err)
|
2018-06-29 02:26:06 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
list, ok := f.Node.(*hcl1ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
|
|
|
|
}
|
|
|
|
|
|
|
|
if providersList := list.Filter("provider"); len(providersList.Items) > 0 {
|
|
|
|
providerObjs := providersList.Children()
|
|
|
|
for _, providerObj := range providerObjs.Items {
|
|
|
|
if len(providerObj.Keys) != 1 {
|
|
|
|
return nil, fmt.Errorf("provider block has wrong number of labels")
|
|
|
|
}
|
|
|
|
name := providerObj.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *hcl1ast.ObjectList
|
|
|
|
if ot, ok := providerObj.Val.(*hcl1ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("provider %q: must be a block", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var versionStr string
|
|
|
|
if a := listVal.Filter("version"); len(a.Items) > 0 {
|
|
|
|
err := hcl1.DecodeObject(&versionStr, a.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading version for provider %q: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var constraints discovery.Constraints
|
|
|
|
if versionStr != "" {
|
|
|
|
constraints, err = discovery.ConstraintStr(versionStr).Parse()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing version for provider %q: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var alias string
|
|
|
|
if a := listVal.Filter("alias"); len(a.Items) > 0 {
|
|
|
|
err := hcl1.DecodeObject(&alias, a.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading alias for provider %q: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inst := moduledeps.ProviderInstance(name)
|
|
|
|
if alias != "" {
|
|
|
|
inst = moduledeps.ProviderInstance(name + "." + alias)
|
|
|
|
}
|
2018-12-01 01:51:45 +01:00
|
|
|
log.Printf("[TRACE] Provider block requires provider %q", inst)
|
2018-06-29 02:26:06 +02:00
|
|
|
m.Providers[inst] = moduledeps.ProviderDependency{
|
|
|
|
Constraints: constraints,
|
|
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
resourceConfigsList := list.Filter("resource")
|
|
|
|
dataResourceConfigsList := list.Filter("data")
|
2018-12-01 18:58:39 +01:00
|
|
|
// list.Filter annoyingly strips off the key used for matching,
|
|
|
|
// so we'll put it back here so we can distinguish our two types
|
|
|
|
// of blocks below.
|
|
|
|
for _, obj := range resourceConfigsList.Items {
|
|
|
|
obj.Keys = append([]*hcl1ast.ObjectKey{
|
|
|
|
{Token: hcl1token.Token{Type: hcl1token.IDENT, Text: "resource"}},
|
|
|
|
}, obj.Keys...)
|
|
|
|
}
|
|
|
|
for _, obj := range dataResourceConfigsList.Items {
|
|
|
|
obj.Keys = append([]*hcl1ast.ObjectKey{
|
|
|
|
{Token: hcl1token.Token{Type: hcl1token.IDENT, Text: "data"}},
|
|
|
|
}, obj.Keys...)
|
|
|
|
}
|
|
|
|
// Now we can merge the two lists together, since we can distinguish
|
|
|
|
// them just by their keys[0].
|
2018-06-29 02:26:06 +02:00
|
|
|
resourceConfigsList.Items = append(resourceConfigsList.Items, dataResourceConfigsList.Items...)
|
|
|
|
|
|
|
|
resourceObjs := resourceConfigsList.Children()
|
|
|
|
for _, resourceObj := range resourceObjs.Items {
|
2018-12-01 18:58:39 +01:00
|
|
|
if len(resourceObj.Keys) != 3 {
|
2018-06-29 02:26:06 +02:00
|
|
|
return nil, fmt.Errorf("resource or data block has wrong number of labels")
|
|
|
|
}
|
2018-12-01 18:58:39 +01:00
|
|
|
typeName := resourceObj.Keys[1].Token.Value().(string)
|
|
|
|
name := resourceObj.Keys[2].Token.Value().(string)
|
2018-06-29 02:26:06 +02:00
|
|
|
rAddr := addrs.Resource{
|
2018-12-01 18:58:39 +01:00
|
|
|
Mode: addrs.ManagedResourceMode,
|
2018-06-29 02:26:06 +02:00
|
|
|
Type: typeName,
|
|
|
|
Name: name,
|
|
|
|
}
|
2018-12-01 18:58:39 +01:00
|
|
|
if resourceObj.Keys[0].Token.Value() == "data" {
|
|
|
|
rAddr.Mode = addrs.DataResourceMode
|
|
|
|
}
|
2018-06-29 02:26:06 +02:00
|
|
|
|
|
|
|
var listVal *hcl1ast.ObjectList
|
|
|
|
if ot, ok := resourceObj.Val.(*hcl1ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
2018-12-01 18:58:39 +01:00
|
|
|
return nil, fmt.Errorf("config for %q must be a block", rAddr)
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if o := listVal.Filter("count"); len(o.Items) > 0 {
|
|
|
|
ret.ResourceHasCount[rAddr] = true
|
2018-12-01 21:03:16 +01:00
|
|
|
} else {
|
|
|
|
ret.ResourceHasCount[rAddr] = false
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var providerKey string
|
|
|
|
if o := listVal.Filter("provider"); len(o.Items) > 0 {
|
|
|
|
err := hcl1.DecodeObject(&providerKey, o.Items[0].Val)
|
|
|
|
if err != nil {
|
2018-12-01 18:58:39 +01:00
|
|
|
return nil, fmt.Errorf("Error reading provider for resource %s: %s", rAddr, err)
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerKey == "" {
|
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 14:23:07 +01:00
|
|
|
providerKey = rAddr.DefaultProvider().LegacyString()
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inst := moduledeps.ProviderInstance(providerKey)
|
2018-12-01 18:58:39 +01:00
|
|
|
log.Printf("[TRACE] Resource block for %s requires provider %q", rAddr, inst)
|
2018-06-29 02:26:06 +02:00
|
|
|
if _, exists := m.Providers[inst]; !exists {
|
|
|
|
m.Providers[inst] = moduledeps.ProviderDependency{
|
|
|
|
Reason: moduledeps.ProviderDependencyImplicit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.ResourceProviderType[rAddr] = inst.Type()
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 20:42:33 +01:00
|
|
|
|
|
|
|
if variablesList := list.Filter("variable"); len(variablesList.Items) > 0 {
|
|
|
|
variableObjs := variablesList.Children()
|
|
|
|
for _, variableObj := range variableObjs.Items {
|
|
|
|
if len(variableObj.Keys) != 1 {
|
|
|
|
return nil, fmt.Errorf("variable block has wrong number of labels")
|
|
|
|
}
|
|
|
|
name := variableObj.Keys[0].Token.Value().(string)
|
|
|
|
|
|
|
|
var listVal *hcl1ast.ObjectList
|
|
|
|
if ot, ok := variableObj.Val.(*hcl1ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("variable %q: must be a block", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var typeStr string
|
|
|
|
if a := listVal.Filter("type"); len(a.Items) > 0 {
|
|
|
|
err := hcl1.DecodeObject(&typeStr, a.Items[0].Val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading type for variable %q: %s", name, err)
|
|
|
|
}
|
|
|
|
} else if a := listVal.Filter("default"); len(a.Items) > 0 {
|
|
|
|
switch a.Items[0].Val.(type) {
|
|
|
|
case *hcl1ast.ObjectType:
|
|
|
|
typeStr = "map"
|
|
|
|
case *hcl1ast.ListType:
|
|
|
|
typeStr = "list"
|
|
|
|
default:
|
|
|
|
typeStr = "string"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
typeStr = "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.VariableTypes[name] = strings.TrimSpace(typeStr)
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 14:14:59 +02:00
|
|
|
providerFactories, errs := u.Providers.ResolveProviders(m.PluginRequirements())
|
|
|
|
if len(errs) > 0 {
|
|
|
|
var errorsMsg string
|
|
|
|
for _, err := range errs {
|
|
|
|
errorsMsg += fmt.Sprintf("\n- %s", err)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("error resolving providers:\n%s", errorsMsg)
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
for fqn, fn := range providerFactories {
|
|
|
|
log.Printf("[TRACE] Fetching schema from provider %q", fqn.LegacyString())
|
2018-06-29 02:26:06 +02:00
|
|
|
provider, err := fn()
|
|
|
|
if err != nil {
|
2019-12-04 17:30:20 +01:00
|
|
|
return nil, fmt.Errorf("failed to load provider %q: %s", fqn.LegacyString(), err)
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 01:13:50 +02:00
|
|
|
resp := provider.GetSchema()
|
|
|
|
if resp.Diagnostics.HasErrors() {
|
|
|
|
return nil, resp.Diagnostics.Err()
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
2018-08-25 01:13:50 +02:00
|
|
|
|
|
|
|
schema := &terraform.ProviderSchema{
|
|
|
|
Provider: resp.Provider.Block,
|
|
|
|
ResourceTypes: map[string]*configschema.Block{},
|
|
|
|
DataSources: map[string]*configschema.Block{},
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
2018-08-25 01:13:50 +02:00
|
|
|
for t, s := range resp.ResourceTypes {
|
|
|
|
schema.ResourceTypes[t] = s.Block
|
|
|
|
}
|
|
|
|
for t, s := range resp.DataSources {
|
|
|
|
schema.DataSources[t] = s.Block
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
2019-12-04 17:30:20 +01:00
|
|
|
ret.ProviderSchemas[fqn.LegacyString()] = schema
|
2018-06-29 02:26:06 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 01:13:17 +01:00
|
|
|
for name, fn := range u.Provisioners {
|
|
|
|
log.Printf("[TRACE] Fetching schema from provisioner %q", name)
|
|
|
|
provisioner, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load provisioner %q: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := provisioner.GetSchema()
|
|
|
|
if resp.Diagnostics.HasErrors() {
|
|
|
|
return nil, resp.Diagnostics.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.ProvisionerSchemas[name] = resp.Provisioner
|
|
|
|
}
|
2018-06-29 02:26:06 +02:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|