Add a VariableSourceType for names .tfvars files

This new source type should be used for variables loaded from .tfvars files that were explicitly passed as command line arguments (e.g. -var-file=foo.tfvars)
This commit is contained in:
Sander van Harmelen 2018-11-05 18:08:05 +01:00
parent ca18d677a9
commit b62a22ab62
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 {
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
// a nice error message with good context.
diags = diags.Append(&hcl.Diagnostic{

View File

@ -9,7 +9,6 @@ import (
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
hcljson "github.com/hashicorp/hcl2/hcl/json"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/configs"
"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
// ending in .auto.tfvars.
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)
}
const defaultVarsFilenameJSON = DefaultVarsFilename + ".json"
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)
}
if infos, err := ioutil.ReadDir("."); err == nil {
@ -74,7 +73,7 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
if !isAutoVarFile(name) {
continue
}
moreDiags := m.addVarsFromFile(name, terraform.ValueFromFile, ret)
moreDiags := m.addVarsFromFile(name, terraform.ValueFromAutoFile, ret)
diags = diags.Append(moreDiags)
}
}
@ -106,7 +105,7 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
}
case "-var-file":
moreDiags := m.addVarsFromFile(rawFlag.Value, terraform.ValueFromFile, ret)
moreDiags := m.addVarsFromFile(rawFlag.Value, terraform.ValueFromNamedFile, ret)
diags = diags.Append(moreDiags)
default:

View File

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

View File

@ -32,10 +32,14 @@ const (
// e.g. the default value defined for a variable.
ValueFromConfig ValueSourceType = 'C'
// ValueFromFile indicates that a value came from a "values file", like
// a .tfvars file, either passed explicitly on the command line or
// implicitly loaded by naming convention.
ValueFromFile ValueSourceType = 'F'
// ValueFromAutoFile indicates that a value came from a "values file", like
// a .tfvars file, that was implicitly loaded by naming convention.
ValueFromAutoFile 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
// a CLI argument. The name of this argument is not recorded and so it must