github.com/terraform-modules-krish/terratest@v0.29.0/modules/terraform/apply.go (about)

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