diff --git a/terraform/state.go b/terraform/state.go index ba591449a..011a808fc 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -1,6 +1,8 @@ package terraform import ( + "bytes" + "fmt" "sync" "github.com/hashicorp/terraform/config" @@ -21,6 +23,21 @@ func (s *State) init() { }) } +func (s *State) String() string { + var buf bytes.Buffer + + for k, rs := range s.Resources { + buf.WriteString(fmt.Sprintf("%s:\n", k)) + buf.WriteString(fmt.Sprintf(" ID = %s\n", rs.ID)) + + for ak, av := range rs.Attributes { + buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av)) + } + } + + return buf.String() +} + // ResourceState holds the state of a resource that is used so that // a provider can find and manage an existing resource as well as for // storing attributes that are uesd to populate variables of child diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index a56330141..743736faf 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -207,7 +207,10 @@ func TestTerraformApply(t *testing.T) { tf := testTerraform(t, "apply-good") s := &State{} - d := &Diff{} + d, err := tf.Diff(s) + if err != nil { + t.Fatalf("err: %s", err) + } state, err := tf.Apply(s, d) if err != nil { @@ -217,6 +220,12 @@ func TestTerraformApply(t *testing.T) { if len(state.Resources) < 2 { t.Fatalf("bad: %#v", state.Resources) } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } } func TestTerraformDiff(t *testing.T) { @@ -456,6 +465,17 @@ func testTerraformProvider(tf *Terraform, n string) *terraformProvider { return nil } +const testTerraformApplyStr = ` +aws_instance.bar: + ID = foo + type = aws_instance + foo = bar +aws_instance.foo: + ID = foo + type = aws_instance + num = 2 +` + const testTerraformDiffStr = ` UPDATE: aws_instance.bar foo: "" => "2" diff --git a/terraform/test-fixtures/apply-good/main.tf b/terraform/test-fixtures/apply-good/main.tf index 94ed55478..ce5654441 100644 --- a/terraform/test-fixtures/apply-good/main.tf +++ b/terraform/test-fixtures/apply-good/main.tf @@ -3,5 +3,5 @@ resource "aws_instance" "foo" { } resource "aws_instance" "bar" { - foo = "${aws_instance.foo.num}" + foo = "bar" }