terraform: refactor EvalValidateSelfref

The EvalValidateSelfref eval node implementation was removed in favor of a regular function.
This commit is contained in:
Kristin Laemmert 2020-12-08 10:52:39 -05:00
parent 468999b9b0
commit a5cb780c87
3 changed files with 9 additions and 33 deletions

View File

@ -65,12 +65,7 @@ func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) (di
return diags
}
validateSelfRef := &EvalValidateSelfRef{
Addr: addr.Resource,
Config: config.Config,
ProviderSchema: &providerSchema,
}
diags = diags.Append(validateSelfRef.Eval(ctx))
diags = diags.Append(validateSelfRef(addr.Resource, config.Config, providerSchema))
if diags.HasErrors() {
return diags
}
@ -110,12 +105,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
return diags
}
validateSelfRef := &EvalValidateSelfRef{
Addr: addr.Resource,
Config: config.Config,
ProviderSchema: &providerSchema,
}
diags = diags.Append(validateSelfRef.Eval(ctx))
diags = diags.Append(validateSelfRef(addr.Resource, config.Config, providerSchema))
if diags.HasErrors() {
return diags
}

View File

@ -11,18 +11,10 @@ import (
"github.com/hashicorp/terraform/tfdiags"
)
// EvalValidateSelfRef is an EvalNode implementation that checks to ensure that
// expressions within a particular referencable block do not reference that
// same block.
type EvalValidateSelfRef struct {
Addr addrs.Referenceable
Config hcl.Body
ProviderSchema **ProviderSchema
}
func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
// validateSelfRef checks to ensure that expressions within a particular
// referencable block do not reference that same block.
func validateSelfRef(addr addrs.Referenceable, config hcl.Body, providerSchema *ProviderSchema) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
addr := n.Addr
addrStrs := make([]string, 0, 1)
addrStrs = append(addrStrs, addr.String())
@ -32,12 +24,11 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
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))
return diags
}
providerSchema := *n.ProviderSchema
var schema *configschema.Block
switch tAddr := addr.(type) {
case addrs.Resource:
@ -51,7 +42,7 @@ func (n *EvalValidateSelfRef) Eval(ctx EvalContext) tfdiags.Diagnostics {
return diags
}
refs, _ := lang.ReferencesInBlock(n.Config, schema)
refs, _ := lang.ReferencesInBlock(config, schema)
for _, ref := range refs {
for _, addrStr := range addrStrs {
if ref.Subject.String() == addrStr {

View File

@ -12,7 +12,7 @@ import (
"github.com/zclconf/go-cty/cty"
)
func TestEvalValidateSelfRef(t *testing.T) {
func TestValidateSelfRef(t *testing.T) {
rAddr := addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
@ -92,12 +92,7 @@ func TestEvalValidateSelfRef(t *testing.T) {
},
}
n := &EvalValidateSelfRef{
Addr: test.Addr,
Config: body,
ProviderSchema: &ps,
}
diags := n.Eval(nil)
diags := validateSelfRef(test.Addr, body, ps)
if diags.HasErrors() != test.Err {
if test.Err {
t.Errorf("unexpected success; want error")