github.com/opentofu/opentofu@v1.7.1/internal/tofu/update_state_hook.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  // updateStateHook calls the PostStateUpdate hook with the current state.
     9  func updateStateHook(ctx EvalContext) error {
    10  	// In principle we could grab the lock here just long enough to take a
    11  	// deep copy and then pass that to our hooks below, but we'll instead
    12  	// hold the hook for the duration to avoid the potential confusing
    13  	// situation of us racing to call PostStateUpdate concurrently with
    14  	// different state snapshots.
    15  	stateSync := ctx.State()
    16  	state := stateSync.Lock().DeepCopy()
    17  	defer stateSync.Unlock()
    18  
    19  	// Call the hook
    20  	err := ctx.Hook(func(h Hook) (HookAction, error) {
    21  		return h.PostStateUpdate(state)
    22  	})
    23  	return err
    24  }