2015-02-03 03:43:18 -06:00
|
|
|
package terraform
|
|
|
|
|
2015-02-08 19:58:02 -06:00
|
|
|
import (
|
|
|
|
"log"
|
2018-05-29 13:47:49 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2015-02-08 19:58:02 -06:00
|
|
|
)
|
|
|
|
|
2015-02-03 03:43:18 -06:00
|
|
|
// EvalNode is the interface that must be implemented by graph nodes to
|
|
|
|
// evaluate/execute.
|
|
|
|
type EvalNode interface {
|
2015-02-03 18:06:12 -06:00
|
|
|
// Eval evaluates this node with the given context. The second parameter
|
|
|
|
// are the argument values. These will match in order and 1-1 with the
|
|
|
|
// results of the Args() return value.
|
2015-02-14 00:58:41 -06:00
|
|
|
Eval(EvalContext) (interface{}, error)
|
2015-02-03 03:43:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable is the interface that graph nodes must implement
|
|
|
|
// to enable valuation.
|
|
|
|
type GraphNodeEvalable interface {
|
|
|
|
EvalTree() EvalNode
|
|
|
|
}
|
2015-02-04 10:30:53 -06:00
|
|
|
|
2015-02-11 15:43:07 -06:00
|
|
|
// EvalEarlyExitError is a special error return value that can be returned
|
|
|
|
// by eval nodes that does an early exit.
|
|
|
|
type EvalEarlyExitError struct{}
|
|
|
|
|
|
|
|
func (EvalEarlyExitError) Error() string { return "early exit" }
|
|
|
|
|
2015-02-04 10:30:53 -06:00
|
|
|
// Eval evaluates the given EvalNode with the given context, properly
|
|
|
|
// evaluating all args in the correct order.
|
|
|
|
func Eval(n EvalNode, ctx EvalContext) (interface{}, error) {
|
2015-02-13 11:05:09 -06:00
|
|
|
// Call the lower level eval which doesn't understand early exit,
|
|
|
|
// and if we early exit, it isn't an error.
|
2015-02-13 11:49:29 -06:00
|
|
|
result, err := EvalRaw(n, ctx)
|
2015-02-13 11:05:09 -06:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(EvalEarlyExitError); ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:49:29 -06:00
|
|
|
// EvalRaw is like Eval except that it returns all errors, even if they
|
|
|
|
// signal something normal such as EvalEarlyExitError.
|
|
|
|
func EvalRaw(n EvalNode, ctx EvalContext) (interface{}, error) {
|
2015-02-16 21:04:05 -06:00
|
|
|
path := "unknown"
|
|
|
|
if ctx != nil {
|
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
|
|
|
path = ctx.Path().String()
|
|
|
|
}
|
|
|
|
if path == "" {
|
|
|
|
path = "<root>"
|
2015-02-16 21:04:05 -06:00
|
|
|
}
|
|
|
|
|
2017-08-14 04:43:45 -05:00
|
|
|
log.Printf("[TRACE] %s: eval: %T", path, n)
|
2015-02-14 00:58:41 -06:00
|
|
|
output, err := n.Eval(ctx)
|
2015-02-08 19:58:02 -06:00
|
|
|
if err != nil {
|
2018-05-29 13:47:49 -05:00
|
|
|
switch err.(type) {
|
|
|
|
case EvalEarlyExitError:
|
|
|
|
log.Printf("[TRACE] %s: eval: %T, early exit err: %s", path, n, err)
|
|
|
|
case tfdiags.NonFatalError:
|
|
|
|
log.Printf("[WARN] %s: eval: %T, non-fatal err: %s", path, n, err)
|
|
|
|
default:
|
2016-02-24 09:58:33 -06:00
|
|
|
log.Printf("[ERROR] %s: eval: %T, err: %s", path, n, err)
|
|
|
|
}
|
2015-02-08 19:58:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return output, err
|
2015-02-04 10:30:53 -06:00
|
|
|
}
|