Documentation ¶
Overview ¶
Package terraform allows to interact with Terraform.
Index ¶
- Constants
- Variables
- func Apply(t testing.TestingT, options *Options) string
- func ApplyAndIdempotent(t testing.TestingT, options *Options) string
- func ApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)
- func ApplyE(t testing.TestingT, options *Options) (string, error)
- func Destroy(t testing.TestingT, options *Options) string
- func DestroyE(t testing.TestingT, options *Options) (string, error)
- func FormatArgs(options *Options, args ...string) []string
- func FormatTerraformArgs(argName string, args []string) []string
- func FormatTerraformBackendConfigAsArgs(vars map[string]interface{}) []string
- func FormatTerraformLockAsArgs(lockCheck bool, lockTimeout string) []string
- func FormatTerraformPlanFileAsArg(commandType string, outPath string) []string
- func FormatTerraformVarsAsArgs(vars map[string]interface{}) []string
- func Get(t testing.TestingT, options *Options) string
- func GetE(t testing.TestingT, options *Options) (string, error)
- func GetExitCodeForTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) int
- func GetExitCodeForTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (int, error)
- func Init(t testing.TestingT, options *Options) string
- func InitAndApply(t testing.TestingT, options *Options) string
- func InitAndApplyAndIdempotent(t testing.TestingT, options *Options) string
- func InitAndApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)
- func InitAndApplyE(t testing.TestingT, options *Options) (string, error)
- func InitAndPlan(t testing.TestingT, options *Options) string
- func InitAndPlanAndShow(t testing.TestingT, options *Options) string
- func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error)
- func InitAndPlanE(t testing.TestingT, options *Options) (string, error)
- func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int
- func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error)
- func InitE(t testing.TestingT, options *Options) (string, error)
- func Output(t testing.TestingT, options *Options, key string) string
- func OutputAll(t testing.TestingT, options *Options) map[string]interface{}
- func OutputAllE(t testing.TestingT, options *Options) (map[string]interface{}, error)
- func OutputE(t testing.TestingT, options *Options, key string) (string, error)
- func OutputForKeys(t testing.TestingT, options *Options, keys []string) map[string]interface{}
- func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]interface{}, error)
- func OutputJson(t testing.TestingT, options *Options, key string) string
- func OutputJsonE(t testing.TestingT, options *Options, key string) (string, error)
- func OutputList(t testing.TestingT, options *Options, key string) []string
- func OutputListE(t testing.TestingT, options *Options, key string) ([]string, error)
- func OutputListOfObjects(t testing.TestingT, options *Options, key string) []map[string]interface{}
- func OutputListOfObjectsE(t testing.TestingT, options *Options, key string) ([]map[string]interface{}, error)
- func OutputMap(t testing.TestingT, options *Options, key string) map[string]string
- func OutputMapE(t testing.TestingT, options *Options, key string) (map[string]string, error)
- func OutputMapOfObjects(t testing.TestingT, options *Options, key string) map[string]interface{}
- func OutputMapOfObjectsE(t testing.TestingT, options *Options, key string) (map[string]interface{}, error)
- func OutputRequired(t testing.TestingT, options *Options, key string) string
- func OutputRequiredE(t testing.TestingT, options *Options, key string) (string, error)
- func OutputStruct(t testing.TestingT, options *Options, key string, v interface{})
- func OutputStructE(t testing.TestingT, options *Options, key string, v interface{}) error
- func Plan(t testing.TestingT, options *Options) string
- func PlanE(t testing.TestingT, options *Options) (string, error)
- func PlanExitCode(t testing.TestingT, options *Options) int
- func PlanExitCodeE(t testing.TestingT, options *Options) (int, error)
- func RunTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) string
- func RunTerraformCommandAndGetStdoutE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)
- func RunTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)
- func Show(t testing.TestingT, options *Options) string
- func ShowE(t testing.TestingT, options *Options) (string, error)
- func TgApplyAll(t testing.TestingT, options *Options) string
- func TgApplyAllE(t testing.TestingT, options *Options) (string, error)
- func TgDestroyAll(t testing.TestingT, options *Options) string
- func TgDestroyAllE(t testing.TestingT, options *Options) (string, error)
- func TgPlanAllExitCode(t testing.TestingT, options *Options) int
- func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error)
- func WorkspaceSelectOrNew(t testing.TestingT, options *Options, name string) string
- func WorkspaceSelectOrNewE(t testing.TestingT, options *Options, name string) (string, error)
- type EmptyOutput
- type Options
- type OutputKeyNotFound
- type OutputValueNotList
- type OutputValueNotMap
- type ResourceCount
- type TgInvalidBinary
- type UnexpectedOutputType
Constants ¶
const DefaultErrorExitCode = 1
DefaultErrorExitCode is the exit code returned when terraform command fails
const DefaultSuccessExitCode = 0
DefaultSuccessExitCode is the exit code returned when terraform command succeeds
const TerraformPlanChangesPresentExitCode = 2
TerraformPlanChangesPresentExitCode is the exit code returned by terraform plan detailed exitcode when changes are present
Variables ¶
var (
DefaultRetryableTerraformErrors = map[string]string{
".*read: connection reset by peer.*": "Failed to reach helm charts repository.",
".*transport is closing.*": "Failed to reach Kubernetes API.",
".*unable to verify signature.*": "Failed to retrieve plugin due to transient network error.",
".*unable to verify checksum.*": "Failed to retrieve plugin due to transient network error.",
".*no provider exists with the given name.*": "Failed to retrieve plugin due to transient network error.",
".*registry service is unreachable.*": "Failed to retrieve plugin due to transient network error.",
".*Error installing provider.*": "Failed to retrieve plugin due to transient network error.",
".*Failed to query available provider packages.*": "Failed to retrieve plugin due to transient network error.",
".*timeout while waiting for plugin to start.*": "Failed to retrieve plugin due to transient network error.",
".*timed out waiting for server handshake.*": "Failed to retrieve plugin due to transient network error.",
".*Provider produced inconsistent result after apply.*": "Provider eventual consistency error.",
}
)
var (
PlanFilePathRequired = fmt.Errorf("You must set PlanFilePath on options struct to use this function.")
)
var TerraformCommandsWithLockSupport = []string{
"plan",
"apply",
"destroy",
"init",
"refresh",
"taint",
"untaint",
"import",
}
TerraformCommandsWithLockSupport is a list of all the Terraform commands that can obtain locks on Terraform state
var TerraformCommandsWithPlanFileSupport = []string{
"plan",
"apply",
"show",
"graph",
}
TerraformCommandsWithPlanFileSupport is a list of all the Terraform commands that support interacting with plan files.
Functions ¶
func Apply ¶
Apply runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func ApplyAndIdempotent ¶ added in v0.28.5
ApplyAndIdempotent runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func ApplyAndIdempotentE ¶ added in v0.28.5
ApplyAndIdempotentE runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func ApplyE ¶
ApplyE runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func FormatArgs ¶
FormatArgs converts the inputs to a format palatable to terraform. This includes converting the given vars to the format the Terraform CLI expects (-var key=value).
func FormatTerraformArgs ¶ added in v0.13.27
FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars"}) returns "-var-file foo.tfvars -var-file bar.tfvars"
func FormatTerraformBackendConfigAsArgs ¶ added in v0.9.13
FormatTerraformBackendConfigAsArgs formats the given variables as backend config args for Terraform (e.g. of the format -backend-config=key=value).
func FormatTerraformLockAsArgs ¶ added in v0.23.1
FormatTerraformLockAsArgs formats the lock and lock-timeout variables -lock, -lock-timeout
func FormatTerraformPlanFileAsArg ¶ added in v0.30.16
FormatTerraformPlanFileAsArg formats the out variable as a command-line arg for Terraform (e.g. of the format -out=/some/path/to/plan.out or /some/path/to/plan.out). Only plan supports passing in the plan file as -out; the other commands expect it as the first positional argument. This returns an empty string if outPath is empty string.
func FormatTerraformVarsAsArgs ¶
FormatTerraformVarsAsArgs formats the given variables as command-line args for Terraform (e.g. of the format -var key=value).
func GetExitCodeForTerraformCommand ¶ added in v0.10.4
func GetExitCodeForTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) int
GetExitCodeForTerraformCommand runs terraform with the given arguments and options and returns exit code
func GetExitCodeForTerraformCommandE ¶ added in v0.10.4
func GetExitCodeForTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (int, error)
GetExitCodeForTerraformCommandE runs terraform with the given arguments and options and returns exit code
func InitAndApply ¶
InitAndApply runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func InitAndApplyAndIdempotent ¶ added in v0.28.5
InitAndApplyAndIdempotent runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func InitAndApplyAndIdempotentE ¶ added in v0.28.5
InitAndApplyAndIdempotentE runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func InitAndApplyE ¶
InitAndApplyE runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func InitAndPlan ¶ added in v0.10.4
InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command. This will fail the test if there is an error in the command.
func InitAndPlanAndShow ¶ added in v0.30.16
InitAndPlanAndShow runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file. This will fail the test if there is an error in the command.
func InitAndPlanAndShowE ¶ added in v0.30.16
InitAndPlanAndShowE runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file.
func InitAndPlanE ¶ added in v0.10.4
InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
func InitAndPlanWithExitCode ¶ added in v0.17.0
InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command. This will fail the test if there is an error in the command.
func InitAndPlanWithExitCodeE ¶ added in v0.17.0
InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.
func Output ¶
Output calls terraform output for the given variable and return its string value representation. It only designed to work with primitive terraform types: string, number and bool. Please use OutputStruct for anything else.
func OutputAll ¶ added in v0.13.19
OutputAll calls terraform output returns all values as a map. If there is error fetching the output, fails the test
func OutputAllE ¶ added in v0.13.19
OutputAllE calls terraform and returns all the outputs as a map
func OutputE ¶
OutputE calls terraform output for the given variable and return its string value representation. It only designed to work with primitive terraform types: string, number and bool. Please use OutputStructE for anything else.
func OutputForKeys ¶ added in v0.13.19
OutputForKeys calls terraform output for the given key list and returns values as a map. If keys not found in the output, fails the test
func OutputForKeysE ¶ added in v0.13.19
func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]interface{}, error)
OutputForKeysE calls terraform output for the given key list and returns values as a map. The returned values are of type interface{} and need to be type casted as necessary. Refer to output_test.go
func OutputJson ¶ added in v0.31.1
OutputJson calls terraform output for the given variable and returns the result as the json string. If key is an empty string, it will return all the output variables.
func OutputJsonE ¶ added in v0.31.1
OutputJsonE calls terraform output for the given variable and returns the result as the json string. If key is an empty string, it will return all the output variables.
func OutputList ¶ added in v0.13.1
OutputList calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it fails the test.
func OutputListE ¶ added in v0.13.1
OutputListE calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it returns an error.
func OutputListOfObjects ¶ added in v0.28.2
OutputListOfObjects calls terraform output for the given variable and returns its value as a list of maps/lists. If the output value is not a list of maps/lists, then it fails the test.
func OutputListOfObjectsE ¶ added in v0.28.2
func OutputListOfObjectsE(t testing.TestingT, options *Options, key string) ([]map[string]interface{}, error)
OutputListOfObjectsE calls terraform output for the given variable and returns its value as a list of maps/lists. Also returns an error object if an error was generated. If the output value is not a list of maps/lists, then it fails the test.
func OutputMap ¶ added in v0.13.6
OutputMap calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it fails the test.
func OutputMapE ¶ added in v0.13.6
OutputMapE calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it returns an error.
func OutputMapOfObjects ¶ added in v0.28.2
OutputMapOfObjects calls terraform output for the given variable and returns its value as a map of lists/maps. If the output value is not a map of lists/maps, then it fails the test.
func OutputMapOfObjectsE ¶ added in v0.28.2
func OutputMapOfObjectsE(t testing.TestingT, options *Options, key string) (map[string]interface{}, error)
OutputMapOfObjectsE calls terraform output for the given variable and returns its value as a map of lists/maps. Also returns an error object if an error was generated. If the output value is not a map of lists/maps, then it fails the test.
func OutputRequired ¶
OutputRequired calls terraform output for the given variable and return its value. If the value is empty, fail the test.
func OutputRequiredE ¶
OutputRequiredE calls terraform output for the given variable and return its value. If the value is empty, return an error.
func OutputStruct ¶ added in v0.28.8
OutputStruct calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it fails the test.
func OutputStructE ¶ added in v0.28.8
OutputStructE calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it returns an error.
func Plan ¶ added in v0.17.0
Plan runs terraform plan with the given options and returns stdout/stderr. This will fail the test if there is an error in the command.
func PlanE ¶ added in v0.17.0
PlanE runs terraform plan with the given options and returns stdout/stderr.
func PlanExitCode ¶ added in v0.10.4
PlanExitCode runs terraform plan with the given options and returns the detailed exitcode. This will fail the test if there is an error in the command.
func PlanExitCodeE ¶ added in v0.10.4
PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode.
func RunTerraformCommand ¶ added in v0.9.2
RunTerraformCommand runs terraform with the given arguments and options and return stdout/stderr.
func RunTerraformCommandAndGetStdoutE ¶ added in v0.17.2
func RunTerraformCommandAndGetStdoutE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)
RunTerraformCommandAndGetStdoutE runs terraform with the given arguments and options and returns solely its stdout (but not stderr).
func RunTerraformCommandE ¶ added in v0.9.2
func RunTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)
RunTerraformCommandE runs terraform with the given arguments and options and return stdout/stderr.
func Show ¶ added in v0.30.16
Show calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. This will fail the test if there is an error in the command.
func ShowE ¶ added in v0.30.16
ShowE calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir.
func TgApplyAll ¶ added in v0.15.12
TgApplyAll runs terragrunt apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func TgApplyAllE ¶ added in v0.15.12
TgApplyAllE runs terragrunt apply-all with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.
func TgDestroyAll ¶ added in v0.15.12
TgDestroyAll runs terragrunt destroy with the given options and return stdout.
func TgDestroyAllE ¶ added in v0.15.12
TgDestroyAllE runs terragrunt destroy with the given options and return stdout.
func TgPlanAllExitCode ¶ added in v0.15.12
TgPlanAllExitCode runs terragrunt plan-all with the given options and returns the detailed exitcode. This will fail the test if there is an error in the command.
func TgPlanAllExitCodeE ¶ added in v0.15.12
TgPlanAllExitCodeE runs terragrunt plan-all with the given options and returns the detailed exitcode.
func WorkspaceSelectOrNew ¶ added in v0.12.2
WorkspaceSelectOrNew runs terraform workspace with the given options and the workspace name and returns a name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist.
func WorkspaceSelectOrNewE ¶ added in v0.12.2
WorkspaceSelectOrNewE runs terraform workspace with the given options and the workspace name and returns a name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist.
Types ¶
type EmptyOutput ¶
type EmptyOutput string
EmptyOutput is an error that occurs when an output is empty.
func (EmptyOutput) Error ¶
func (outputName EmptyOutput) Error() string
type Options ¶
type Options struct { TerraformBinary string // Name of the binary that will be used TerraformDir string // The path to the folder where the Terraform code is defined. // The vars to pass to Terraform commands using the -var option. Note that terraform does not support passing `null` // as a variable value through the command line. That is, if you use `map[string]interface{}{"foo": nil}` as `Vars`, // this will translate to the string literal `"null"` being assigned to the variable `foo`. However, nulls in // lists and maps/objects are supported. E.g., the following var will be set as expected (`{ bar = null }`: // map[string]interface{}{ // "foo": map[string]interface{}{"bar": nil}, // } Vars map[string]interface{} VarFiles []string // The var file paths to pass to Terraform commands using -var-file option. Targets []string // The target resources to pass to the terraform command with -target Lock bool // The lock option to pass to the terraform command with -lock LockTimeout string // The lock timeout option to pass to the terraform command with -lock-timeout EnvVars map[string]string // Environment variables to set when running Terraform BackendConfig map[string]interface{} // The vars to pass to the terraform init command for extra configuration for the backend RetryableTerraformErrors map[string]string // If Terraform apply fails with one of these (transient) errors, retry. The keys are a regexp to match against the error and the message is what to display to a user if that error is matched. MaxRetries int // Maximum number of times to retry errors matching RetryableTerraformErrors TimeBetweenRetries time.Duration // The amount of time to wait between retries Upgrade bool // Whether the -upgrade flag of the terraform init command should be set to true or not NoColor bool // Whether the -no-color flag will be set for any Terraform command or not SshAgent *ssh.SshAgent // Overrides local SSH agent with the given in-process agent NoStderr bool // Disable stderr redirection OutputMaxLineSize int // The max size of one line in stdout and stderr (in bytes) Logger *logger.Logger // Set a non-default logger that should be used. See the logger package for more info. Parallelism int // Set the parallelism setting for Terraform PlanFilePath string // The path to output a plan file to (for the plan command) or read one from (for the apply command) }
Options for running Terraform commands
func GetCommonOptions ¶ added in v0.10.4
GetCommonOptions extracts commons terraform options
func WithDefaultRetryableErrors ¶ added in v0.30.15
WithDefaultRetryableErrors makes a copy of the Options object and returns an updated object with sensible defaults for retryable errors. The included retryable errors are typical errors that most terraform modules encounter during testing, and are known to self resolve upon retrying. This will fail the test if there are any errors in the cloning process.
func (*Options) Clone ¶ added in v0.30.15
Clone makes a deep copy of most fields on the Options object and returns it.
NOTE: options.SshAgent and options.Logger CANNOT be deep copied (e.g., the SshAgent struct contains channels and listeners that can't be meaningfully copied), so the original values are retained.
type OutputKeyNotFound ¶ added in v0.15.12
type OutputKeyNotFound string
OutputKeyNotFound occurs when terraform output does not contain a value for the key specified in the function call
func (OutputKeyNotFound) Error ¶ added in v0.15.12
func (err OutputKeyNotFound) Error() string
type OutputValueNotList ¶ added in v0.15.12
type OutputValueNotList struct {
Value interface{}
}
OutputValueNotList occurs when casting a found output value to a list of interfaces fails
func (OutputValueNotList) Error ¶ added in v0.15.12
func (err OutputValueNotList) Error() string
type OutputValueNotMap ¶ added in v0.15.12
type OutputValueNotMap struct {
Value interface{}
}
OutputValueNotMap occures when casting a found output value to a map fails
func (OutputValueNotMap) Error ¶ added in v0.15.12
func (err OutputValueNotMap) Error() string
type ResourceCount ¶ added in v0.17.1
ResourceCount represents counts of resources affected by terraform apply/plan/destroy command.
func GetResourceCount ¶ added in v0.17.1
func GetResourceCount(t testing.TestingT, cmdout string) *ResourceCount
GetResourceCount parses stdout/stderr of apply/plan/destroy commands and returns number of affected resources. This will fail the test if given stdout/stderr isn't a valid output of apply/plan/destroy.
func GetResourceCountE ¶ added in v0.17.1
func GetResourceCountE(t testing.TestingT, cmdout string) (*ResourceCount, error)
GetResourceCountE parses stdout/stderr of apply/plan/destroy commands and returns number of affected resources.
type TgInvalidBinary ¶ added in v0.15.12
type TgInvalidBinary string
TgInvalidBinary occurs when a terragrunt function is called and the TerraformBinary is set to a value other than terragrunt
func (TgInvalidBinary) Error ¶ added in v0.15.12
func (err TgInvalidBinary) Error() string
type UnexpectedOutputType ¶ added in v0.15.13
UnexpectedOutputType is an error that occurs when the output is not of the type we expect
func (UnexpectedOutputType) Error ¶ added in v0.15.13
func (err UnexpectedOutputType) Error() string