From 4a6dac39a547670e114adde1d6e5cf259fda7346 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 21 Sep 2020 15:56:07 -0400 Subject: [PATCH] Use Plan instead of Refresh Now that the planning process generates a refreshed state, and can handle changes between the configuration and the state which the refresh process cannot, we can use the plan for the refresh command. --- terraform/context.go | 44 +++++--------------------------------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index 6c671c1a07..56b5578b68 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -578,51 +578,17 @@ The -target option is not for routine use, and is provided only for exceptional } // Refresh goes through all the resources in the state and refreshes them -// to their latest state. This will update the state that this context -// works with, along with returning it. +// to their latest state. This is done by executing a plan, and retaining the +// state while discarding the change set. // -// Even in the case an error is returned, the state may be returned and -// will potentially be partially updated. +// In the case of an error, there is no state returned. func (c *Context) Refresh() (*states.State, tfdiags.Diagnostics) { - defer c.acquireRun("refresh")() - - // Copy our own state - c.state = c.state.DeepCopy() - - // Refresh builds a partial changeset as part of its work because it must - // create placeholder stubs for any resource instances that'll be created - // in subsequent plan so that provider configurations and data resources - // can interpolate from them. This plan is always thrown away after - // the operation completes, restoring any existing changeset. - oldChanges := c.changes - defer func() { c.changes = oldChanges }() - c.changes = plans.NewChanges() - - // Build the graph. - graph, diags := c.Graph(GraphTypeRefresh, nil) + p, diags := c.Plan() if diags.HasErrors() { return nil, diags } - // Do the walk - _, walkDiags := c.walk(graph, walkRefresh) - diags = diags.Append(walkDiags) - if walkDiags.HasErrors() { - return nil, diags - } - - // During our walk we will have created planned object placeholders in - // state for resource instances that are in configuration but not yet - // created. These were created only to allow expression evaluation to - // work properly in provider and data blocks during the walk and must - // now be discarded, since a subsequent plan walk is responsible for - // creating these "for real". - // TODO: Consolidate refresh and plan into a single walk, so that the - // refresh walk doesn't need to emulate various aspects of the plan - // walk in order to properly evaluate provider and data blocks. - c.state.SyncWrapper().RemovePlannedResourceInstanceObjects() - - return c.state, diags + return p.State, diags } // Stop stops the running task.