plans/planfile: Create takes most arguments via a struct type

Previously the planfile.Create function had accumulated probably already
too many positional arguments, and I'm intending to add another one in
a subsequent commit and so this is preparation to make the callsites more
readable (subjectively) and make it clearer how we can extend this
function's arguments to include further components in a plan file.

There's no difference in observable functionality here. This is just
passing the same set of arguments in a slightly different way.
This commit is contained in:
Martin Atkins 2021-09-29 16:03:10 -07:00
parent 8d193ad268
commit 6a98e4720c
6 changed files with 60 additions and 10 deletions

View File

@ -132,7 +132,13 @@ func TestLocalRun_stalePlan(t *testing.T) {
outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan")
if err := planfile.Create(planPath, configload.NewEmptySnapshot(), prevStateFile, stateFile, plan); err != nil {
planfileArgs := planfile.CreateArgs{
ConfigSnapshot: configload.NewEmptySnapshot(),
PreviousRunStateFile: prevStateFile,
StateFile: stateFile,
Plan: plan,
}
if err := planfile.Create(planPath, planfileArgs); err != nil {
t.Fatalf("unexpected error writing planfile: %s", err)
}
planFile, err := planfile.Open(planPath)

View File

@ -133,7 +133,12 @@ func (b *Local) opPlan(
}
log.Printf("[INFO] backend/local: writing plan output to: %s", path)
err := planfile.Create(path, configSnap, prevStateFile, plannedStateFile, plan)
err := planfile.Create(path, planfile.CreateArgs{
ConfigSnapshot: configSnap,
PreviousRunStateFile: prevStateFile,
StateFile: plannedStateFile,
Plan: plan,
})
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,

View File

@ -242,7 +242,12 @@ func testPlanFile(t *testing.T, configSnap *configload.Snapshot, state *states.S
}
path := testTempFile(t)
err := planfile.Create(path, configSnap, prevStateFile, stateFile, plan)
err := planfile.Create(path, planfile.CreateArgs{
ConfigSnapshot: configSnap,
PreviousRunStateFile: prevStateFile,
StateFile: stateFile,
Plan: plan,
})
if err != nil {
t.Fatalf("failed to create temporary plan file: %s", err)
}

View File

@ -77,7 +77,12 @@ func TestRoundtrip(t *testing.T) {
}
planFn := filepath.Join(workDir, "tfplan")
err = Create(planFn, snapIn, prevStateFileIn, stateFileIn, planIn)
err = Create(planFn, CreateArgs{
ConfigSnapshot: snapIn,
PreviousRunStateFile: prevStateFileIn,
StateFile: stateFileIn,
Plan: planIn,
})
if err != nil {
t.Fatalf("failed to create plan file: %s", err)
}

View File

@ -11,6 +11,30 @@ import (
"github.com/hashicorp/terraform/internal/states/statefile"
)
type CreateArgs struct {
// ConfigSnapshot is a snapshot of the configuration that the plan
// was created from.
ConfigSnapshot *configload.Snapshot
// PreviousRunStateFile is a representation of the state snapshot we used
// as the original input when creating this plan, containing the same
// information as recorded at the end of the previous apply except for
// upgrading managed resource instance data to the provider's latest
// schema versions.
PreviousRunStateFile *statefile.File
// BaseStateFile is a representation of the state snapshot we used to
// create the plan, which is the result of asking the providers to refresh
// all previously-stored objects to match the current situation in the
// remote system. (If this plan was created with refreshing disabled,
// this should be the same as PreviousRunStateFile.)
StateFile *statefile.File
// Plan records the plan itself, which is the main artifact inside a
// saved plan file.
Plan *plans.Plan
}
// Create creates a new plan file with the given filename, overwriting any
// file that might already exist there.
//
@ -18,7 +42,7 @@ import (
// state file in addition to the plan itself, so that Terraform can detect
// if the world has changed since the plan was created and thus refuse to
// apply it.
func Create(filename string, configSnap *configload.Snapshot, prevStateFile, stateFile *statefile.File, plan *plans.Plan) error {
func Create(filename string, args CreateArgs) error {
f, err := os.Create(filename)
if err != nil {
return err
@ -38,7 +62,7 @@ func Create(filename string, configSnap *configload.Snapshot, prevStateFile, sta
if err != nil {
return fmt.Errorf("failed to create tfplan file: %s", err)
}
err = writeTfplan(plan, w)
err = writeTfplan(args.Plan, w)
if err != nil {
return fmt.Errorf("failed to write plan: %s", err)
}
@ -54,7 +78,7 @@ func Create(filename string, configSnap *configload.Snapshot, prevStateFile, sta
if err != nil {
return fmt.Errorf("failed to create embedded tfstate file: %s", err)
}
err = statefile.Write(stateFile, w)
err = statefile.Write(args.StateFile, w)
if err != nil {
return fmt.Errorf("failed to write state snapshot: %s", err)
}
@ -70,7 +94,7 @@ func Create(filename string, configSnap *configload.Snapshot, prevStateFile, sta
if err != nil {
return fmt.Errorf("failed to create embedded tfstate-prev file: %s", err)
}
err = statefile.Write(prevStateFile, w)
err = statefile.Write(args.PreviousRunStateFile, w)
if err != nil {
return fmt.Errorf("failed to write previous state snapshot: %s", err)
}
@ -78,7 +102,7 @@ func Create(filename string, configSnap *configload.Snapshot, prevStateFile, sta
// tfconfig directory
{
err := writeConfigSnapshot(configSnap, zw)
err := writeConfigSnapshot(args.ConfigSnapshot, zw)
if err != nil {
return fmt.Errorf("failed to write config snapshot: %s", err)
}

View File

@ -655,7 +655,12 @@ func contextOptsForPlanViaFile(configSnap *configload.Snapshot, plan *plans.Plan
}
filename := filepath.Join(dir, "tfplan")
err = planfile.Create(filename, configSnap, prevStateFile, stateFile, plan)
err = planfile.Create(filename, planfile.CreateArgs{
ConfigSnapshot: configSnap,
PreviousRunStateFile: prevStateFile,
StateFile: stateFile,
Plan: plan,
})
if err != nil {
return nil, nil, nil, err
}