mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: providers inherit properly for validation
This commit is contained in:
parent
6712ed15aa
commit
c88614c585
@ -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 {
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,3 @@
|
||||
provider "aws" {}
|
||||
|
||||
resource "aws_instance" "foo" {}
|
@ -0,0 +1,9 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
set = true
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" {}
|
Loading…
Reference in New Issue
Block a user