Merge pull request #12942 from hashicorp/jbardin/GH-12905

merge config.Terraform fields in config.Append
This commit is contained in:
James Bardin 2017-03-22 09:11:42 -04:00 committed by GitHub
commit 579e15c97c
5 changed files with 76 additions and 5 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,31 @@ func TestAppend(t *testing.T) {
},
false,
},
// appending configs merges terraform blocks
{
&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

View File

@ -32,9 +32,13 @@ func Merge(c1, c2 *Config) (*Config, error) {
c.Atlas = c2.Atlas
}
// Merge the Terraform configuration, which is a complete overwrite.
c.Terraform = c1.Terraform
if c2.Terraform != nil {
// Merge the Terraform configuration
if c1.Terraform != nil {
c.Terraform = c1.Terraform
if c2.Terraform != nil {
c.Terraform.Merge(c2.Terraform)
}
} else {
c.Terraform = c2.Terraform
}

View File

@ -434,6 +434,31 @@ func TestMerge(t *testing.T) {
},
false,
},
// terraform blocks are merged, not overwritten
{
&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 {