mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: test count = 1 variable access
This commit is contained in:
parent
221e40a3a9
commit
66c58788fe
@ -1498,10 +1498,15 @@ func (c *walkContext) computeResourceVariable(
|
||||
|
||||
r, ok := module.Resources[id]
|
||||
if !ok {
|
||||
return "", fmt.Errorf(
|
||||
"Resource '%s' not found for variable '%s'",
|
||||
id,
|
||||
v.FullKey())
|
||||
if v.Multi && v.Index == 0 {
|
||||
r, ok = module.Resources[v.ResourceId()]
|
||||
}
|
||||
if !ok {
|
||||
return "", fmt.Errorf(
|
||||
"Resource '%s' not found for variable '%s'",
|
||||
id,
|
||||
v.FullKey())
|
||||
}
|
||||
}
|
||||
|
||||
if r.Primary == nil {
|
||||
|
@ -2513,6 +2513,29 @@ func TestContextPlan_countZero(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_countOneIndex(t *testing.T) {
|
||||
m := testModule(t, "plan-count-one-index")
|
||||
p := testProvider("aws")
|
||||
p.DiffFn = testDiffFn
|
||||
ctx := testContext(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, err := ctx.Plan(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(plan.String())
|
||||
expected := strings.TrimSpace(testTerraformPlanCountOneIndexStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPlan_countDecreaseToOne(t *testing.T) {
|
||||
m := testModule(t, "plan-count-dec")
|
||||
p := testProvider("aws")
|
||||
|
@ -477,6 +477,21 @@ STATE:
|
||||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanCountOneIndexStr = `
|
||||
DIFF:
|
||||
|
||||
CREATE: aws_instance.bar
|
||||
foo: "" => "foo"
|
||||
type: "" => "aws_instance"
|
||||
CREATE: aws_instance.foo
|
||||
foo: "" => "foo"
|
||||
type: "" => "aws_instance"
|
||||
|
||||
STATE:
|
||||
|
||||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanCountZeroStr = `
|
||||
DIFF:
|
||||
|
||||
|
8
terraform/test-fixtures/plan-count-one-index/main.tf
Normal file
8
terraform/test-fixtures/plan-count-one-index/main.tf
Normal file
@ -0,0 +1,8 @@
|
||||
resource "aws_instance" "foo" {
|
||||
count = 1
|
||||
foo = "foo"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${aws_instance.foo.0.foo}"
|
||||
}
|
Loading…
Reference in New Issue
Block a user