terraform: start using the RawConfig interpolations

This commit is contained in:
Mitchell Hashimoto 2014-06-12 17:51:38 -07:00
parent 69841c22e6
commit fd39728cbb
5 changed files with 23 additions and 19 deletions

View File

@ -1,10 +1,5 @@
package terraform package terraform
// ComputedPlaceholder is the placeholder value for computed attributes.
// ResourceProviders can compare values to this during a diff to determine
// if it is just a placeholder.
const ComputedPlaceholder = "74D93920-ED26-11E3-AC10-0800200C9A66"
// ResourceProvider is an interface that must be implemented by any // ResourceProvider is an interface that must be implemented by any
// resource provider: the thing that creates and manages the resources in // resource provider: the thing that creates and manages the resources in
// a Terraform configuration. // a Terraform configuration.

View File

@ -2,6 +2,8 @@ package terraform
import ( import (
"sync" "sync"
"github.com/hashicorp/terraform/config"
) )
// State keeps track of a snapshot state-of-the-world that Terraform // State keeps track of a snapshot state-of-the-world that Terraform
@ -43,8 +45,7 @@ type ResourceState struct {
// won't be available until apply, the value is replaced with the // won't be available until apply, the value is replaced with the
// computeID. // computeID.
func (s *ResourceState) MergeDiff( func (s *ResourceState) MergeDiff(
d map[string]*ResourceAttrDiff, d map[string]*ResourceAttrDiff) *ResourceState {
computedID string) *ResourceState {
var result ResourceState var result ResourceState
if s != nil { if s != nil {
result = *s result = *s
@ -58,7 +59,7 @@ func (s *ResourceState) MergeDiff(
} }
for k, diff := range d { for k, diff := range d {
if diff.NewComputed { if diff.NewComputed {
result.Attributes[k] = computedID result.Attributes[k] = config.UnknownVariableValue
continue continue
} }

View File

@ -3,6 +3,8 @@ package terraform
import ( import (
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/terraform/config"
) )
func TestResourceState_MergeDiff(t *testing.T) { func TestResourceState_MergeDiff(t *testing.T) {
@ -29,12 +31,12 @@ func TestResourceState_MergeDiff(t *testing.T) {
}, },
} }
rs2 := rs.MergeDiff(diff, "what") rs2 := rs.MergeDiff(diff)
expected := map[string]string{ expected := map[string]string{
"foo": "baz", "foo": "baz",
"bar": "foo", "bar": "foo",
"baz": "what", "baz": config.UnknownVariableValue,
} }
if !reflect.DeepEqual(expected, rs2.Attributes) { if !reflect.DeepEqual(expected, rs2.Attributes) {
@ -52,7 +54,7 @@ func TestResourceState_MergeDiff_nil(t *testing.T) {
}, },
} }
rs2 := rs.MergeDiff(diff, "computed") rs2 := rs.MergeDiff(diff)
expected := map[string]string{ expected := map[string]string{
"foo": "baz", "foo": "baz",

View File

@ -144,11 +144,13 @@ func (t *Terraform) diffWalkFn(
rs = state.resources[r.Id()] rs = state.resources[r.Id()]
} }
if len(vars) > 0 { if len(vars) > 0 {
r = r.ReplaceVariables(vars) if err := r.RawConfig.Interpolate(vars); err != nil {
panic(fmt.Sprintf("Interpolate error: %s", err))
}
} }
l.RUnlock() l.RUnlock()
diff, err := p.Provider.Diff(rs, r.Config) diff, err := p.Provider.Diff(rs, r.RawConfig.Config())
if err != nil { if err != nil {
return err return err
} }
@ -166,7 +168,7 @@ func (t *Terraform) diffWalkFn(
result.Resources[r.Id()] = diff result.Resources[r.Id()] = diff
// Determine the new state and update variables // Determine the new state and update variables
rs = rs.MergeDiff(diff.Attributes, ComputedPlaceholder) rs = rs.MergeDiff(diff.Attributes)
for ak, av := range rs.Attributes { for ak, av := range rs.Attributes {
vars[fmt.Sprintf("%s.%s", r.Id(), ak)] = av vars[fmt.Sprintf("%s.%s", r.Id(), ak)] = av
} }
@ -179,7 +181,11 @@ func (t *terraformProvider) init(vars map[string]string) (err error) {
t.Once.Do(func() { t.Once.Do(func() {
var c map[string]interface{} var c map[string]interface{}
if t.Config != nil { if t.Config != nil {
c = t.Config.ReplaceVariables(vars).Config if err := t.Config.RawConfig.Interpolate(vars); err != nil {
panic(err)
}
c = t.Config.RawConfig.Config()
} }
err = t.Provider.Configure(c) err = t.Provider.Configure(c)

View File

@ -47,7 +47,7 @@ func TestNew(t *testing.T) {
} }
pc = testProviderConfig(tf, "aws_instance.foo") pc = testProviderConfig(tf, "aws_instance.foo")
if pc.Config["foo"].(string) != "bar" { if pc.RawConfig.Raw["foo"].(string) != "bar" {
t.Fatalf("bad: %#v", pc) t.Fatalf("bad: %#v", pc)
} }
} }
@ -114,11 +114,11 @@ func TestNew_providerConfigCache(t *testing.T) {
t.Fatalf("bad: %#v", pc) t.Fatalf("bad: %#v", pc)
} }
pc = testProviderConfig(tf, "aws_instance.foo") pc = testProviderConfig(tf, "aws_instance.foo")
if pc.Config["foo"].(string) != "bar" { if pc.RawConfig.Raw["foo"].(string) != "bar" {
t.Fatalf("bad: %#v", pc) t.Fatalf("bad: %#v", pc)
} }
pc = testProviderConfig(tf, "aws_elb.lb") pc = testProviderConfig(tf, "aws_elb.lb")
if pc.Config["foo"].(string) != "baz" { if pc.RawConfig.Raw["foo"].(string) != "baz" {
t.Fatalf("bad: %#v", pc) t.Fatalf("bad: %#v", pc)
} }
@ -299,7 +299,7 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
New: v.(string), New: v.(string),
} }
if strings.Contains(attrDiff.New, ComputedPlaceholder) { if strings.Contains(attrDiff.New, config.UnknownVariableValue) {
attrDiff.NewComputed = true attrDiff.NewComputed = true
} }