opentofu/terraform/variables.go
Martin Atkins c937c06a03 terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.

The three main goals here are:
- Use the configuration models from the "configs" package instead of the
  older models in the "config" package, which is now deprecated and
  preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
  new "lang" package, instead of the Interpolator type and related
  functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
  rather than hand-constructed strings. This is not critical to support
  the above, but was a big help during the implementation of these other
  points since it made it much more explicit what kind of address is
  expected in each context.

Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.

I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-10-16 18:46:46 -07:00

109 lines
3.4 KiB
Go

package terraform
import (
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/tfdiags"
"github.com/zclconf/go-cty/cty"
)
// InputValue represents a value for a variable in the root module, provided
// as part of the definition of an operation.
type InputValue struct {
Value cty.Value
SourceType ValueSourceType
// SourceRange provides source location information for values whose
// SourceType is either ValueFromConfig or ValueFromFile. It is not
// populated for other source types, and so should not be used.
SourceRange tfdiags.SourceRange
}
// ValueSourceType describes what broad category of source location provided
// a particular value.
type ValueSourceType rune
const (
// ValueFromUnknown is the zero value of ValueSourceType and is not valid.
ValueFromUnknown ValueSourceType = 0
// ValueFromConfig indicates that a value came from a .tf or .tf.json file,
// 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'
// ValueFromCLIArg indicates that the value was provided directly in
// a CLI argument. The name of this argument is not recorded and so it must
// be inferred from context.
ValueFromCLIArg ValueSourceType = 'A'
// ValueFromEnvVar indicates that the value was provided via an environment
// variable. The name of the variable is not recorded and so it must be
// inferred from context.
ValueFromEnvVar ValueSourceType = 'E'
// ValueFromInput indicates that the value was provided at an interactive
// input prompt.
ValueFromInput ValueSourceType = 'I'
// ValueFromPlan indicates that the value was retrieved from a stored plan.
ValueFromPlan ValueSourceType = 'P'
// ValueFromCaller indicates that the value was explicitly overridden by
// a caller to Context.SetVariable after the context was constructed.
ValueFromCaller ValueSourceType = 'S'
)
//go:generate stringer -type ValueSourceType
// InputValues is a map of InputValue instances.
type InputValues map[string]*InputValue
// Override merges the given value maps with the receiver, overriding any
// conflicting keys so that the latest definition wins.
func (vv InputValues) Override(others ...InputValues) InputValues {
// FIXME: This should check to see if any of the values are maps and
// merge them if so, in order to preserve the behavior from prior to
// Terraform 0.12.
ret := make(InputValues)
for k, v := range vv {
ret[k] = v
}
for _, other := range others {
for k, v := range other {
ret[k] = v
}
}
return ret
}
// JustValues returns a map that just includes the values, discarding the
// source information.
func (vv InputValues) JustValues() map[string]cty.Value {
ret := make(map[string]cty.Value, len(vv))
for k, v := range vv {
ret[k] = v.Value
}
return ret
}
// DefaultVariableValues returns an InputValues map representing the default
// values specified for variables in the given configuration map.
func DefaultVariableValues(configs map[string]*configs.Variable) InputValues {
ret := make(InputValues)
for k, c := range configs {
if c.Default == cty.NilVal {
continue
}
ret[k] = &InputValue{
Value: c.Default,
SourceType: ValueFromConfig,
SourceRange: tfdiags.SourceRangeFromHCL(c.DeclRange),
}
}
return ret
}