kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/terraform/context_refresh.go (about)

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