terraform: providers inherit properly for validation

This commit is contained in:
Mitchell Hashimoto 2014-09-24 21:38:23 -07:00
parent 6712ed15aa
commit c88614c585
5 changed files with 70 additions and 0 deletions

View File

@ -837,6 +837,37 @@ func (c *walkContext) validateWalkFn() depgraph.WalkFunc {
raw = sharedProvider.Config.RawConfig
}
// If we have a parent, then merge in the parent configurations
// properly so we "inherit" the configurations.
if sharedProvider.Parent != nil {
var rawMap map[string]interface{}
if raw != nil {
rawMap = raw.Raw
}
parent := sharedProvider.Parent
for parent != nil {
if parent.Config != nil {
if rawMap == nil {
rawMap = parent.Config.RawConfig.Raw
}
for k, v := range parent.Config.RawConfig.Raw {
rawMap[k] = v
}
}
parent = parent.Parent
}
// Update our configuration to be the merged result
var err error
raw, err = config.NewRawConfig(rawMap)
if err != nil {
return fmt.Errorf("Error merging configurations: %s", err)
}
}
rc := NewResourceConfig(raw)
for k, p := range sharedProvider.Providers {

View File

@ -128,6 +128,29 @@ func TestContextValidate_moduleBadResource(t *testing.T) {
}
}
func TestContextValidate_moduleProviderInherit(t *testing.T) {
m := testModule(t, "validate-module-pc-inherit")
p := testProvider("aws")
c := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
return nil, c.CheckSet([]string{"set"})
}
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) > 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_orphans(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-good")

View File

@ -40,6 +40,7 @@ type MockResourceProvider struct {
ResourcesReturn []ResourceType
ValidateCalled bool
ValidateConfig *ResourceConfig
ValidateFn func(*ResourceConfig) ([]string, []error)
ValidateReturnWarns []string
ValidateReturnErrors []error
ValidateResourceFn func(string, *ResourceConfig) ([]string, []error)
@ -56,6 +57,9 @@ func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
p.ValidateCalled = true
p.ValidateConfig = c
if p.ValidateFn != nil {
return p.ValidateFn(c)
}
return p.ValidateReturnWarns, p.ValidateReturnErrors
}

View File

@ -0,0 +1,3 @@
provider "aws" {}
resource "aws_instance" "foo" {}

View File

@ -0,0 +1,9 @@
module "child" {
source = "./child"
}
provider "aws" {
set = true
}
resource "aws_instance" "foo" {}