terraform/states
Martin Atkins 9af67806fc core: Prune placeholder objects from state after refresh
Prior to our refactoring here, we were relying on a lucky coincidence for
correct behavior of the plan walk following a refresh in the same run:

- The refresh phase created placeholder objects in the state to represent
  any resource instance pending creation, to allow the interpolator to
  read attributes from them when evaluating "provider" and "data" blocks.
  In effect, the refresh walk is creating a partial plan that only covers
  creation actions, but was immediately discarding the actual diff entries
  and storing only the planned new state.

- It happened that objects pending creation showed up in state with an
  empty ID value, since that only gets assigned by the provider during
  apply.

- The Refresh function concluded by calling terraform.State.Prune, which
  deletes from the state any objects that have an empty ID value, which
  therefore prevented these temporary objects from surviving into the
  plan phase.

After refactoring, we no longer have this special ID field on instance
object state, and we instead rely on the Status field for tracking such
things. We also no longer have an explicit "prune" step on state, since
the state mutation methods themselves keep the structure pruned.

To address this, here we introduce a new instance object status "planned",
which is equivalent to having an empty ID value in the old world. We also
introduce a new method on states.SyncState that deletes from the state
any planned objects, which therefore replaces that portion of the old
State.prune operation just for this refresh use-case.

Finally, we are now expecting the expression evaluator to pull pending
objects from the planned changeset rather than from the state directly,
and so for correct results these placeholder resource creation changes
must also be reported in a throwaway changeset during the refresh walk.

The addition of states.ObjectPlanned also permits a previously-missing
safety check in the expression evaluator to prevent us from relying on the
incomplete value stored in state for a pending object, in the event that
some bug prevents the real pending object from being written into the
planned changeset.
2018-10-16 19:14:11 -07:00
..
statefile states: object Private data is []byte, not cty.Value 2018-10-16 19:11:09 -07:00
statemgr states/statemgr: don't panic if no state file is present on first write 2018-10-16 19:14:11 -07:00
doc.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
eachmode_string.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
instance_generation.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
instance_object.go core: Prune placeholder objects from state after refresh 2018-10-16 19:14:11 -07:00
instance_object_src.go states: object Private data is []byte, not cty.Value 2018-10-16 19:11:09 -07:00
module.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
objectstatus_string.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
output_value.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
resource.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
resource_test.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
state.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
state_deepcopy.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
state_equal.go states: New package with modern models for Terraform state 2018-10-16 18:49:20 -07:00
state_string.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
state_test.go terraform: Ugly huge change to weave in new State and Plan types 2018-10-16 19:11:09 -07:00
sync.go core: Prune placeholder objects from state after refresh 2018-10-16 19:14:11 -07:00