Skip to content

Commit

Permalink
Add 'explicit configuration required' message when provider schema va…
Browse files Browse the repository at this point in the history
…lidation fails (#34595)
  • Loading branch information
liamcervante authored Jan 31, 2024
1 parent d9ccdfb commit 9c39e51
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
10 changes: 10 additions & 0 deletions internal/terraform/node_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func (n *NodeApplyableProvider) ConfigureProvider(ctx EvalContext, provider prov
configVal, configBody, evalDiags := ctx.EvaluateBlock(configBody, configSchema, nil, EvalDataForNoInstanceKey)
diags = diags.Append(evalDiags)
if evalDiags.HasErrors() {
if config == nil {
// The error messages from the above evaluation will be confusing
// if there isn't an explicit "provider" block in the configuration.
// Add some detail to the error message in this case.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid provider configuration",
fmt.Sprintf(providerConfigErr, n.Addr.Provider),
))
}
return diags
}

Expand Down
53 changes: 52 additions & 1 deletion internal/terraform/node_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"testing"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)

func TestNodeApplyableProviderExecute(t *testing.T) {
Expand Down Expand Up @@ -349,6 +350,19 @@ func TestNodeApplyableProvider_ConfigProvider(t *testing.T) {
}
return
}

// requiredProvider matches provider, but its attributes are required
// explicitly. This means we can simulate an earlier failure in the
// config validation.
requiredProvider := mockProviderWithConfigSchema(&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {
Type: cty.String,
Required: true,
},
},
})

ctx := &MockEvalContext{ProviderProvider: provider}
ctx.installSimpleEval()

Expand Down Expand Up @@ -410,6 +424,43 @@ func TestNodeApplyableProvider_ConfigProvider(t *testing.T) {
}
})

t.Run("missing schema-required config (no config at all)", func(t *testing.T) {
node := NodeApplyableProvider{
NodeAbstractProvider: &NodeAbstractProvider{
Addr: mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`),
},
}

diags := node.ConfigureProvider(ctx, requiredProvider, false)
if !diags.HasErrors() {
t.Fatal("missing expected error with nil config")
}
if !strings.Contains(diags.Err().Error(), "requires explicit configuration") {
t.Errorf("diagnostic is missing \"requires explicit configuration\" message: %s", diags.Err())
}
})

t.Run("missing schema-required config", func(t *testing.T) {
config := &configs.Provider{
Name: "test",
Config: hcl.EmptyBody(),
}
node := NodeApplyableProvider{
NodeAbstractProvider: &NodeAbstractProvider{
Addr: mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`),
Config: config,
},
}

diags := node.ConfigureProvider(ctx, requiredProvider, false)
if !diags.HasErrors() {
t.Fatal("missing expected error with invalid config")
}
if !strings.Contains(diags.Err().Error(), "The argument \"region\" is required, but was not set.") {
t.Errorf("wrong diagnostic: %s", diags.Err())
}
})

}

// This test is similar to TestNodeApplyableProvider_ConfigProvider, but tests responses from the providers.ConfigureProviderRequest
Expand Down

0 comments on commit 9c39e51

Please sign in to comment.