Merge pull request #26005 from hashicorp/alisdair/backend-config-schema-fix

command: Fix backend config schema validation
This commit is contained in:
Alisdair McDiarmid 2020-08-26 11:52:04 -04:00 committed by GitHub
commit c13eba9b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -866,10 +866,13 @@ func (c *InitCommand) backendConfigOverrideBody(flags rawFlags, schema *configsc
}
// Generate an HCL body schema for the backend block.
var bodySchema hcl.BodySchema
for name, attr := range schema.Attributes {
for name := range schema.Attributes {
// We intentionally ignore the `Required` attribute here
// because backend config override files can be partial. The
// goal is to make sure we're not loading a file with
// extraneous attributes or blocks.
bodySchema.Attributes = append(bodySchema.Attributes, hcl.AttributeSchema{
Name: name,
Required: attr.Required,
Name: name,
})
}
for name, block := range schema.BlockTypes {

View File

@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/internal/getproviders"
"github.com/hashicorp/terraform/internal/providercache"
@ -429,6 +430,30 @@ func TestInit_backendConfigFile(t *testing.T) {
t.Errorf("wrong config\ngot: %s\nwant: %s", got, want)
}
})
// simulate the local backend having a required field which is not
// specified in the override file
t.Run("required-argument", func(t *testing.T) {
c := &InitCommand{}
schema := &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"path": {
Type: cty.String,
Optional: true,
},
"workspace_dir": {
Type: cty.String,
Required: true,
},
},
}
flagConfigExtra := newRawFlags("-backend-config")
flagConfigExtra.Set("input.config")
_, diags := c.backendConfigOverrideBody(flagConfigExtra, schema)
if len(diags) != 0 {
t.Errorf("expected no diags, got: %s", diags.Err())
}
})
}
func TestInit_backendConfigFilePowershellConfusion(t *testing.T) {