terraform: refactor EvalValidateSelfref
The EvalValidateSelfref eval node implementation was removed in favor of a regular function.
This commit is contained in:
parent
468999b9b0
commit
a5cb780c87
|
@ -65,12 +65,7 @@ func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) (di
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
||||||
validateSelfRef := &EvalValidateSelfRef{
|
diags = diags.Append(validateSelfRef(addr.Resource, config.Config, providerSchema))
|
||||||
Addr: addr.Resource,
|
|
||||||
Config: config.Config,
|
|
||||||
ProviderSchema: &providerSchema,
|
|
||||||
}
|
|
||||||
diags = diags.Append(validateSelfRef.Eval(ctx))
|
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
@ -110,12 +105,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
||||||
validateSelfRef := &EvalValidateSelfRef{
|
diags = diags.Append(validateSelfRef(addr.Resource, config.Config, providerSchema))
|
||||||
Addr: addr.Resource,
|
|
||||||
Config: config.Config,
|
|
||||||
ProviderSchema: &providerSchema,
|
|
||||||
}
|
|
||||||
diags = diags.Append(validateSelfRef.Eval(ctx))
|
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,10 @@ import (
|
||||||
"github.com/hashicorp/terraform/tfdiags"
|
"github.com/hashicorp/terraform/tfdiags"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EvalValidateSelfRef is an EvalNode implementation that checks to ensure that
|
// validateSelfRef checks to ensure that expressions within a particular
|
||||||
// expressions within a particular referencable block do not reference that
|
// referencable block do not reference that same block.
|
||||||
// same block.
|
func validateSelfRef(addr addrs.Referenceable, config hcl.Body, providerSchema *ProviderSchema) tfdiags.Diagnostics {
|
||||||
type EvalValidateSelfRef struct {
|
|
||||||
Addr addrs.Referenceable
|
|
||||||
Config hcl.Body
|
|
||||||
ProviderSchema **ProviderSchema
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
|
|
||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
addr := n.Addr
|
|
||||||
|
|
||||||
addrStrs := make([]string, 0, 1)
|
addrStrs := make([]string, 0, 1)
|
||||||
addrStrs = append(addrStrs, addr.String())
|
addrStrs = append(addrStrs, addr.String())
|
||||||
|
@ -32,12 +24,11 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
|
||||||
addrStrs = append(addrStrs, tAddr.ContainingResource().String())
|
addrStrs = append(addrStrs, tAddr.ContainingResource().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.ProviderSchema == nil || *n.ProviderSchema == nil {
|
if providerSchema == nil {
|
||||||
diags = diags.Append(fmt.Errorf("provider schema unavailable while validating %s for self-references; this is a bug in Terraform and should be reported", addr))
|
diags = diags.Append(fmt.Errorf("provider schema unavailable while validating %s for self-references; this is a bug in Terraform and should be reported", addr))
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
||||||
providerSchema := *n.ProviderSchema
|
|
||||||
var schema *configschema.Block
|
var schema *configschema.Block
|
||||||
switch tAddr := addr.(type) {
|
switch tAddr := addr.(type) {
|
||||||
case addrs.Resource:
|
case addrs.Resource:
|
||||||
|
@ -51,7 +42,7 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
|
||||||
return diags
|
return diags
|
||||||
}
|
}
|
||||||
|
|
||||||
refs, _ := lang.ReferencesInBlock(n.Config, schema)
|
refs, _ := lang.ReferencesInBlock(config, schema)
|
||||||
for _, ref := range refs {
|
for _, ref := range refs {
|
||||||
for _, addrStr := range addrStrs {
|
for _, addrStr := range addrStrs {
|
||||||
if ref.Subject.String() == addrStr {
|
if ref.Subject.String() == addrStr {
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEvalValidateSelfRef(t *testing.T) {
|
func TestValidateSelfRef(t *testing.T) {
|
||||||
rAddr := addrs.Resource{
|
rAddr := addrs.Resource{
|
||||||
Mode: addrs.ManagedResourceMode,
|
Mode: addrs.ManagedResourceMode,
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
|
@ -92,12 +92,7 @@ func TestEvalValidateSelfRef(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
n := &EvalValidateSelfRef{
|
diags := validateSelfRef(test.Addr, body, ps)
|
||||||
Addr: test.Addr,
|
|
||||||
Config: body,
|
|
||||||
ProviderSchema: &ps,
|
|
||||||
}
|
|
||||||
diags := n.Eval(nil)
|
|
||||||
if diags.HasErrors() != test.Err {
|
if diags.HasErrors() != test.Err {
|
||||||
if test.Err {
|
if test.Err {
|
||||||
t.Errorf("unexpected success; want error")
|
t.Errorf("unexpected success; want error")
|
Loading…
Reference in New Issue