terraform: support MergeDiff for nil ResourceState
This commit is contained in:
parent
082e784566
commit
8358e7dc1f
|
@ -34,10 +34,16 @@ type ResourceState struct {
|
||||||
func (s *ResourceState) MergeDiff(
|
func (s *ResourceState) MergeDiff(
|
||||||
d map[string]*ResourceAttrDiff,
|
d map[string]*ResourceAttrDiff,
|
||||||
computedID string) ResourceState {
|
computedID string) ResourceState {
|
||||||
result := *s
|
var result ResourceState
|
||||||
|
if s != nil {
|
||||||
|
result = *s
|
||||||
|
}
|
||||||
|
|
||||||
result.Attributes = make(map[string]string)
|
result.Attributes = make(map[string]string)
|
||||||
for k, v := range s.Attributes {
|
if s != nil {
|
||||||
result.Attributes[k] = v
|
for k, v := range s.Attributes {
|
||||||
|
result.Attributes[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for k, diff := range d {
|
for k, diff := range d {
|
||||||
if diff.NewComputed {
|
if diff.NewComputed {
|
||||||
|
|
|
@ -41,3 +41,24 @@ func TestResourceState_MergeDiff(t *testing.T) {
|
||||||
t.Fatalf("bad: %#v", rs2.Attributes)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue