2014-06-18 03:40:32 +02:00
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-31 08:46:46 +01:00
|
|
|
"github.com/hashicorp/hil/ast"
|
2014-06-18 03:40:32 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig(
|
|
|
|
t *testing.T,
|
|
|
|
c map[string]interface{},
|
|
|
|
vs map[string]string) *terraform.ResourceConfig {
|
|
|
|
rc, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vs) > 0 {
|
2015-01-15 07:01:42 +01:00
|
|
|
vars := make(map[string]ast.Variable)
|
2015-01-15 00:29:56 +01:00
|
|
|
for k, v := range vs {
|
2015-01-15 07:01:42 +01:00
|
|
|
vars[k] = ast.Variable{Value: v, Type: ast.TypeString}
|
2015-01-15 00:29:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rc.Interpolate(vars); err != nil {
|
2014-06-18 03:40:32 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.NewResourceConfig(rc)
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
func testResourceDiffStr(rd *terraform.InstanceDiff) string {
|
2014-06-18 03:40:32 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
crud := "UPDATE"
|
|
|
|
if rd.RequiresNew() {
|
|
|
|
crud = "CREATE"
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
"%s\n",
|
|
|
|
crud))
|
|
|
|
|
|
|
|
keyLen := 0
|
|
|
|
keys := make([]string, 0, len(rd.Attributes))
|
|
|
|
for key, _ := range rd.Attributes {
|
|
|
|
keys = append(keys, key)
|
|
|
|
if len(key) > keyLen {
|
|
|
|
keyLen = len(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, attrK := range keys {
|
|
|
|
attrDiff := rd.Attributes[attrK]
|
|
|
|
|
|
|
|
v := attrDiff.New
|
|
|
|
if attrDiff.NewComputed {
|
|
|
|
v = "<computed>"
|
|
|
|
}
|
2014-07-09 18:51:36 +02:00
|
|
|
if attrDiff.NewRemoved {
|
|
|
|
v = "<removed>"
|
|
|
|
}
|
2014-06-18 03:40:32 +02:00
|
|
|
|
|
|
|
newResource := ""
|
|
|
|
if attrDiff.RequiresNew {
|
|
|
|
newResource = " (forces new resource)"
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:49:30 +02:00
|
|
|
inOut := "IN "
|
|
|
|
if attrDiff.Type == terraform.DiffAttrOutput {
|
|
|
|
inOut = "OUT"
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:40:32 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
2014-06-23 21:49:30 +02:00
|
|
|
" %s %s:%s %#v => %#v%s\n",
|
|
|
|
inOut,
|
2014-06-18 03:40:32 +02:00
|
|
|
attrK,
|
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
|
|
|
attrDiff.Old,
|
|
|
|
v,
|
|
|
|
newResource))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|