remove ParentProviderTransformer

There is no longer any concept of a "parent provider". Inheritance of
default providers is discovered and connected in the ProviderTransformer
itself.
This commit is contained in:
James Bardin 2021-02-18 12:42:01 -05:00
parent 0da0b24527
commit b1d8d856ca
2 changed files with 51 additions and 38 deletions

View File

@ -31,8 +31,6 @@ func TransformProviders(providers []string, concrete ConcreteProviderNodeFunc, c
},
// Remove unused providers and proxies
&PruneProviderTransformer{},
// Connect provider to their parent provider nodes
&ParentProviderTransformer{},
)
}
@ -357,42 +355,6 @@ func (t *MissingProviderTransformer) Transform(g *Graph) error {
return err
}
// ParentProviderTransformer connects provider nodes to their parents.
//
// This works by finding nodes that are both GraphNodeProviders and
// GraphNodeModuleInstance. It then connects the providers to their parent
// path. The parent provider is always at the root level.
type ParentProviderTransformer struct{}
func (t *ParentProviderTransformer) Transform(g *Graph) error {
pm := providerVertexMap(g)
for _, v := range g.Vertices() {
// Only care about providers
pn, ok := v.(GraphNodeProvider)
if !ok {
continue
}
// Also require non-empty path, since otherwise we're in the root
// module and so cannot have a parent.
if len(pn.ModulePath()) <= 1 {
continue
}
// this provider may be disabled, but we can only get it's name from
// the ProviderName string
addr := pn.ProviderAddr()
parentAddr, ok := addr.Inherited()
if ok {
parent := pm[parentAddr.String()]
if parent != nil {
g.Connect(dag.BasicEdge(v, parent))
}
}
}
return nil
}
// PruneProviderTransformer removes any providers that are not actually used by
// anything, and provider proxies. This avoids the provider being initialized
// and configured. This both saves resources but also avoids errors since

View File

@ -434,6 +434,57 @@ func TestProviderConfigTransformer_grandparentProviders(t *testing.T) {
}
}
func TestProviderConfigTransformer_inheritOldSkool(t *testing.T) {
mod := testModuleInline(t, map[string]string{
"main.tf": `
provider "test" {
test_string = "config"
}
module "moda" {
source = "./moda"
}
`,
"moda/main.tf": `
resource "test_object" "a" {
}
`,
})
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }
g := Graph{Path: addrs.RootModuleInstance}
{
tf := &ConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Config: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := TransformProviders([]string{"registry.terraform.io/hashicorp/test"}, concrete, mod)
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
expected := `module.moda.test_object.a
provider["registry.terraform.io/hashicorp/test"]
provider["registry.terraform.io/hashicorp/test"]`
actual := strings.TrimSpace(g.String())
if actual != expected {
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
}
}
// Verify that configurations which are not recommended yet supported still work
func TestProviderConfigTransformer_nestedModuleProviders(t *testing.T) {
mod := testModuleInline(t, map[string]string{