Merge pull request #19288 from hashicorp/f-variable-sources

Add a VariableSourceType for names .tfvars files
This commit is contained in:
Sander van Harmelen 2018-11-05 20:22:47 +01:00 committed by GitHub
commit 37687b8d6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 11 deletions

View File

@ -45,7 +45,7 @@ func ParseVariableValues(vv map[string]UnparsedVariableValue, decls map[string]*
if !declared { if !declared {
switch val.SourceType { switch val.SourceType {
case terraform.ValueFromConfig, terraform.ValueFromFile: case terraform.ValueFromConfig, terraform.ValueFromAutoFile, terraform.ValueFromNamedFile:
// These source types have source ranges, so we can produce // These source types have source ranges, so we can produce
// a nice error message with good context. // a nice error message with good context.
diags = diags.Append(&hcl.Diagnostic{ diags = diags.Append(&hcl.Diagnostic{

View File

@ -9,7 +9,6 @@ import (
"github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax" "github.com/hashicorp/hcl2/hcl/hclsyntax"
hcljson "github.com/hashicorp/hcl2/hcl/json" hcljson "github.com/hashicorp/hcl2/hcl/json"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -59,12 +58,12 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
// (DefaultVarsFilename) along with the later-added search for all files // (DefaultVarsFilename) along with the later-added search for all files
// ending in .auto.tfvars. // ending in .auto.tfvars.
if _, err := os.Stat(DefaultVarsFilename); err == nil { if _, err := os.Stat(DefaultVarsFilename); err == nil {
moreDiags := m.addVarsFromFile(DefaultVarsFilename, terraform.ValueFromFile, ret) moreDiags := m.addVarsFromFile(DefaultVarsFilename, terraform.ValueFromAutoFile, ret)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
} }
const defaultVarsFilenameJSON = DefaultVarsFilename + ".json" const defaultVarsFilenameJSON = DefaultVarsFilename + ".json"
if _, err := os.Stat(defaultVarsFilenameJSON); err == nil { if _, err := os.Stat(defaultVarsFilenameJSON); err == nil {
moreDiags := m.addVarsFromFile(defaultVarsFilenameJSON, terraform.ValueFromFile, ret) moreDiags := m.addVarsFromFile(defaultVarsFilenameJSON, terraform.ValueFromAutoFile, ret)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
} }
if infos, err := ioutil.ReadDir("."); err == nil { if infos, err := ioutil.ReadDir("."); err == nil {
@ -74,7 +73,7 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
if !isAutoVarFile(name) { if !isAutoVarFile(name) {
continue continue
} }
moreDiags := m.addVarsFromFile(name, terraform.ValueFromFile, ret) moreDiags := m.addVarsFromFile(name, terraform.ValueFromAutoFile, ret)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
} }
} }
@ -106,7 +105,7 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
} }
case "-var-file": case "-var-file":
moreDiags := m.addVarsFromFile(rawFlag.Value, terraform.ValueFromFile, ret) moreDiags := m.addVarsFromFile(rawFlag.Value, terraform.ValueFromNamedFile, ret)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
default: default:

View File

@ -79,7 +79,7 @@ func checkInputVariables(vcs map[string]*configs.Variable, vs InputValues) tfdia
_, err := convert.Convert(val.Value, wantType) _, err := convert.Convert(val.Value, wantType)
if err != nil { if err != nil {
switch val.SourceType { switch val.SourceType {
case ValueFromConfig, ValueFromFile: case ValueFromConfig, ValueFromAutoFile, ValueFromNamedFile:
// We have source location information for these. // We have source location information for these.
diags = diags.Append(&hcl.Diagnostic{ diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,

View File

@ -32,10 +32,14 @@ const (
// e.g. the default value defined for a variable. // e.g. the default value defined for a variable.
ValueFromConfig ValueSourceType = 'C' ValueFromConfig ValueSourceType = 'C'
// ValueFromFile indicates that a value came from a "values file", like // ValueFromAutoFile indicates that a value came from a "values file", like
// a .tfvars file, either passed explicitly on the command line or // a .tfvars file, that was implicitly loaded by naming convention.
// implicitly loaded by naming convention. ValueFromAutoFile ValueSourceType = 'F'
ValueFromFile ValueSourceType = 'F'
// ValueFromNamedFile indicates that a value came from a named "values file",
// like a .tfvars file, that was passed explicitly on the command line (e.g.
// -var-file=foo.tfvars).
ValueFromNamedFile ValueSourceType = 'N'
// ValueFromCLIArg indicates that the value was provided directly in // ValueFromCLIArg indicates that the value was provided directly in
// a CLI argument. The name of this argument is not recorded and so it must // a CLI argument. The name of this argument is not recorded and so it must