mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
a3403f2766
Due to how often the state and plan types are referenced throughout Terraform, there isn't a great way to switch them out gradually. As a consequence, this huge commit gets us from the old world to a _compilable_ new world, but still has a large number of known test failures due to key functionality being stubbed out. The stubs here are for anything that interacts with providers, since we now need to do the follow-up work to similarly replace the old terraform.ResourceProvider interface with its replacement in the new "providers" package. That work, along with work to fix the remaining failing tests, will follow in subsequent commits. The aim here was to replace all references to terraform.State and its downstream types with states.State, terraform.Plan with plans.Plan, state.State with statemgr.State, and switch to the new implementations of the state and plan file formats. However, due to the number of times those types are used, this also ended up affecting numerous other parts of core such as terraform.Hook, the backend.Backend interface, and most of the CLI commands. Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package local
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
"github.com/hashicorp/terraform/backend"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
)
|
|
|
|
func (b *Local) opRefresh(
|
|
stopCtx context.Context,
|
|
cancelCtx context.Context,
|
|
op *backend.Operation,
|
|
runningOp *backend.RunningOperation) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
// Check if our state exists if we're performing a refresh operation. We
|
|
// only do this if we're managing state with this backend.
|
|
if b.Backend == nil {
|
|
if _, err := os.Stat(b.StatePath); err != nil {
|
|
if os.IsNotExist(err) {
|
|
err = nil
|
|
}
|
|
|
|
if err != nil {
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
tfdiags.Error,
|
|
"Cannot read state file",
|
|
fmt.Sprintf("Failed to read %s: %s", b.StatePath, err),
|
|
))
|
|
b.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get our context
|
|
tfCtx, _, opState, contextDiags := b.context(op)
|
|
diags = diags.Append(contextDiags)
|
|
if contextDiags.HasErrors() {
|
|
b.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
// Set our state
|
|
runningOp.State = opState.State()
|
|
if !runningOp.State.HasResources() {
|
|
if b.CLI != nil {
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
tfdiags.Warning,
|
|
"Empty or non-existent state",
|
|
"There are currently no resources tracked in the state, so there is nothing to refresh.",
|
|
))
|
|
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(refreshNoState) + "\n"))
|
|
}
|
|
}
|
|
|
|
// Perform the refresh in a goroutine so we can be interrupted
|
|
var newState *states.State
|
|
var refreshDiags tfdiags.Diagnostics
|
|
doneCh := make(chan struct{})
|
|
go func() {
|
|
defer close(doneCh)
|
|
newState, refreshDiags = tfCtx.Refresh()
|
|
log.Printf("[INFO] backend/local: refresh calling Refresh")
|
|
}()
|
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, tfCtx, opState) {
|
|
return
|
|
}
|
|
|
|
// write the resulting state to the running op
|
|
runningOp.State = newState
|
|
diags = diags.Append(refreshDiags)
|
|
if refreshDiags.HasErrors() {
|
|
b.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
err := statemgr.WriteAndPersist(opState, newState)
|
|
if err != nil {
|
|
diags = diags.Append(errwrap.Wrapf("Failed to write state: {{err}}", err))
|
|
b.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
}
|
|
|
|
const refreshNoState = `
|
|
[reset][bold][yellow]Empty or non-existent state file.[reset][yellow]
|
|
|
|
Refresh will do nothing. Refresh does not error or return an erroneous
|
|
exit status because many automation scripts use refresh, plan, then apply
|
|
and may not have a state file yet for the first run.
|
|
`
|