Skip to content

Commit

Permalink
re-add ModuleInstance -> Module conversion
Browse files Browse the repository at this point in the history
When working with a ConfigResource, the generalization of a
ModuleInstance to a Module was inadvertently dropped, and there was to
test coverage for that type of target.

Ensure we can target a specific module instance alone.
  • Loading branch information
jbardin committed Aug 10, 2020
1 parent bab64c4 commit cc766aa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
47 changes: 47 additions & 0 deletions terraform/context_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6110,3 +6110,50 @@ data "test_data_source" "foo" {}
}
}
}

func TestContext2Plan_targetedModuleInstance(t *testing.T) {
m := testModule(t, "plan-targeted")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
Targets: []addrs.Targetable{
addrs.RootModuleInstance.Child("mod", addrs.IntKey(0)),
},
})

plan, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatalf("unexpected errors: %s", diags.Err())
}
schema := p.GetSchemaReturn.ResourceTypes["aws_instance"]
ty := schema.ImpliedType()

if len(plan.Changes.Resources) != 1 {
t.Fatal("expected 1 changes, got", len(plan.Changes.Resources))
}

for _, res := range plan.Changes.Resources {
ric, err := res.Decode(ty)
if err != nil {
t.Fatal(err)
}

switch i := ric.Addr.String(); i {
case "module.mod[0].aws_instance.foo":
if res.Action != plans.Create {
t.Fatalf("resource %s should be created", i)
}
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
"num": cty.NumberIntVal(2),
"type": cty.StringVal("aws_instance"),
}), ric.After)
default:
t.Fatal("unknown instance:", i)
}
}
}
7 changes: 6 additions & 1 deletion terraform/testdata/plan-targeted/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ resource "aws_instance" "foo" {
}

resource "aws_instance" "bar" {
foo = "${aws_instance.foo.num}"
foo = aws_instance.foo.num
}

module "mod" {
source = "./mod"
count = 1
}
3 changes: 3 additions & 0 deletions terraform/testdata/plan-targeted/mod/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
num = "2"
}
2 changes: 2 additions & 0 deletions terraform/transform_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func (t *TargetsTransformer) nodeIsTarget(v dag.Vertex, targets []addrs.Targetab
targetAddr = target.ContainingResource().Config()
case addrs.AbsResource:
targetAddr = target.Config()
case addrs.ModuleInstance:
targetAddr = target.Module()
}
}

Expand Down

0 comments on commit cc766aa

Please sign in to comment.