From b45b6a5c20de0fa9b3363e0b9f99dc8fddce6f1d Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 7 Apr 2017 21:12:21 -0400 Subject: [PATCH] remove duplicates in Dependencies duplicate entries could end up in "depends_on" in the state, which could possible lead to erroneous state comparisons. Remove them when walking the graph, and remove existing duplicates when pruning the state. --- terraform/node_resource_abstract.go | 2 +- terraform/state.go | 5 ++++- terraform/util.go | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/terraform/node_resource_abstract.go b/terraform/node_resource_abstract.go index e4577e9dbf..50bb70792a 100644 --- a/terraform/node_resource_abstract.go +++ b/terraform/node_resource_abstract.go @@ -102,7 +102,7 @@ func (n *NodeAbstractResource) References() []string { } } - return result + return uniqueStrings(result) } // If we have state, that is our next source diff --git a/terraform/state.go b/terraform/state.go index 905ec3dcbd..78725e88db 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -1163,6 +1163,8 @@ func (m *ModuleState) prune() { delete(m.Outputs, k) } } + + m.Dependencies = uniqueStrings(m.Dependencies) } func (m *ModuleState) sort() { @@ -1526,8 +1528,9 @@ func (s *ResourceState) prune() { i-- } } - s.Deposed = s.Deposed[:n] + + s.Dependencies = uniqueStrings(s.Dependencies) } func (s *ResourceState) sort() { diff --git a/terraform/util.go b/terraform/util.go index e1d951c01a..f41f0d7d63 100644 --- a/terraform/util.go +++ b/terraform/util.go @@ -1,6 +1,7 @@ package terraform import ( + "sort" "strings" ) @@ -73,3 +74,20 @@ func strSliceContains(haystack []string, needle string) bool { } return false } + +// deduplicate a slice of strings +func uniqueStrings(s []string) []string { + if len(s) < 2 { + return s + } + + sort.Strings(s) + result := make([]string, 1, len(s)) + result[0] = s[0] + for i := 1; i < len(s); i++ { + if s[i] != result[len(result)-1] { + result = append(result, s[i]) + } + } + return result +}