Skip to content

Commit

Permalink
add ShowStateFile, ShowPlanFile
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Aug 20, 2020
1 parent 63af3ce commit dd8ef6b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tfexec/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
tfjson "github.com/hashicorp/terraform-json"
)

// Show reads the default state path and outputs the state.
// To read a state or plan file, ShowState or ShowPlan must be used instead.
func (tf *Terraform) Show(ctx context.Context) (*tfjson.State, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
Expand Down Expand Up @@ -40,6 +42,77 @@ func (tf *Terraform) Show(ctx context.Context) (*tfjson.State, error) {
return &ret, nil
}

// ShowStateFile reads a given state file and outputs the state.
func (tf *Terraform) ShowStateFile(ctx context.Context, statePath string) (*tfjson.State, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform show -json was added in 0.12.0: %w", err)
}

if statePath == "" {
return nil, fmt.Errorf("statePath cannot be blank: use Show() if not passing statePath")
}

showCmd := tf.showCmd(ctx, statePath)

var ret tfjson.State
var outBuf bytes.Buffer
showCmd.Stdout = &outBuf

err = tf.runTerraformCmd(showCmd)
if err != nil {
return nil, err
}

err = json.Unmarshal(outBuf.Bytes(), &ret)
if err != nil {
return nil, err
}

err = ret.Validate()
if err != nil {
return nil, err
}

return &ret, nil
}

// ShowPlanFile reads a given plan file and outputs the plan.
func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string) (*tfjson.Plan, error) {
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform show -json was added in 0.12.0: %w", err)
}

if planPath == "" {
return nil, fmt.Errorf("planPath cannot be blank: use Show() if not passing planPath")
}

showCmd := tf.showCmd(ctx, planPath)

var ret tfjson.Plan
var outBuf bytes.Buffer
showCmd.Stdout = &outBuf

err = tf.runTerraformCmd(showCmd)
if err != nil {
return nil, err
}

err = json.Unmarshal(outBuf.Bytes(), &ret)
if err != nil {
return nil, err
}

err = ret.Validate()
if err != nil {
return nil, err
}

return &ret, nil

}

func (tf *Terraform) showCmd(ctx context.Context, args ...string) *exec.Cmd {
allArgs := []string{"show", "-json", "-no-color"}
allArgs = append(allArgs, args...)
Expand Down
44 changes: 44 additions & 0 deletions tfexec/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,47 @@ func TestShowCmd(t *testing.T) {
"-no-color",
}, nil, showCmd)
}

func TestShowStateFileCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

showCmd := tf.showCmd(context.Background(), "statefilepath")

assertCmd(t, []string{
"show",
"-json",
"-no-color",
"statefilepath",
}, nil, showCmd)
}

func TestShowPlanFileCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

showCmd := tf.showCmd(context.Background(), "planfilepath")

assertCmd(t, []string{
"show",
"-json",
"-no-color",
"planfilepath",
}, nil, showCmd)
}

0 comments on commit dd8ef6b

Please sign in to comment.