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.
This commit is contained in:
James Bardin 2017-03-21 15:43:55 -04:00
parent b7152c4405
commit b38e620b2f
3 changed files with 43 additions and 2 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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