diff --git a/terraform/diff.go b/terraform/diff.go index f05cff954..bef052827 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -14,19 +14,6 @@ type Diff struct { once sync.Once } -// ResourceDiff is the diff of a resource from some state to another. -type ResourceDiff struct { - Attributes map[string]*ResourceAttrDiff -} - -// ResourceAttrDiff is the diff of a single attribute of a resource. -type ResourceAttrDiff struct { - Old string // Old Value - New string // New Value - NewComputed bool // True if new value is computed (unknown currently) - RequiresNew bool // True if change requires new resource -} - func (d *Diff) init() { d.once.Do(func() { if d.Resources == nil { @@ -49,7 +36,15 @@ func (d *Diff) String() string { for _, name := range names { rdiff := d.Resources[name] - buf.WriteString(name + "\n") + crud := "UPDATE" + if rdiff.RequiresNew() { + crud = "CREATE" + } + + buf.WriteString(fmt.Sprintf( + "%s: %s\n", + crud, + name)) keyLen := 0 keys := make([]string, 0, len(rdiff.Attributes)) @@ -86,3 +81,28 @@ func (d *Diff) String() string { return buf.String() } + +// ResourceDiff is the diff of a resource from some state to another. +type ResourceDiff struct { + Attributes map[string]*ResourceAttrDiff +} + +// ResourceAttrDiff is the diff of a single attribute of a resource. +type ResourceAttrDiff struct { + Old string // Old Value + New string // New Value + NewComputed bool // True if new value is computed (unknown currently) + RequiresNew bool // True if change requires new resource +} + +// RequiresNew returns true if the diff requires the creation of a new +// resource (implying the destruction of the old). +func (d *ResourceDiff) RequiresNew() bool { + for _, rd := range d.Attributes { + if rd.RequiresNew { + return true + } + } + + return false +} diff --git a/terraform/diff_test.go b/terraform/diff_test.go index 6b325f7c0..7e7acda21 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -36,7 +36,7 @@ func TestDiff_String(t *testing.T) { } const diffStrBasic = ` -nodeA +CREATE: nodeA bar: "foo" => "" foo: "foo" => "bar" longfoo: "foo" => "bar" (forces new resource) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 424486be8..9422f3f60 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -391,16 +391,16 @@ func testTerraformProvider(tf *Terraform, n string) *terraformProvider { } const testTerraformDiffStr = ` -aws_instance.bar +UPDATE: aws_instance.bar foo: "" => "2" -aws_instance.foo +UPDATE: aws_instance.foo num: "" => "2" ` const testTerraformDiffComputedStr = ` -aws_instance.bar +UPDATE: aws_instance.bar foo: "" => "" -aws_instance.foo +UPDATE: aws_instance.foo id: "" => "" num: "" => "2" `