mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
configs/configupgrade: Add some logging and enable it for tests
This commit is contained in:
parent
e8999eefdc
commit
1aa368d0d8
@ -2,6 +2,7 @@ package configupgrade
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
hcl1 "github.com/hashicorp/hcl"
|
hcl1 "github.com/hashicorp/hcl"
|
||||||
hcl1ast "github.com/hashicorp/hcl/hcl/ast"
|
hcl1ast "github.com/hashicorp/hcl/hcl/ast"
|
||||||
@ -48,12 +49,15 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[TRACE] configupgrade: Analyzing %q", name)
|
||||||
|
|
||||||
f, err := hcl1parser.Parse(src)
|
f, err := hcl1parser.Parse(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we encounter a syntax error then we'll just skip for now
|
// 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.
|
// 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
|
// If not, we'll break the upgrade step of renaming .tf files to
|
||||||
// .tf.json if they seem to be JSON syntax.
|
// .tf.json if they seem to be JSON syntax.
|
||||||
|
log.Printf("[ERROR] Failed to parse %q: %s", name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +108,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) {
|
|||||||
if alias != "" {
|
if alias != "" {
|
||||||
inst = moduledeps.ProviderInstance(name + "." + alias)
|
inst = moduledeps.ProviderInstance(name + "." + alias)
|
||||||
}
|
}
|
||||||
|
log.Printf("[TRACE] Provider block requires provider %q", inst)
|
||||||
m.Providers[inst] = moduledeps.ProviderDependency{
|
m.Providers[inst] = moduledeps.ProviderDependency{
|
||||||
Constraints: constraints,
|
Constraints: constraints,
|
||||||
Reason: moduledeps.ProviderDependencyExplicit,
|
Reason: moduledeps.ProviderDependencyExplicit,
|
||||||
@ -157,6 +162,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inst := moduledeps.ProviderInstance(providerKey)
|
inst := moduledeps.ProviderInstance(providerKey)
|
||||||
|
log.Printf("[TRACE] Resource block for %q %q requires provider %q", typeName, name, inst)
|
||||||
if _, exists := m.Providers[inst]; !exists {
|
if _, exists := m.Providers[inst]; !exists {
|
||||||
m.Providers[inst] = moduledeps.ProviderDependency{
|
m.Providers[inst] = moduledeps.ProviderDependency{
|
||||||
Reason: moduledeps.ProviderDependencyImplicit,
|
Reason: moduledeps.ProviderDependencyImplicit,
|
||||||
@ -173,6 +179,7 @@ func (u *Upgrader) analyze(ms ModuleSources) (*analysis, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for name, fn := range providerFactories {
|
for name, fn := range providerFactories {
|
||||||
|
log.Printf("[TRACE] Fetching schema from provider %q", name)
|
||||||
provider, err := fn()
|
provider, err := fn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load provider %q: %s", name, err)
|
return nil, fmt.Errorf("failed to load provider %q: %s", name, err)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package configupgrade
|
package configupgrade
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -31,6 +32,8 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
|||||||
var result upgradeFileResult
|
var result upgradeFileResult
|
||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
|
|
||||||
|
log.Printf("[TRACE] configupgrade: Working on %q", filename)
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
f, err := hcl1parser.Parse(src)
|
f, err := hcl1parser.Parse(src)
|
||||||
@ -100,6 +103,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
|||||||
rAddr.Mode = addrs.DataResourceMode
|
rAddr.Mode = addrs.DataResourceMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[TRACE] configupgrade: Upgrading %s at %s", rAddr, declRange)
|
||||||
moreDiags := u.upgradeNativeSyntaxResource(filename, &buf, rAddr, item, an, adhocComments)
|
moreDiags := u.upgradeNativeSyntaxResource(filename, &buf, rAddr, item, an, adhocComments)
|
||||||
diags = diags.Append(moreDiags)
|
diags = diags.Append(moreDiags)
|
||||||
|
|
||||||
@ -115,6 +119,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
|||||||
}
|
}
|
||||||
|
|
||||||
pType := labels[0]
|
pType := labels[0]
|
||||||
|
log.Printf("[TRACE] configupgrade: Upgrading provider.%s at %s", pType, declRange)
|
||||||
moreDiags := u.upgradeNativeSyntaxProvider(filename, &buf, pType, item, an, adhocComments)
|
moreDiags := u.upgradeNativeSyntaxProvider(filename, &buf, pType, item, an, adhocComments)
|
||||||
diags = diags.Append(moreDiags)
|
diags = diags.Append(moreDiags)
|
||||||
|
|
||||||
@ -155,6 +160,7 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
|||||||
"map": `map(string)`,
|
"map": `map(string)`,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
log.Printf("[TRACE] configupgrade: Upgrading var.%s at %s", labels[0], declRange)
|
||||||
bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("var.%s", labels[0]), &buf, body.List.Items, rules, adhocComments)
|
bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("var.%s", labels[0]), &buf, body.List.Items, rules, adhocComments)
|
||||||
diags = diags.Append(bodyDiags)
|
diags = diags.Append(bodyDiags)
|
||||||
buf.WriteString("}\n\n")
|
buf.WriteString("}\n\n")
|
||||||
@ -179,11 +185,13 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
|||||||
"sensitive": noInterpAttributeRule(filename, cty.Bool, an),
|
"sensitive": noInterpAttributeRule(filename, cty.Bool, an),
|
||||||
"depends_on": dependsOnAttributeRule(filename, an),
|
"depends_on": dependsOnAttributeRule(filename, an),
|
||||||
}
|
}
|
||||||
|
log.Printf("[TRACE] configupgrade: Upgrading output.%s at %s", labels[0], declRange)
|
||||||
bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("output.%s", labels[0]), &buf, body.List.Items, rules, adhocComments)
|
bodyDiags := u.upgradeBlockBody(filename, fmt.Sprintf("output.%s", labels[0]), &buf, body.List.Items, rules, adhocComments)
|
||||||
diags = diags.Append(bodyDiags)
|
diags = diags.Append(bodyDiags)
|
||||||
buf.WriteString("}\n\n")
|
buf.WriteString("}\n\n")
|
||||||
|
|
||||||
case "locals":
|
case "locals":
|
||||||
|
log.Printf("[TRACE] configupgrade: Upgrading locals block at %s", declRange)
|
||||||
printComments(&buf, item.LeadComment)
|
printComments(&buf, item.LeadComment)
|
||||||
printBlockOpen(&buf, blockType, labels, item.LineComment)
|
printBlockOpen(&buf, blockType, labels, item.LineComment)
|
||||||
|
|
||||||
@ -280,7 +288,7 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe
|
|||||||
panic(fmt.Sprintf("missing schema for provider type %q", providerType))
|
panic(fmt.Sprintf("missing schema for provider type %q", providerType))
|
||||||
}
|
}
|
||||||
schema, _ := providerSchema.SchemaForResourceAddr(addr)
|
schema, _ := providerSchema.SchemaForResourceAddr(addr)
|
||||||
if !ok {
|
if schema == nil {
|
||||||
diags = diags.Append(&hcl2.Diagnostic{
|
diags = diags.Append(&hcl2.Diagnostic{
|
||||||
Severity: hcl2.DiagError,
|
Severity: hcl2.DiagError,
|
||||||
Summary: "Unknown resource type",
|
Summary: "Unknown resource type",
|
||||||
|
@ -4,15 +4,18 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
backendinit "github.com/hashicorp/terraform/backend/init"
|
backendinit "github.com/hashicorp/terraform/backend/init"
|
||||||
"github.com/hashicorp/terraform/configs/configschema"
|
"github.com/hashicorp/terraform/configs/configschema"
|
||||||
|
"github.com/hashicorp/terraform/helper/logging"
|
||||||
"github.com/hashicorp/terraform/providers"
|
"github.com/hashicorp/terraform/providers"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
@ -213,3 +216,22 @@ func init() {
|
|||||||
// Initialize the backends
|
// Initialize the backends
|
||||||
backendinit.Init(nil)
|
backendinit.Init(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
if testing.Verbose() {
|
||||||
|
// if we're verbose, use the logging requested by TF_LOG
|
||||||
|
logging.SetOutput()
|
||||||
|
} else {
|
||||||
|
// otherwise silence all logs
|
||||||
|
log.SetOutput(ioutil.Discard)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have fmt.Stringer implementations on lots of objects that hide
|
||||||
|
// details that we very often want to see in tests, so we just disable
|
||||||
|
// spew's use of String methods globally on the assumption that spew
|
||||||
|
// usage implies an intent to see the raw values and ignore any
|
||||||
|
// abstractions.
|
||||||
|
spew.Config.DisableMethods = true
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user