From b38e620b2f77ce7d987f516b4647a35ab232902d Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 21 Mar 2017 15:43:55 -0400 Subject: [PATCH] merge config.Terraform fields in config.Append Ensure that fields set in an earlier Terraform config block aren't removed by Append when encountering another Terraform block. When multiple blocks contain the same field, the later one still wins. --- config/append.go | 9 +++++++-- config/append_test.go | 24 ++++++++++++++++++++++++ config/config_terraform.go | 12 ++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/config/append.go b/config/append.go index a421df4a0..5f4e89eef 100644 --- a/config/append.go +++ b/config/append.go @@ -35,8 +35,13 @@ func Append(c1, c2 *Config) (*Config, error) { c.Atlas = c2.Atlas } - c.Terraform = c1.Terraform - if c2.Terraform != nil { + // merge Terraform blocks + if c1.Terraform != nil { + c.Terraform = c1.Terraform + if c2.Terraform != nil { + c.Terraform.Merge(c2.Terraform) + } + } else { c.Terraform = c2.Terraform } diff --git a/config/append_test.go b/config/append_test.go index aecb80e66..8c81ed91d 100644 --- a/config/append_test.go +++ b/config/append_test.go @@ -118,6 +118,30 @@ func TestAppend(t *testing.T) { }, false, }, + + { + &Config{ + Terraform: &Terraform{ + RequiredVersion: "A", + }, + }, + &Config{ + Terraform: &Terraform{ + Backend: &Backend{ + Type: "test", + }, + }, + }, + &Config{ + Terraform: &Terraform{ + RequiredVersion: "A", + Backend: &Backend{ + Type: "test", + }, + }, + }, + false, + }, } for i, tc := range cases { diff --git a/config/config_terraform.go b/config/config_terraform.go index 952d59cc4..a547cc798 100644 --- a/config/config_terraform.go +++ b/config/config_terraform.go @@ -47,6 +47,18 @@ func (t *Terraform) Validate() []error { return errs } +// Merge t with t2. +// Any conflicting fields are overwritten by t2. +func (t *Terraform) Merge(t2 *Terraform) { + if t2.RequiredVersion != "" { + t.RequiredVersion = t2.RequiredVersion + } + + if t2.Backend != nil { + t.Backend = t2.Backend + } +} + // Backend is the configuration for the "backend" to use with Terraform. // A backend is responsible for all major behavior of Terraform's core. // The abstraction layer above the core (the "backend") allows for behavior