Commit Graph

8 Commits

Author SHA1 Message Date
Martin Atkins 3b49028b77 core: Static-validate resource references against schemas
In the initial move to HCL2 we started relying only on full expression
evaluation to catch attribute errors, but that's not sufficient for
resource attributes in practice because during validation we can't know
yet whether a resource reference evaluates to a single object or to a
list of objects (if count is set).

To address this, here we reinstate some static validation of resource
references by analyzing directly the reference objects, disregarding any
instance index if present, and produce errors if the remaining subsequent
traversal steps do not correspond to items within the resource type
schema.

This also allows us to produce some more specialized error messages for
certain situations. In particular, we can recognize a reference like
aws_instance.foo.count, which in 0.11 and prior was a weird special case
for determining the count value of a resource block, and offer a helpful
error showing the new length(aws_instance.foo) usage pattern.

This eventually delegates to the static traversal validation logic that
was added to the configschema package in a previous commit, which also
includes some specialized error messages that distinguish between
attributes and block types in the schema so that the errors relate more
directly to constructs the user can see in the configuration.

In future we could potentially move more of the checks from the dynamic
schema construction step to the static validation step, but resources
are the reference type that most needs this immediately due to the
ambiguity caused by the instance indexing syntax. We can safely refactor
other reference types to be statically validated in later releases.

This is verified by two pre-existing context validate tests which we
temporarily disabled during earlier work (now re-enabled) and also by a
new validate test aimed specifically at the special case for the "count"
attribute.
2018-11-26 08:25:03 -08:00
Martin Atkins ece8b6e734 repl: Fix tests
The test suite was not updated to deal with the new assumptions of the
HCL2-based expression evaluator, which requires configuration to exist
as well as state. Therefore we add a simple configuration fixture to have
it validate expressions against.

This also includes updates to expect the different error messages that the
new evaluator produces.
2018-10-16 19:14:11 -07:00
Martin Atkins 9bc153e809 repl: Make full logging optional in tests
Logs will be hidden unless verbose mode is enabled on the test run.
2018-10-16 19:14:11 -07:00
Martin Atkins 5ff35c1a9a repl: Make tests compile and execute without panics
There are still some errors left, because our expression evaluator now
does more validation than before and so we'll need to (in a subsequent
commit) actually use a fixture configuration for these tests so that the
validations will allow the expressions to be validated.
2018-10-16 19:14:11 -07:00
Martin Atkins f085af4ba6 command: update "terraform console" for HCL2
This now uses the HCL2 parser and evaluator APIs and evaluates in terms
of a new-style *lang.Scope, rather than the old terraform.Interpolator
type that is no longer functional.

The Context.Eval method used here behaves differently than the
Context.Interpolater method used previously: it performs a graph walk
to populate transient values such as input variables, local values, and
output values, and produces its scope in terms of the result of that
graph walk. Because of this, it is a lot more robust than the prior method
when asked to resolve references other than those that are persisted
in the state.
2018-10-16 18:46:46 -07:00
Martin Atkins 5659128126 repl: fix formatting of list and map values
The indent function was stripping out newlines, causing multi-element
lists and maps to be rendered incorrectly.

We were also not quoting strings in these nested structures, leading to
weird behavior if any expression punctuation or newlines were present in
these strings.

This part of Terraform will get a more serious overhaul as part of
switching to the new parser/interpreter implementation but this is a
tactical fix to make the results of this command more usable in the
short term.
2018-03-28 14:43:45 -07:00
Mitchell Hashimoto a633cdf95d
terraform: improve error messages to assist REPL 2016-11-13 23:17:04 -08:00
Mitchell Hashimoto d9c522173d
repl: package for TF REPL 2016-11-13 23:17:03 -08:00