Skip to content

Commit

Permalink
Sort terraform literals in ELB task
Browse files Browse the repository at this point in the history
Fixes #3578
  • Loading branch information
justinsb committed Oct 10, 2017
1 parent 35cf4a3 commit f3a41ab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,12 @@ func (_ *LoadBalancer) RenderTerraform(t *terraform.TerraformTarget, a, e, chang
for _, subnet := range e.Subnets {
tf.Subnets = append(tf.Subnets, subnet.TerraformLink())
}
terraform.SortLiterals(tf.Subnets)

for _, sg := range e.SecurityGroups {
tf.SecurityGroups = append(tf.SecurityGroups, sg.TerraformLink())
}
terraform.SortLiterals(tf.SecurityGroups)

for loadBalancerPort, listener := range e.Listeners {
loadBalancerPortInt, err := strconv.ParseInt(loadBalancerPort, 10, 64)
Expand Down
38 changes: 32 additions & 6 deletions upup/pkg/fi/cloudup/terraform/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package terraform
import (
"encoding/json"
"sort"

"github.com/golang/glog"
)

type Literal struct {
Expand Down Expand Up @@ -63,7 +65,8 @@ func (a byKey) Less(i, j int) bool {
return a[i].key < a[j].key
}

func sortLiterals(v []*Literal, dedup bool) ([]*Literal, error) {
// buildSortProxies maps a list of Literals to a list of literalWithJSON
func buildSortProxies(v []*Literal) ([]*literalWithJSON, error) {
var proxies []*literalWithJSON
for _, l := range v {
k, err := json.Marshal(l)
Expand All @@ -76,16 +79,39 @@ func sortLiterals(v []*Literal, dedup bool) ([]*Literal, error) {
})
}

return proxies, nil
}

// SortLiterals sorts a list of Literal, by key. It does so in-place
func SortLiterals(v []*Literal) {
proxies, err := buildSortProxies(v)
if err != nil {
// Very unexpected
glog.Fatalf("error processing terraform Literal: %v", err)
}

sort.Sort(byKey(proxies))

var sorted []*Literal
for i := range proxies {
v[i] = proxies[i].literal
}
}

func DedupLiterals(v []*Literal) ([]*Literal, error) {
proxies, err := buildSortProxies(v)
if err != nil {
return nil, err
}

sort.Sort(byKey(proxies))

var deduped []*Literal
for i, p := range proxies {
if dedup && i != 0 && proxies[i-1].key == proxies[i].key {
if i != 0 && proxies[i-1].key == proxies[i].key {
continue
}

sorted = append(sorted, p.literal)
deduped = append(deduped, p.literal)
}

return sorted, nil
return deduped, nil
}
8 changes: 4 additions & 4 deletions upup/pkg/fi/cloudup/terraform/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ func (t *TerraformTarget) Finish(taskMap map[string]fi.Task) error {
if v.Value != nil {
tfVar["value"] = v.Value
} else {
dedup := true
sorted, err := sortLiterals(v.ValueArray, dedup)
SortLiterals(v.ValueArray)
deduped, err := DedupLiterals(v.ValueArray)
if err != nil {
return fmt.Errorf("error sorting literals: %v", err)
return err
}
tfVar["value"] = sorted
tfVar["value"] = deduped
}
outputVariables[tfName] = tfVar
}
Expand Down

0 comments on commit f3a41ab

Please sign in to comment.