|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/hcl2/hcl" |
| 7 | + "github.com/hashicorp/terraform/configs" |
| 8 | + "github.com/hashicorp/terraform/terraform" |
| 9 | + "github.com/hashicorp/terraform/tfdiags" |
| 10 | +) |
| 11 | + |
| 12 | +// UnparsedVariableValue represents a variable value provided by the caller |
| 13 | +// whose parsing must be deferred until configuration is available. |
| 14 | +// |
| 15 | +// This exists to allow processing of variable-setting arguments (e.g. in the |
| 16 | +// command package) to be separated from parsing (in the backend package). |
| 17 | +type UnparsedVariableValue interface { |
| 18 | + // ParseVariableValue information in the provided variable configuration |
| 19 | + // to parse (if necessary) and return the variable value encapsulated in |
| 20 | + // the receiver. |
| 21 | + // |
| 22 | + // If error diagnostics are returned, the resulting value may be invalid |
| 23 | + // or incomplete. |
| 24 | + ParseVariableValue(mode configs.VariableParsingMode) (*terraform.InputValue, tfdiags.Diagnostics) |
| 25 | +} |
| 26 | + |
| 27 | +func ParseVariableValues(vv map[string]UnparsedVariableValue, decls map[string]*configs.Variable) (terraform.InputValues, tfdiags.Diagnostics) { |
| 28 | + var diags tfdiags.Diagnostics |
| 29 | + ret := make(terraform.InputValues, len(vv)) |
| 30 | + |
| 31 | + for name, rv := range vv { |
| 32 | + var mode configs.VariableParsingMode |
| 33 | + config, declared := decls[name] |
| 34 | + if declared { |
| 35 | + mode = config.ParsingMode |
| 36 | + } else { |
| 37 | + mode = configs.VariableParseLiteral |
| 38 | + } |
| 39 | + |
| 40 | + val, valDiags := rv.ParseVariableValue(mode) |
| 41 | + diags = diags.Append(valDiags) |
| 42 | + if valDiags.HasErrors() { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + if !declared { |
| 47 | + switch val.SourceType { |
| 48 | + case terraform.ValueFromConfig, terraform.ValueFromFile: |
| 49 | + // These source types have source ranges, so we can produce |
| 50 | + // a nice error message with good context. |
| 51 | + diags = diags.Append(&hcl.Diagnostic{ |
| 52 | + Severity: hcl.DiagError, |
| 53 | + Summary: "Value for undeclared variable", |
| 54 | + Detail: fmt.Sprintf("The root module does not declare a variable named %q. To use this value, add a \"variable\" block to the configuration.", name), |
| 55 | + Subject: val.SourceRange.ToHCL().Ptr(), |
| 56 | + }) |
| 57 | + case terraform.ValueFromEnvVar: |
| 58 | + // We allow and ignore undeclared names for environment |
| 59 | + // variables, because users will often set these globally |
| 60 | + // when they are used across many (but not necessarily all) |
| 61 | + // configurations. |
| 62 | + case terraform.ValueFromCLIArg: |
| 63 | + diags = diags.Append(tfdiags.Sourceless( |
| 64 | + tfdiags.Error, |
| 65 | + "Value for undeclared variable", |
| 66 | + fmt.Sprintf("A variable named %q was assigned on the command line, but the root module does not declare a variable of that name. To use this value, add a \"variable\" block to the configuration.", name), |
| 67 | + )) |
| 68 | + default: |
| 69 | + // For all other source types we are more vague, but other situations |
| 70 | + // don't generally crop up at this layer in practice. |
| 71 | + diags = diags.Append(tfdiags.Sourceless( |
| 72 | + tfdiags.Error, |
| 73 | + "Value for undeclared variable", |
| 74 | + fmt.Sprintf("A variable named %q was assigned a value, but the root module does not declare a variable of that name. To use this value, add a \"variable\" block to the configuration.", name), |
| 75 | + )) |
| 76 | + } |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + ret[name] = val |
| 81 | + } |
| 82 | + |
| 83 | + return ret, diags |
| 84 | +} |
0 commit comments