terraform: support MergeDiff for nil ResourceState

This commit is contained in:
Mitchell Hashimoto 2014-06-05 07:04:44 -07:00
parent 082e784566
commit 8358e7dc1f
2 changed files with 30 additions and 3 deletions

View File

@ -34,10 +34,16 @@ type ResourceState struct {
func (s *ResourceState) MergeDiff(
d map[string]*ResourceAttrDiff,
computedID string) ResourceState {
result := *s
var result ResourceState
if s != nil {
result = *s
}
result.Attributes = make(map[string]string)
for k, v := range s.Attributes {
result.Attributes[k] = v
if s != nil {
for k, v := range s.Attributes {
result.Attributes[k] = v
}
}
for k, diff := range d {
if diff.NewComputed {

View File

@ -41,3 +41,24 @@ func TestResourceState_MergeDiff(t *testing.T) {
t.Fatalf("bad: %#v", rs2.Attributes)
}
}
func TestResourceState_MergeDiff_nil(t *testing.T) {
var rs *ResourceState = nil
diff := map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "baz",
},
}
rs2 := rs.MergeDiff(diff, "computed")
expected := map[string]string{
"foo": "baz",
}
if !reflect.DeepEqual(expected, rs2.Attributes) {
t.Fatalf("bad: %#v", rs2.Attributes)
}
}