From b62a22ab62cee7ffea9036a232d4704bb1df4232 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Mon, 5 Nov 2018 18:08:05 +0100 Subject: [PATCH] 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) --- backend/unparsed_value.go | 2 +- command/meta_vars.go | 9 ++++----- terraform/semantics.go | 2 +- terraform/variables.go | 12 ++++++++---- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/backend/unparsed_value.go b/backend/unparsed_value.go index eec2998b6f..0c246f6dd9 100644 --- a/backend/unparsed_value.go +++ b/backend/unparsed_value.go @@ -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{ diff --git a/command/meta_vars.go b/command/meta_vars.go index 772dc867de..b11974cf72 100644 --- a/command/meta_vars.go +++ b/command/meta_vars.go @@ -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: diff --git a/terraform/semantics.go b/terraform/semantics.go index 6c583dc642..3f6189a981 100644 --- a/terraform/semantics.go +++ b/terraform/semantics.go @@ -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, diff --git a/terraform/variables.go b/terraform/variables.go index a4318f7aa8..e54e275639 100644 --- a/terraform/variables.go +++ b/terraform/variables.go @@ -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