forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff_test.go
92 lines (76 loc) · 1.58 KB
/
diff_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package diff
import (
"bytes"
"fmt"
"sort"
"strings"
"testing"
"github.com/hashicorp/hil/ast"
"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 {
vars := make(map[string]ast.Variable)
for k, v := range vs {
vars[k] = ast.Variable{Value: v, Type: ast.TypeString}
}
if err := rc.Interpolate(vars); err != nil {
t.Fatalf("err: %s", err)
}
}
return terraform.NewResourceConfig(rc)
}
func testResourceDiffStr(rd *terraform.InstanceDiff) string {
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>"
}
if attrDiff.NewRemoved {
v = "<removed>"
}
newResource := ""
if attrDiff.RequiresNew {
newResource = " (forces new resource)"
}
inOut := "IN "
if attrDiff.Type == terraform.DiffAttrOutput {
inOut = "OUT"
}
buf.WriteString(fmt.Sprintf(
" %s %s:%s %#v => %#v%s\n",
inOut,
attrK,
strings.Repeat(" ", keyLen-len(attrK)),
attrDiff.Old,
v,
newResource))
}
return buf.String()
}