github.com/opentofu/opentofu@v1.7.1/internal/tofu/context_refresh.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"log"
    10  
    11  	"github.com/opentofu/opentofu/internal/configs"
    12  	"github.com/opentofu/opentofu/internal/plans"
    13  	"github.com/opentofu/opentofu/internal/states"
    14  	"github.com/opentofu/opentofu/internal/tfdiags"
    15  )
    16  
    17  // Refresh is a vestigial operation that is equivalent to call to Plan and
    18  // then taking the prior state of the resulting plan.
    19  //
    20  // We retain this only as a measure of semi-backward-compatibility for
    21  // automation relying on the "tofu refresh" subcommand. The modern way
    22  // to get this effect is to create and then apply a plan in the refresh-only
    23  // mode.
    24  func (c *Context) Refresh(config *configs.Config, prevRunState *states.State, opts *PlanOpts) (*states.State, tfdiags.Diagnostics) {
    25  	if opts == nil {
    26  		// This fallback is only here for tests, not for real code.
    27  		opts = &PlanOpts{
    28  			Mode: plans.NormalMode,
    29  		}
    30  	}
    31  	if opts.Mode != plans.NormalMode {
    32  		panic("can only Refresh in the normal planning mode")
    33  	}
    34  
    35  	log.Printf("[DEBUG] Refresh is really just plan now, so creating a %s plan", opts.Mode)
    36  	p, diags := c.Plan(config, prevRunState, opts)
    37  	if diags.HasErrors() {
    38  		return nil, diags
    39  	}
    40  
    41  	return p.PriorState, diags
    42  }