Skip to content

Commit

Permalink
remove import transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Jun 20, 2022
1 parent c002bab commit 77cca0a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 117 deletions.
2 changes: 1 addition & 1 deletion internal/terraform/graph_builder_plan.go
Expand Up @@ -128,7 +128,7 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {

// We also need nodes for any deposed instance objects present in the
// state, so we can plan to destroy them. (During plan this will
// intentionally skips creating nodes for _current_ objects, since
// intentionally skip creating nodes for _current_ objects, since
// ConfigTransformer created nodes that will do that during
// DynamicExpand.)
&StateTransformer{
Expand Down
Expand Up @@ -5,66 +5,11 @@ import (
"log"

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)

// ImportStateTransformer is a GraphTransformer that adds nodes to the
// graph to represent the imports we want to do for resources.
type ImportStateTransformer struct {
Targets []*ImportTarget
Config *configs.Config
skip bool
}

func (t *ImportStateTransformer) Transform(g *Graph) error {
if t.skip {
return nil
}

for _, target := range t.Targets {
// This is only likely to happen in misconfigured tests
if t.Config == nil {
return fmt.Errorf("cannot import into an empty configuration")
}

// Get the module config
modCfg := t.Config.Descendent(target.Addr.Module.Module())
if modCfg == nil {
return fmt.Errorf("module %s not found", target.Addr.Module.Module())
}

providerAddr := addrs.AbsProviderConfig{
Module: target.Addr.Module.Module(),
}

// Try to find the resource config
rsCfg := modCfg.Module.ResourceByAddr(target.Addr.Resource.Resource)
if rsCfg != nil {
// Get the provider FQN for the resource from the resource configuration
providerAddr.Provider = rsCfg.Provider

// Get the alias from the resource's provider local config
providerAddr.Alias = rsCfg.ProviderConfigAddr().Alias
} else {
// Resource has no matching config, so use an implied provider
// based on the resource type
rsProviderType := target.Addr.Resource.Resource.ImpliedProvider()
providerAddr.Provider = modCfg.Module.ImpliedProviderForUnqualifiedType(rsProviderType)
}

node := &graphNodeImportState{
Addr: target.Addr,
ID: target.ID,
ProviderAddr: providerAddr,
}
g.Add(node)
}
return nil
}

type graphNodeImportState struct {
Addr addrs.AbsResourceInstance // Addr is the resource address to import into
ID string // ID is the ID to import as
Expand Down
61 changes: 0 additions & 61 deletions internal/terraform/transform_provider_test.go
Expand Up @@ -49,58 +49,6 @@ func TestProviderTransformer(t *testing.T) {
}
}

func TestProviderTransformer_ImportModuleChild(t *testing.T) {
mod := testModule(t, "import-module")

g := testProviderTransformerGraph(t, mod)

{
tf := &ImportStateTransformer{
Config: mod,
Targets: []*ImportTarget{
&ImportTarget{
Addr: addrs.RootModuleInstance.
Child("child", addrs.NoKey).
ResourceInstance(
addrs.ManagedResourceMode,
"aws_instance",
"foo",
addrs.NoKey,
),
ID: "bar",
},
},
}

if err := tf.Transform(g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after ImportStateTransformer:\n%s", g.String())
}

{
tf := &MissingProviderTransformer{}
if err := tf.Transform(g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after MissingProviderTransformer:\n%s", g.String())
}

{
tf := &ProviderTransformer{}
if err := tf.Transform(g); err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("graph after ProviderTransformer:\n%s", g.String())
}

actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformImportModuleChildStr)
if actual != expected {
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
}
}

// Test providers with FQNs that do not match the typeName
func TestProviderTransformer_fqns(t *testing.T) {
for _, mod := range []string{"fqns", "fqns-module"} {
Expand Down Expand Up @@ -505,12 +453,3 @@ module.child.module.grandchild.aws_instance.baz
provider["registry.terraform.io/hashicorp/aws"].foo
provider["registry.terraform.io/hashicorp/aws"].foo
`

const testTransformImportModuleChildStr = `
module.child.aws_instance.foo
provider["registry.terraform.io/hashicorp/aws"]
module.child.aws_instance.foo (import id "bar")
provider["registry.terraform.io/hashicorp/aws"]
module.child.module.nested.aws_instance.foo
provider["registry.terraform.io/hashicorp/aws"]
provider["registry.terraform.io/hashicorp/aws"]`

0 comments on commit 77cca0a

Please sign in to comment.