Skip to content

Commit

Permalink
Add a diff option to a plan command
Browse files Browse the repository at this point in the history
  • Loading branch information
bells17 committed Feb 26, 2018
1 parent 9cf5350 commit c7f6c5d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type Operation struct {
PlanId string
PlanRefresh bool // PlanRefresh will do a refresh before a plan
PlanOutPath string // PlanOutPath is the path to save the plan
PlanDiffPath string // PlanDiffPath is the path to save the plan diff
PlanOutBackend *terraform.BackendState

// Module settings specify the root module to use for operations.
Expand Down
25 changes: 25 additions & 0 deletions backend/local/backend_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package local
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -174,6 +175,26 @@ func (b *Local) opPlan(
}

}

// Save the diff to disk
if diffPath := op.PlanDiffPath; diffPath != "" {
log.Printf("[INFO] backend/local: writing diff output to: %s", diffPath)
f, diffErr := os.Create(diffPath)
if diffErr == nil {
diffJson, diffErr := json.MarshalIndent(plan.Diff, "", "\t")
if diffErr == nil {
fmt.Fprint(f, string(diffJson))
}
}
f.Close()

if diffErr != nil {
runningOp.Err = fmt.Errorf("Error writing diff file: %s", diffErr)
return
} else {
b.CLI.Output(fmt.Sprintf("\n"+strings.TrimSpace(diffHeaderOutput)+"\n", diffPath))
}
}
}
}

Expand Down Expand Up @@ -240,6 +261,10 @@ To perform exactly these actions, run the following command to apply:
terraform apply %q
`

const diffHeaderOutput = `
This plan diff was saved to: %s
`

const planNoChanges = `
[reset][bold][green]No changes. Infrastructure is up-to-date.[reset][green]
Expand Down
39 changes: 39 additions & 0 deletions backend/local/backend_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,45 @@ func TestLocal_planOutPathNoChange(t *testing.T) {
}
}

func TestLocal_planDiff(t *testing.T) {
b := TestLocal(t)
p := TestLocalProvider(t, b, "test")

mod, modCleanup := module.TestTree(t, "./test-fixtures/plan")
defer modCleanup()

outDir := testTempDir(t)
defer os.RemoveAll(outDir)
diffPath := filepath.Join(outDir, "diff.json")

b.CLI = cli.NewMockUi()
{
op := testOperationPlan()
op.Module = mod
op.PlanRefresh = true
op.PlanDiffPath = diffPath

run, err := b.Operation(context.Background(), op)
if err != nil {
t.Fatalf("bad: %s", err)
}
<-run.Done()
if run.Err != nil {
t.Fatalf("err: %s", err)
}

if !p.DiffCalled {
t.Fatal("diff should be called")
}

f, err := os.Open(diffPath)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
}
}

// TestLocal_planScaleOutNoDupeCount tests a Refresh/Plan sequence when a
// resource count is scaled out. The scaled out node needs to exist in the
// graph and run through a plan-style sequence during the refresh phase, but
Expand Down
5 changes: 5 additions & 0 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type PlanCommand struct {
func (c *PlanCommand) Run(args []string) int {
var destroy, refresh, detailed bool
var outPath string
var diffPath string
var moduleDepth int

args, err := c.Meta.process(args, true)
Expand All @@ -31,6 +32,7 @@ func (c *PlanCommand) Run(args []string) int {
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
c.addModuleDepthFlag(cmdFlags, &moduleDepth)
cmdFlags.StringVar(&outPath, "out", "", "path")
cmdFlags.StringVar(&diffPath, "diff", "", "path")
cmdFlags.IntVar(
&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
Expand Down Expand Up @@ -103,6 +105,7 @@ func (c *PlanCommand) Run(args []string) int {
opReq.Plan = plan
opReq.PlanRefresh = refresh
opReq.PlanOutPath = outPath
opReq.PlanDiffPath = diffPath
opReq.Type = backend.OperationTypePlan

// Perform the operation
Expand Down Expand Up @@ -148,6 +151,8 @@ Options:
1 - Errored
2 - Succeeded, there is a diff
-diff=path Write a plan diff file to the given path.
-input=true Ask for input for variables if not directly set.
-lock=true Lock the state file when locking is supported.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/commands/plan.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The command-line flags are all optional. The list of available flags are:
* 1 = Error
* 2 = Succeeded with non-empty diff (changes present)

* `-diff=path` - The path to save the generated plan diff file.

* `-input=true` - Ask for input for variables if not directly set.

* `-lock=true` - Lock the state file when locking is supported.
Expand Down

0 comments on commit c7f6c5d

Please sign in to comment.