From 5643a7c28b82947ca08393c7a3b591ed84f530f9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Nov 2016 17:14:20 -0800 Subject: [PATCH] terraform: when returning a raw attribute value, use hil conversion Because we now rely on HIL to do the computed calculation, we must make sure the type is correct (TypeUnknown). Before, we'd just check for the UUID in the string. This changes all variable returns in the interpolater to run it through `hil.InterfaceToVariable` which handles this lookup for us. --- terraform/interpolate.go | 3 ++- terraform/interpolate_test.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/terraform/interpolate.go b/terraform/interpolate.go index 2bc74994f..3fb0b66a7 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -404,7 +404,8 @@ func (i *Interpolater) computeResourceVariable( } if attr, ok := r.Primary.Attributes[v.Field]; ok { - return &ast.Variable{Type: ast.TypeString, Value: attr}, nil + v, err := hil.InterfaceToVariable(attr) + return &v, err } // computed list or map attribute diff --git a/terraform/interpolate_test.go b/terraform/interpolate_test.go index 9b0925908..4eaf882bc 100644 --- a/terraform/interpolate_test.go +++ b/terraform/interpolate_test.go @@ -569,6 +569,44 @@ func TestInterpolator_resourceMultiAttributesComputed(t *testing.T) { }) } +func TestInterpolator_resourceAttributeComputed(t *testing.T) { + lock := new(sync.RWMutex) + // The state would never be written with an UnknownVariableValue in it, but + // it can/does exist that way in memory during the plan phase. + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_route53_zone.yada": &ResourceState{ + Type: "aws_route53_zone", + Primary: &InstanceState{ + ID: "z-abc123", + Attributes: map[string]string{ + "id": config.UnknownVariableValue, + }, + }, + }, + }, + }, + }, + } + i := &Interpolater{ + Module: testModule(t, "interpolate-multi-vars"), + StateLock: lock, + State: state, + } + + scope := &InterpolationScope{ + Path: rootModulePath, + } + + testInterpolate(t, i, scope, "aws_route53_zone.yada.id", ast.Variable{ + Value: config.UnknownVariableValue, + Type: ast.TypeUnknown, + }) +} + func TestInterpolater_selfVarWithoutResource(t *testing.T) { i := &Interpolater{}