github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/terraform/show.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/gruntwork-io/terratest/modules/testing"
     5  	"github.com/stretchr/testify/require"
     6  )
     7  
     8  // Show calls terraform show in json mode with the given options and returns stdout from the command. If
     9  // PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the
    10  // terraform module at options.TerraformDir. This will fail the test if there is an error in the command.
    11  func Show(t testing.TestingT, options *Options) string {
    12  	out, err := ShowE(t, options)
    13  	require.NoError(t, err)
    14  	return out
    15  }
    16  
    17  // ShowE calls terraform show in json mode with the given options and returns stdout from the command. If
    18  // PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the
    19  // terraform module at options.TerraformDir.
    20  func ShowE(t testing.TestingT, options *Options) (string, error) {
    21  	// We manually construct the args here instead of using `FormatArgs`, because show only accepts a limited set of
    22  	// args.
    23  	args := []string{"show", "-no-color", "-json"}
    24  
    25  	// Attach plan file path if specified.
    26  	if options.PlanFilePath != "" {
    27  		args = append(args, options.PlanFilePath)
    28  	}
    29  	return RunTerraformCommandAndGetStdoutE(t, options, args...)
    30  }
    31  
    32  func ShowWithStruct(t testing.TestingT, options *Options) *PlanStruct {
    33  	out, err := ShowWithStructE(t, options)
    34  	require.NoError(t, err)
    35  	return out
    36  }
    37  
    38  func ShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error) {
    39  	json, err := ShowE(t, options)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	planStruct, err := parsePlanJson(json)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return planStruct, nil
    48  }