Commit Graph

22097 Commits

Author SHA1 Message Date
Martin Atkins
1761faa29c core: schema attach interfaces are not mutually-exclusive
It was incorrect to use a type switch to detect the optional schema
attachment interfaces, because they are not mutually-exclusive: resource
nodes implement both GraphNodeAttachResourceSchema and
GraphNodeAttachProvisionerSchema.

This fixes a number of test regressions around dependency analysis in
"provisioner" blocks.
2018-10-16 18:49:20 -07:00
Martin Atkins
4b085c9cac core: fix trace log output for LoadSchemas
Some of this code was derived from earlier code in AttachSchemaTransformer,
leading to this stale log message.
2018-10-16 18:49:20 -07:00
Martin Atkins
da2d91c0e4 core: Re-fix edge case with missing module state during evaluation
In #14526 we fixed a sticky edge-case where a resource with count = 0 set
won't create its containing module state on apply, and thus when another
expression refers to it we need to deal with that absense.

The original bug fixed by #14526 was actually a nil dereference panic in
this case. Our new HCL2-oriented expression evaluation codepath was, on
the other hand, correctly checking for the nil, but was not taking the
correct action in response to it, leading to the result being an
unexpected unknown value.

Here we replicate the fix to #14526 by behaving as if there are just no
instances present in this case. We achieve this in a slightly different
way here by just creating an empty ModuleState, but the effect is the
same as #14526.

This fixes TestContext2Apply_multiVarMissingState.
2018-10-16 18:49:20 -07:00
Martin Atkins
26f76dd222 core: When planning to destroy an orphan instance, nil it in state
While we're planning we must always update the state with the proposed new
data resulting from the plan. In this case, we must record that the
orphan instance doesn't exist at all in the proposed new state by storing
its state as nil.

This in turn allows references to the containing resource to evaluate
properly, using the new updated resource count. This fixes
TestContext2Apply_multiVarCountDec.

This also includes a number of changes to the test output of
TestContext2Apply_multiVarCountDec that make it easier to debug failures.
2018-10-16 18:49:20 -07:00
Martin Atkins
f7aa06726a core: Run AttachSchemaTransformer twice to catch provider nodes too
Both ProviderTransformer and ReferenceTransformer need schema information,
and so there's a chicken-and-egg problem here where previously the schemas
were not getting attached to provider nodes created during
ProviderTransformer.

As a stop-gap measure for now we'll just run AttachSchemaTransformer
twice, so we can catch any new nodes created during the provider
transforms.
2018-10-16 18:49:20 -07:00
Martin Atkins
88b5607a7a core: Fetch schemas during context construction
Previously we fetched schemas during the AttachSchemaTransformer,
potentially multiple times as that was re-run for each graph built. Now
we fetch the schemas just once during context construction, passing that
result into each of the graph builders.

This only addresses the schema accesses during graph construction. We're
still separately loading schemas during the main walk for evaluation
purposes. This will be addressed in a later commit.
2018-10-16 18:49:20 -07:00
Martin Atkins
c036613ed3 addrs: Helper function for parsing absolute provider config strings 2018-10-16 18:49:20 -07:00
James Bardin
65cd163365 core: provider alias inheritance
An aliased provider should not be automatically inherited, nor
implicitly instantiated in a module. This test should not have
previously passed.

Add a proxy provider block to the module and update the provider to
match the schema.
2018-10-16 18:49:20 -07:00
James Bardin
46ba98233a core: change more "count" variables to "num" 2018-10-16 18:49:20 -07:00
James Bardin
d217a9cbf8 core: fix NodePlannableResourceInstance data reads
The state after EvalReadDataDiff is no longer nil during plan, which
means that we can't use that as a proxy for requiring the diff.

Rather than exiting early to save the EvalWriteState and EvalWriteDiff
evaluations, continue normally regardless to ensure we have the latest
diff and state after the plan. This also aligns the data data source
handling with that of the managed resource.
2018-10-16 18:49:20 -07:00
Martin Atkins
bba9660dd4 core: context apply test output distinguish plan vs. apply errors 2018-10-16 18:49:20 -07:00
Martin Atkins
b9ed280182 core: Fix TestContext2Apply_destroyComputed
The Provider field in ResourceState is now required, whereas before it
could be omitted and have Terraform try to discover a fitting provider
configuration automatically.

The automatic behavior was a compatibility shim added in v0.11 to support
states from prior versions without an explicit migration, but for v0.12
we will have a migration to our new state format anyway and so we will
fix this up during that migration pass.
2018-10-16 18:49:20 -07:00
Martin Atkins
e22f1d5dce core: Don't try to run provisioners with nil state
If a resource has been removed altogether then we have nothing to
provision.
2018-10-16 18:49:20 -07:00
Martin Atkins
3e64311dc2 core: Update TestContext2Apply_multiVarComprehensive for new assumptions
This comprehensive test was covering a few different behaviors that are
intentionally different for v0.12:

- Applying the splat operator to a list of resource instances that haven't
  been created yet produces a list of unknown values rather than a single
  unknown list as before. This is important because it allows that list
  to be passed into length().

- Wrapping a splat expression in another round of brackets now produces
  a list of lists, whereas before we had a special case (for compatibility
  with prior to v0.10) that would flatten this away in the schema layer.
2018-10-16 18:49:20 -07:00
Martin Atkins
cdce0d7e27 core: if InstanceState has empty id after apply, instance is deleted
Previously we would just retain an empty InstanceState in this case, but
now that we must enumerate all of the available instances during
expression evaluation it's important that we be able to recognize
instances that have been deleted.
2018-10-16 18:49:20 -07:00
James Bardin
90893eb3f9 core: update test count vars to num 2018-10-16 18:49:20 -07:00
James Bardin
7edff454f2 core: update test fixtures 2018-10-16 18:49:20 -07:00
Martin Atkins
de7b834de7 core: Local and output values must reference destroy nodes too
Because we currently rely on the ReferenceTransformer to introduce the
necessary edges between local/output values and resource destroy nodes, we
must include the destroy phase of any resource we depend on in the
references of these.

This works in conjunction with the changes in the prior commit to restore
correct handling of dependencies for local and output values during
destroy.

With the current design, several seemingly-separate parts of the code must
all coincidentally agree with one another for destroy edges to be created
properly, which makes this code very hard to maintain. In future we should
refactor this so that ReferenceTransformer doesn't create edges for
destroy nodes at all, and have _all_ destroy edges (including
create_before_destroy) be dealt with in the single DestroyEdgeTransformer,
where they can be maintained and unit tested together.
2018-10-16 18:49:20 -07:00
Martin Atkins
6bbfbab93e core: Produce correct references for destroy nodes
Prior to the introduction of our "addrs" package, we represented destroy
nodes as a special kind of address string ending in ".destroy" or
".destroy-cbd".

Using references to resolve these dependencies is a strange idea to begin
with, since these are not user-visible addresses, but rather than refactor
that now we instead have these weird pseudo-address types ResourcePhase
and ResourceInstancePhase that correspond go those weird address suffixes,
thus restoring the prior behavior.

In future we should rework this so that destroy node edges are not handled
as references at all, and instead handled as part of
DestroyEdgeTransformer where there's better context for implementing this
logic and it can be maintained and tested in a single place.
2018-10-16 18:49:20 -07:00
Martin Atkins
c5bd659a0f core: Make context apply tests produce more useful failure output 2018-10-16 18:49:20 -07:00
Martin Atkins
3e55c4e72b Revert "destroy nodes can't be referenced directly"
This reverts commit e708d4ebe9b2f571183250ab79ac217ae9498fcc.
2018-10-16 18:49:20 -07:00
James Bardin
2f6787c9b2 core: update provisioner test fixtures
They need to conform to the test schema
2018-10-16 18:49:20 -07:00
James Bardin
ec8df26a80 prefer the existing instance ID in tests
The old testApplyFn would overwrite ID with "foo" in all cases there
wasn't a diff, which made the test fixtures harder to reason about. If
there's an ID, keep it the same.
2018-10-16 18:49:20 -07:00
James Bardin
1350ed8e3a core: update fixture to match schema 2018-10-16 18:49:20 -07:00
James Bardin
a5c3c454ab filter self references in NodeApplyableResourceIns
Don't save self-references in the state dependencies for a resource.
2018-10-16 18:49:20 -07:00
Martin Atkins
ea73922780 core: DestroyEdgeTransformer must use instance rather than resource addrs
The initial destroyer map is constructed using DestroyAddr(), which
returns resource instance addresses, but we were then going on to _use_
that map with resource addresses, which means the keys can't match when
indexed instances are being destroyed.

Now we'll use resource instance addresses in all cases.

This also includes some additional logging that was helpful in debugging
this issue.
2018-10-16 18:49:20 -07:00
Martin Atkins
aedbbc6207 core: Fix TestContext2Apply_outputOrphanModule
The adaptation of ModuleState.RemovedOutputs for the new config types
was incorrect because it took the absence of any output map as "nothing to
do", rather than "everything has been removed" as expected.

Now it treats a nil map like an empty map, detecting _all_ of the outputs
as having been removed if the output map is nil.
2018-10-16 18:49:20 -07:00
Martin Atkins
11bd07abb2 core: Have WritePlan be explicit that it's non-functional right now
This is temporarily broken until we implement the new plan file format,
since terraform.Plan is no longer serializable with gob. Rather than have
an error that seems like it needs immediate fixing, we'll be explicit
about it in the error message and focus our efforts on other test failures
for now, and return to implement the new file format later.
2018-10-16 18:49:20 -07:00
Martin Atkins
798adf3ce6 core: EvalSequence to handle EvalEarlyExitError
An earlier commit today reworked this to handle non-fatal errors, which
are returned "smuggled" as a special type of error to avoid changing the
EvalNode interface.

Unfortunately, that change then broke the _other_ special thing we smuggle
through the error return path: early exit.

Now we'll handle them both. This is not perfect because the early-exit
path causes us to discard any warnings we've already collected, but it's
more important that we bail early than retain warnings.
2018-10-16 18:49:20 -07:00
Martin Atkins
b6d0abef1d core: Only treat instance ref as resource ref if instance nonexistent
We previously added a special case for dealing with references to
instances in the plan graph where there are only resource nodes. However,
this was too general a fix and so it upset the handling of graphs where
instances _are_ present.

Now we'll do that fallback behavior only if there is no instance node in
the graph already, so the exact matching behavior will be used in graphs
where the instances are present.
2018-10-16 18:49:20 -07:00
Martin Atkins
68c8d83620 core: In TransformDestroyEdge, insert variable nodes before providers
The provider transforms now depend on analyzing references in order to
properly create provider edges, and so we need to now insert all of the
nodes that can have references and attach schemas before we run
TransformProviders.

This was done for the main graph builders in a previous commit, but as
usual we missed this surprising hidden graph builder that lives inside
a graph transformer. 🙄
2018-10-16 18:49:20 -07:00
Martin Atkins
bcadbb2ddb core: Update plan graph builder tests for new provider edges
Due to the need for schema in order to resolve references in expressions,
we now create additional provider dependency edges when a node refers to
an attribute from a resource.
2018-10-16 18:49:20 -07:00
James Bardin
d9239e1f48 use provisioner attr defined in schema 2018-10-16 18:49:20 -07:00
James Bardin
41dde5fafc missing attribute now caught after apply 2018-10-16 18:49:20 -07:00
James Bardin
36aca84572 update ConnInfo test to work with new schema
We can't handle dynamic attributes in the tests any longer
2018-10-16 18:49:20 -07:00
James Bardin
903852b8e2 skip unset values in conection blocks
There's no valid, null value for the connection strings, so we can skip
over any unset values from the schema.
2018-10-16 18:49:20 -07:00
James Bardin
6a896c8748 fix the provisioner schema and update tests
The provisioenr schema needed to have `Optional: true` for all the
attributes. Also add the `type` attr and update the tests to match.
2018-10-16 18:49:20 -07:00
James Bardin
e0e177374f don't hanlde "type" when parsing connection blocks
There's no provisioner schema yet, and we need to handle the type
dynamically during evaluation.
2018-10-16 18:49:20 -07:00
Martin Atkins
30d6a40329 core: Give import graph access to schema, variables, outputs, locals
During import we constrain provider configuration to allow only references
to variables, but since provider configurations in child modules might
refer to variables from the parent, we still need to include the module
variables, outputs and locals in the graph here and attach the provider
schemas.

In future a better check would be that the provider configuration doesn't
refer to anything that is currently unknown, but we'll save that for
another day.
2018-10-16 18:49:20 -07:00
Martin Atkins
fc8030d2ae core: use testProvider for TestContextImport_moduleProvider
This test depends on the Refresh callback that comes built in to a
provider returned from testProvider.
2018-10-16 18:49:20 -07:00
Martin Atkins
9c0335e22e core: More TRACE logging for the "terraform import" walk 2018-10-16 18:49:20 -07:00
Martin Atkins
7f3d7313d8 core: Update error message for non-existing "terraform import"
The previous wording of this message was a little awkward, and a little
confusing due to the mention of it being a non-existing "resource", when
elsewhere in our output we use that noun to refer to the configuration
construct rather than the remote object.

Here we rework it as a diagnostic message, and while here also include an
extra note about a common problem of using an id from a different region
than the provider is configured for, to help the user realize what is
wrong in that case.
2018-10-16 18:49:20 -07:00
Martin Atkins
3de6a3db82 core: Fix TestContextImport_collision
The previous commit rewrote this incorrectly because the fatal message
made it seem like it was failing when an error occurs, but an error is
actually expected here.

Also includes a more detailed error message for this case, consistent with
our new diagnostics style.
2018-10-16 18:49:20 -07:00
Martin Atkins
6bc702f09d core: Update context import tests for diagnostics, rather than errors
ctx.Import now returns tfdiags.Diagnostics rather than "error", so these
tests need to now expect that API for proper behavior.

Several of these tests are still failing for other reasons. That will be
addressed in subsequent commits.
2018-10-16 18:49:20 -07:00
Kristin Laemmert
605e23db6b various code review fixes 2018-10-16 18:49:20 -07:00
Kristin Laemmert
6463dd90e9 functions: TransposeFunc, SliceFunc 2018-10-16 18:49:20 -07:00
Kristin Laemmert
30671d85ad functions: MergeFunc 2018-10-16 18:49:20 -07:00
Kristin Laemmert
aecd7b2e62 functions: LookupFunc 2018-10-16 18:49:20 -07:00
Kristin Laemmert
4d8c398f8e functions: KeysFunc 2018-10-16 18:49:20 -07:00
Kristin Laemmert
21daabe680 functions: MapFunc 2018-10-16 18:49:20 -07:00