2016-08-17 13:10:26 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2018-05-07 18:47:24 -05:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-11-20 14:17:57 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/convert"
|
|
|
|
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2016-08-17 13:10:26 -05:00
|
|
|
)
|
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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
|
|
|
|
}
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// ValueSourceType describes what broad category of source location provided
|
|
|
|
// a particular value.
|
|
|
|
type ValueSourceType rune
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
const (
|
|
|
|
// ValueFromUnknown is the zero value of ValueSourceType and is not valid.
|
|
|
|
ValueFromUnknown ValueSourceType = 0
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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'
|
2016-08-17 13:10:26 -05:00
|
|
|
|
2018-11-05 11:08:05 -06:00
|
|
|
// 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'
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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'
|
|
|
|
)
|
|
|
|
|
2018-05-07 18:47:24 -05:00
|
|
|
func (v *InputValue) GoString() string {
|
|
|
|
if (v.SourceRange != tfdiags.SourceRange{}) {
|
|
|
|
return fmt.Sprintf("&terraform.InputValue{Value: %#v, SourceType: %#v, SourceRange: %#v}", v.Value, v.SourceType, v.SourceRange)
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("&terraform.InputValue{Value: %#v, SourceType: %#v}", v.Value, v.SourceType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v ValueSourceType) GoString() string {
|
|
|
|
return fmt.Sprintf("terraform.%s", v)
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:17:23 -05:00
|
|
|
//go:generate go run golang.org/x/tools/cmd/stringer -type ValueSourceType
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// InputValues is a map of InputValue instances.
|
|
|
|
type InputValues map[string]*InputValue
|
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
// InputValuesFromCaller turns the given map of naked values into an
|
|
|
|
// InputValues that attributes each value to "a caller", using the source
|
|
|
|
// type ValueFromCaller. This is primarily useful for testing purposes.
|
|
|
|
//
|
|
|
|
// This should not be used as a general way to convert map[string]cty.Value
|
|
|
|
// into InputValues, since in most real cases we want to set a suitable
|
|
|
|
// other SourceType and possibly SourceRange value.
|
|
|
|
func InputValuesFromCaller(vals map[string]cty.Value) InputValues {
|
|
|
|
ret := make(InputValues, len(vals))
|
|
|
|
for k, v := range vals {
|
|
|
|
ret[k] = &InputValue{
|
|
|
|
Value: v,
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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
|
|
|
|
}
|
2016-08-17 13:10:26 -05:00
|
|
|
}
|
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-04-30 12:33:53 -05:00
|
|
|
return ret
|
|
|
|
}
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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
|
2016-08-17 13:10:26 -05:00
|
|
|
}
|
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-04-30 12:33:53 -05:00
|
|
|
return ret
|
|
|
|
}
|
2016-08-17 13:10:26 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
// 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
|
2016-08-17 13:10:26 -05:00
|
|
|
}
|
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-04-30 12:33:53 -05:00
|
|
|
ret[k] = &InputValue{
|
|
|
|
Value: c.Default,
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRangeFromHCL(c.DeclRange),
|
2016-08-17 13:10:26 -05:00
|
|
|
}
|
|
|
|
}
|
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-04-30 12:33:53 -05:00
|
|
|
return ret
|
2016-08-17 13:10:26 -05:00
|
|
|
}
|
2018-05-07 18:47:24 -05:00
|
|
|
|
|
|
|
// SameValues returns true if the given InputValues has the same values as
|
|
|
|
// the receiever, disregarding the source types and source ranges.
|
|
|
|
//
|
|
|
|
// Values are compared using the cty "RawEquals" method, which means that
|
|
|
|
// unknown values can be considered equal to one another if they are of the
|
|
|
|
// same type.
|
|
|
|
func (vv InputValues) SameValues(other InputValues) bool {
|
|
|
|
if len(vv) != len(other) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range vv {
|
|
|
|
ov, exists := other[k]
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !v.Value.RawEquals(ov.Value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasValues returns true if the reciever has the same values as in the given
|
|
|
|
// map, disregarding the source types and source ranges.
|
|
|
|
//
|
|
|
|
// Values are compared using the cty "RawEquals" method, which means that
|
|
|
|
// unknown values can be considered equal to one another if they are of the
|
|
|
|
// same type.
|
|
|
|
func (vv InputValues) HasValues(vals map[string]cty.Value) bool {
|
|
|
|
if len(vv) != len(vals) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range vv {
|
|
|
|
oVal, exists := vals[k]
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !v.Value.RawEquals(oVal) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identical returns true if the given InputValues has the same values,
|
|
|
|
// source types, and source ranges as the receiver.
|
|
|
|
//
|
|
|
|
// Values are compared using the cty "RawEquals" method, which means that
|
|
|
|
// unknown values can be considered equal to one another if they are of the
|
|
|
|
// same type.
|
|
|
|
//
|
|
|
|
// This method is primarily for testing. For most practical purposes, it's
|
|
|
|
// better to use SameValues or HasValues.
|
|
|
|
func (vv InputValues) Identical(other InputValues) bool {
|
|
|
|
if len(vv) != len(other) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range vv {
|
|
|
|
ov, exists := other[k]
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !v.Value.RawEquals(ov.Value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.SourceType != ov.SourceType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.SourceRange != ov.SourceRange {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2018-11-20 14:17:57 -06:00
|
|
|
|
core: Functional-style API for terraform.Context
Previously terraform.Context was built in an unfortunate way where all of
the data was provided up front in terraform.NewContext and then mutated
directly by subsequent operations. That made the data flow hard to follow,
commonly leading to bugs, and also meant that we were forced to take
various actions too early in terraform.NewContext, rather than waiting
until a more appropriate time during an operation.
This (enormous) commit changes terraform.Context so that its fields are
broadly just unchanging data about the execution context (current
workspace name, available plugins, etc) whereas the main data Terraform
works with arrives via individual method arguments and is returned in
return values.
Specifically, this means that terraform.Context no longer "has-a" config,
state, and "planned changes", instead holding on to those only temporarily
during an operation. The caller is responsible for propagating the outcome
of one step into the next step so that the data flow between operations is
actually visible.
However, since that's a change to the main entry points in the "terraform"
package, this commit also touches every file in the codebase which
interacted with those APIs. Most of the noise here is in updating tests
to take the same actions using the new API style, but this also affects
the main-code callers in the backends and in the command package.
My goal here was to refactor without changing observable behavior, but in
practice there are a couple externally-visible behavior variations here
that seemed okay in service of the broader goal:
- The "terraform graph" command is no longer hooked directly into the
core graph builders, because that's no longer part of the public API.
However, I did include a couple new Context functions whose contract
is to produce a UI-oriented graph, and _for now_ those continue to
return the physical graph we use for those operations. There's no
exported API for generating the "validate" and "eval" graphs, because
neither is particularly interesting in its own right, and so
"terraform graph" no longer supports those graph types.
- terraform.NewContext no longer has the responsibility for collecting
all of the provider schemas up front. Instead, we wait until we need
them. However, that means that some of our error messages now have a
slightly different shape due to unwinding through a differently-shaped
call stack. As of this commit we also end up reloading the schemas
multiple times in some cases, which is functionally acceptable but
likely represents a performance regression. I intend to rework this to
use caching, but I'm saving that for a later commit because this one is
big enough already.
The proximal reason for this change is to resolve the chicken/egg problem
whereby there was previously no single point where we could apply "moved"
statements to the previous run state before creating a plan. With this
change in place, we can now do that as part of Context.Plan, prior to
forking the input state into the three separate state artifacts we use
during planning.
However, this is at least the third project in a row where the previous
API design led to piling more functionality into terraform.NewContext and
then working around the incorrect order of operations that produces, so
I intend that by paying the cost/risk of this large diff now we can in
turn reduce the cost/risk of future projects that relate to our main
workflow actions.
2021-08-24 14:06:38 -05:00
|
|
|
func mergeDefaultInputVariableValues(setVals InputValues, rootVarsConfig map[string]*configs.Variable) InputValues {
|
|
|
|
var variables InputValues
|
|
|
|
|
|
|
|
// Default variables from the configuration seed our map.
|
|
|
|
variables = DefaultVariableValues(rootVarsConfig)
|
|
|
|
|
|
|
|
// Variables provided by the caller (from CLI, environment, etc) can
|
|
|
|
// override the defaults.
|
|
|
|
variables = variables.Override(setVals)
|
|
|
|
|
|
|
|
return variables
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:17:57 -06:00
|
|
|
// checkInputVariables ensures that variable values supplied at the UI conform
|
|
|
|
// to their corresponding declarations in configuration.
|
|
|
|
//
|
|
|
|
// The set of values is considered valid only if the returned diagnostics
|
|
|
|
// does not contain errors. A valid set of values may still produce warnings,
|
|
|
|
// which should be returned to the user.
|
|
|
|
func checkInputVariables(vcs map[string]*configs.Variable, vs InputValues) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
for name, vc := range vcs {
|
|
|
|
val, isSet := vs[name]
|
|
|
|
if !isSet {
|
|
|
|
// Always an error, since the caller should already have included
|
|
|
|
// default values from the configuration in the values map.
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Unassigned variable",
|
|
|
|
fmt.Sprintf("The input variable %q has not been assigned a value. This is a bug in Terraform; please report it in a GitHub issue.", name),
|
|
|
|
))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// A given value is valid if it can convert to the desired type.
|
2021-09-10 09:58:44 -05:00
|
|
|
_, err := convert.Convert(val.Value, vc.ConstraintType)
|
2018-11-20 14:17:57 -06:00
|
|
|
if err != nil {
|
|
|
|
switch val.SourceType {
|
|
|
|
case ValueFromConfig, ValueFromAutoFile, ValueFromNamedFile:
|
|
|
|
// We have source location information for these.
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid value for input variable",
|
|
|
|
Detail: fmt.Sprintf("The given value is not valid for variable %q: %s.", name, err),
|
|
|
|
Subject: val.SourceRange.ToHCL().Ptr(),
|
|
|
|
})
|
|
|
|
case ValueFromEnvVar:
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid value for input variable",
|
|
|
|
fmt.Sprintf("The environment variable TF_VAR_%s does not contain a valid value for variable %q: %s.", name, name, err),
|
|
|
|
))
|
|
|
|
case ValueFromCLIArg:
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid value for input variable",
|
|
|
|
fmt.Sprintf("The argument -var=\"%s=...\" does not contain a valid value for variable %q: %s.", name, name, err),
|
|
|
|
))
|
|
|
|
case ValueFromInput:
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid value for input variable",
|
|
|
|
fmt.Sprintf("The value entered for variable %q is not valid: %s.", name, err),
|
|
|
|
))
|
|
|
|
default:
|
|
|
|
// The above gets us good coverage for the situations users
|
|
|
|
// are likely to encounter with their own inputs. The other
|
|
|
|
// cases are generally implementation bugs, so we'll just
|
|
|
|
// use a generic error for these.
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid value for input variable",
|
|
|
|
fmt.Sprintf("The value provided for variable %q is not valid: %s.", name, err),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any variables that are assigned without being configured.
|
|
|
|
// This is always an implementation error in the caller, because we
|
|
|
|
// expect undefined variables to be caught during context construction
|
|
|
|
// where there is better context to report it well.
|
|
|
|
for name := range vs {
|
|
|
|
if _, defined := vcs[name]; !defined {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Value assigned to undeclared variable",
|
|
|
|
fmt.Sprintf("A value was assigned to an undeclared input variable %q.", name),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|