github.com/mponton/terratest@v0.44.0/modules/terraform/apply.go (about)

     1  package terraform
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/mponton/terratest/modules/testing"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // InitAndApply runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this
    11  // method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running
    12  // apply.
    13  func InitAndApply(t testing.TestingT, options *Options) string {
    14  	out, err := InitAndApplyE(t, options)
    15  	require.NoError(t, err)
    16  	return out
    17  }
    18  
    19  // InitAndApplyE runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this
    20  // method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running
    21  // apply.
    22  func InitAndApplyE(t testing.TestingT, options *Options) (string, error) {
    23  	if _, err := InitE(t, options); err != nil {
    24  		return "", err
    25  	}
    26  
    27  	return ApplyE(t, options)
    28  }
    29  
    30  // Apply runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and
    31  // assumes the caller is responsible for cleaning up any resources created by running apply.
    32  func Apply(t testing.TestingT, options *Options) string {
    33  	out, err := ApplyE(t, options)
    34  	require.NoError(t, err)
    35  	return out
    36  }
    37  
    38  // TgApplyAll runs terragrunt apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and
    39  // assumes the caller is responsible for cleaning up any resources created by running apply.
    40  func TgApplyAll(t testing.TestingT, options *Options) string {
    41  	out, err := TgApplyAllE(t, options)
    42  	require.NoError(t, err)
    43  	return out
    44  }
    45  
    46  // ApplyE runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and
    47  // assumes the caller is responsible for cleaning up any resources created by running apply.
    48  func ApplyE(t testing.TestingT, options *Options) (string, error) {
    49  	return RunTerraformCommandE(t, options, FormatArgs(options, "apply", "-input=false", "-auto-approve")...)
    50  }
    51  
    52  // TgApplyAllE runs terragrunt apply-all with the given options and return stdout/stderr. Note that this method does NOT call destroy and
    53  // assumes the caller is responsible for cleaning up any resources created by running apply.
    54  func TgApplyAllE(t testing.TestingT, options *Options) (string, error) {
    55  	if options.TerraformBinary != "terragrunt" {
    56  		return "", TgInvalidBinary(options.TerraformBinary)
    57  	}
    58  
    59  	return RunTerraformCommandE(t, options, FormatArgs(options, "run-all", "apply", "-input=false", "-auto-approve")...)
    60  }
    61  
    62  // ApplyAndIdempotent runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs
    63  // plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes
    64  // the caller is responsible for cleaning up any resources created by running apply.
    65  func ApplyAndIdempotent(t testing.TestingT, options *Options) string {
    66  	out, err := ApplyAndIdempotentE(t, options)
    67  	require.NoError(t, err)
    68  
    69  	return out
    70  }
    71  
    72  // ApplyAndIdempotentE runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs
    73  // plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes
    74  // the caller is responsible for cleaning up any resources created by running apply.
    75  func ApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error) {
    76  	out, err := ApplyE(t, options)
    77  
    78  	if err != nil {
    79  		return out, err
    80  	}
    81  
    82  	exitCode, err := PlanExitCodeE(t, options)
    83  
    84  	if err != nil {
    85  		return out, err
    86  	}
    87  
    88  	if exitCode != 0 {
    89  		return out, errors.New("terraform configuration not idempotent")
    90  	}
    91  
    92  	return out, nil
    93  }
    94  
    95  // InitAndApplyAndIdempotent runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs
    96  // plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes
    97  // the caller is responsible for cleaning up any resources created by running apply.
    98  func InitAndApplyAndIdempotent(t testing.TestingT, options *Options) string {
    99  	out, err := InitAndApplyAndIdempotentE(t, options)
   100  	require.NoError(t, err)
   101  
   102  	return out
   103  }
   104  
   105  // InitAndApplyAndIdempotentE runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs
   106  // plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes
   107  // the caller is responsible for cleaning up any resources created by running apply.
   108  func InitAndApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error) {
   109  	if _, err := InitE(t, options); err != nil {
   110  		return "", err
   111  	}
   112  
   113  	return ApplyAndIdempotentE(t, options)
   114  }