terraform

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2021 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package terraform allows to interact with Terraform.

Index

Constants

View Source
const DefaultErrorExitCode = 1

DefaultErrorExitCode is the exit code returned when terraform command fails

View Source
const DefaultSuccessExitCode = 0

DefaultSuccessExitCode is the exit code returned when terraform command succeeds

View Source
const TerraformPlanChangesPresentExitCode = 2

TerraformPlanChangesPresentExitCode is the exit code returned by terraform plan detailed exitcode when changes are present

Variables

View Source
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.",
		"could not query provider registry for":           "Failed to retrieve plugin due to transient network error.",

		".*Provider produced inconsistent result after apply.*": "Provider eventual consistency error.",
	}
)
View Source
var (
	PlanFilePathRequired = fmt.Errorf("You must set PlanFilePath on options struct to use this function.")
)
View Source
var TerraformCommandsWithLockSupport = []string{
	"plan",
	"plan-all",
	"apply",
	"apply-all",
	"destroy",
	"destroy-all",
	"init",
	"refresh",
	"taint",
	"untaint",
	"import",
}

TerraformCommandsWithLockSupport is a list of all the Terraform commands that can obtain locks on Terraform state

View Source
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

func Apply(t testing.TestingT, options *Options) string

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

func ApplyAndIdempotent(t testing.TestingT, options *Options) string

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

func ApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)

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

func ApplyE(t testing.TestingT, options *Options) (string, error)

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 AssertPlannedValuesMapKeyExists

func AssertPlannedValuesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

AssertPlannedValuesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func AssertResourceChangesMapKeyExists

func AssertResourceChangesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

AssertResourceChangesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func Destroy

func Destroy(t testing.TestingT, options *Options) string

Destroy runs terraform destroy with the given options and return stdout/stderr.

func DestroyE

func DestroyE(t testing.TestingT, options *Options) (string, error)

DestroyE runs terraform destroy with the given options and return stdout/stderr.

func FormatArgs

func FormatArgs(options *Options, args ...string) []string

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

func FormatTerraformArgs(argName string, args []string) []string

FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars", "baz.tfvars.json"}) returns "-var-file foo.tfvars -var-file bar.tfvars -var-file baz.tfvars.json"

func FormatTerraformBackendConfigAsArgs

func FormatTerraformBackendConfigAsArgs(vars map[string]interface{}) []string

FormatTerraformBackendConfigAsArgs formats the given variables as backend config args for Terraform (e.g. of the format -backend-config=key=value).

func FormatTerraformLockAsArgs

func FormatTerraformLockAsArgs(lockCheck bool, lockTimeout string) []string

FormatTerraformLockAsArgs formats the lock and lock-timeout variables -lock, -lock-timeout

func FormatTerraformPlanFileAsArg

func FormatTerraformPlanFileAsArg(commandType string, outPath string) []string

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 FormatTerraformPluginDirAsArgs

func FormatTerraformPluginDirAsArgs(pluginDir string) []string

FormatTerraformPluginDirAsArgs formats the plugin-dir variable -plugin-dir

func FormatTerraformVarsAsArgs

func FormatTerraformVarsAsArgs(vars map[string]interface{}) []string

FormatTerraformVarsAsArgs formats the given variables as command-line args for Terraform (e.g. of the format -var key=value).

func Get

func Get(t testing.TestingT, options *Options) string

Get calls terraform get and return stdout/stderr.

func GetAllVariablesFromVarFile

func GetAllVariablesFromVarFile(t testing.TestingT, fileName string, out interface{})

GetAllVariablesFromVarFile Parses all data from a provided input file found ind in VarFile and stores the result in the value pointed to by out.

func GetAllVariablesFromVarFileE

func GetAllVariablesFromVarFileE(t testing.TestingT, fileName string, out interface{}) error

GetAllVariablesFromVarFileE Parses all data from a provided input file found ind in VarFile and stores the result in the value pointed to by out. Returns an error if the specified file does not exist, the specified file is not readable, or the specified file cannot be decoded from HCL.

func GetE

func GetE(t testing.TestingT, options *Options) (string, error)

GetE calls terraform get and return stdout/stderr.

func GetExitCodeForTerraformCommand

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

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 GetVariableAsListFromVarFile

func GetVariableAsListFromVarFile(t testing.TestingT, fileName string, key string) []string

GetVariableAsListFromVarFile Gets the string list representation of a variable from a provided input file found in VarFile Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile.

func GetVariableAsListFromVarFileE

func GetVariableAsListFromVarFileE(t testing.TestingT, fileName string, key string) ([]string, error)

GetVariableAsListFromVarFileE Gets the string list representation of a variable from a provided input file found in VarFile Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile. Will return error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a list

func GetVariableAsMapFromVarFile

func GetVariableAsMapFromVarFile(t testing.TestingT, fileName string, key string) map[string]string

GetVariableAsMapFromVarFile Gets the map representation of a variable from a provided input file found in VarFile Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile.

func GetVariableAsMapFromVarFileE

func GetVariableAsMapFromVarFileE(t testing.TestingT, fileName string, key string) (map[string]string, error)

GetVariableAsMapFromVarFileE Gets the map representation of a variable from a provided input file found in VarFile. Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile Returns an error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a map

func GetVariableAsStringFromVarFile

func GetVariableAsStringFromVarFile(t testing.TestingT, fileName string, key string) string

GetVariableAsStringFromVarFile Gets the string representation of a variable from a provided input file found in VarFile For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.

func GetVariableAsStringFromVarFileE

func GetVariableAsStringFromVarFileE(t testing.TestingT, fileName string, key string) (string, error)

GetVariableAsStringFromVarFileE Gets the string representation of a variable from a provided input file found in VarFile Will return an error if GetAllVariablesFromVarFileE returns an error or the key provided does not exist in the file. For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.

func HCLFileToJSONFile

func HCLFileToJSONFile(hclPath, jsonOutPath string) error

HCLFileToJSONFile is a function that takes a path containing HCL code, and converts it to JSON representation and writes out the contents to the given path.

func Init

func Init(t testing.TestingT, options *Options) string

Init calls terraform init and return stdout/stderr.

func InitAndApply

func InitAndApply(t testing.TestingT, options *Options) string

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

func InitAndApplyAndIdempotent(t testing.TestingT, options *Options) string

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

func InitAndApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)

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

func InitAndApplyE(t testing.TestingT, options *Options) (string, error)

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

func InitAndPlan(t testing.TestingT, options *Options) string

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

func InitAndPlanAndShow(t testing.TestingT, options *Options) string

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

func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error)

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

func InitAndPlanE(t testing.TestingT, options *Options) (string, error)

InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.

func InitAndPlanWithExitCode

func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int

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

func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error)

InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.

func InitAndValidate

func InitAndValidate(t testing.TestingT, options *Options) string

InitAndValidate runs terraform init and validate with the given options and returns stdout/stderr from the validate command. This will fail the test if there is an error in the command.

func InitAndValidateE

func InitAndValidateE(t testing.TestingT, options *Options) (string, error)

InitAndValidateE runs terraform init and validate with the given options and returns stdout/stderr from the validate command.

func InitAndValidateInputs

func InitAndValidateInputs(t testing.TestingT, options *Options) string

InitAndValidateInputs runs terragrunt init and validate-inputs with the given options and returns stdout/stderr from the validate command.

func InitAndValidateInputsE

func InitAndValidateInputsE(t testing.TestingT, options *Options) (string, error)

InitAndValidateInputsE runs terragrunt init and validate with the given options and rerutns stdout/stderr

func InitE

func InitE(t testing.TestingT, options *Options) (string, error)

InitE calls terraform init and return stdout/stderr.

func OPAEval

func OPAEval(
	t testing.TestingT,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
)

OPAEval runs `opa eval` with the given option on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA. This function fails the test if there is an error.

func OPAEvalE

func OPAEvalE(
	t testing.TestingT,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
) error

OPAEvalE runs `opa eval` with the given option on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA.

func Output

func Output(t testing.TestingT, options *Options, key string) string

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

func OutputAll(t testing.TestingT, options *Options) map[string]interface{}

OutputAll calls terraform output returns all values as a map. If there is error fetching the output, fails the test

func OutputAllE

func OutputAllE(t testing.TestingT, options *Options) (map[string]interface{}, error)

OutputAllE calls terraform and returns all the outputs as a map

func OutputE

func OutputE(t testing.TestingT, options *Options, key string) (string, error)

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

func OutputForKeys(t testing.TestingT, options *Options, keys []string) map[string]interface{}

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

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

func OutputJson(t testing.TestingT, options *Options, key string) string

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

func OutputJsonE(t testing.TestingT, options *Options, key string) (string, error)

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

func OutputList(t testing.TestingT, options *Options, key string) []string

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

func OutputListE(t testing.TestingT, options *Options, key string) ([]string, error)

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

func OutputListOfObjects(t testing.TestingT, options *Options, key string) []map[string]interface{}

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

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

func OutputMap(t testing.TestingT, options *Options, key string) map[string]string

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

func OutputMapE(t testing.TestingT, options *Options, key string) (map[string]string, error)

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

func OutputMapOfObjects(t testing.TestingT, options *Options, key string) map[string]interface{}

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

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

func OutputRequired(t testing.TestingT, options *Options, key string) string

OutputRequired calls terraform output for the given variable and return its value. If the value is empty, fail the test.

func OutputRequiredE

func OutputRequiredE(t testing.TestingT, options *Options, key string) (string, error)

OutputRequiredE calls terraform output for the given variable and return its value. If the value is empty, return an error.

func OutputStruct

func OutputStruct(t testing.TestingT, options *Options, key string, v interface{})

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

func OutputStructE(t testing.TestingT, options *Options, key string, v interface{}) error

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

func Plan(t testing.TestingT, options *Options) string

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

func PlanE(t testing.TestingT, options *Options) (string, error)

PlanE runs terraform plan with the given options and returns stdout/stderr.

func PlanExitCode

func PlanExitCode(t testing.TestingT, options *Options) int

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

func PlanExitCodeE(t testing.TestingT, options *Options) (int, error)

PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode.

func RequirePlannedValuesMapKeyExists

func RequirePlannedValuesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

RequirePlannedValuesMapKeyExists checks if the given key exists in the map, failing and halting the test if it does not.

func RequireResourceChangesMapKeyExists

func RequireResourceChangesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

RequireResourceChangesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func RunTerraformCommand

func RunTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) string

RunTerraformCommand runs terraform with the given arguments and options and return stdout/stderr.

func RunTerraformCommandAndGetStdoutE

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

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

func Show(t testing.TestingT, options *Options) string

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

func ShowE(t testing.TestingT, options *Options) (string, error)

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

func TgApplyAll(t testing.TestingT, options *Options) string

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

func TgApplyAllE(t testing.TestingT, options *Options) (string, error)

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

func TgDestroyAll(t testing.TestingT, options *Options) string

TgDestroyAll runs terragrunt destroy with the given options and return stdout.

func TgDestroyAllE

func TgDestroyAllE(t testing.TestingT, options *Options) (string, error)

TgDestroyAllE runs terragrunt destroy with the given options and return stdout.

func TgPlanAllExitCode

func TgPlanAllExitCode(t testing.TestingT, options *Options) int

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

func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error)

TgPlanAllExitCodeE runs terragrunt plan-all with the given options and returns the detailed exitcode.

func Validate

func Validate(t testing.TestingT, options *Options) string

Validate calls terraform validate and returns stdout/stderr.

func ValidateE

func ValidateE(t testing.TestingT, options *Options) (string, error)

ValidateE calls terraform validate and returns stdout/stderr.

func ValidateInputs

func ValidateInputs(t testing.TestingT, options *Options) string

ValidateInputs calls terragrunt validate and returns stdout/stderr.

func ValidateInputsE

func ValidateInputsE(t testing.TestingT, options *Options) (string, error)

ValidateInputsE calls terragrunt validate-inputs and returns stdout/stderr

func WorkspaceDelete

func WorkspaceDelete(t testing.TestingT, options *Options, name string) string

WorkspaceDelete removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion. If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported and only return an empty string (to avoid a fatal error).

func WorkspaceDeleteE

func WorkspaceDeleteE(t testing.TestingT, options *Options, name string) (string, error)

WorkspaceDelete removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion, and the returned error (that can be nil). If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported.

func WorkspaceSelectOrNew

func WorkspaceSelectOrNew(t testing.TestingT, options *Options, name string) string

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

func WorkspaceSelectOrNewE(t testing.TestingT, options *Options, name string) (string, error)

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 CtyJsonOutput

type CtyJsonOutput struct {
	Value map[string]interface{}
	Type  interface{}
}

When you convert a cty value to JSON, if any of that types are not yet known (i.e., are labeled as DynamicPseudoType), cty's Marshall method will write the type information to a type field and the actual value to a value field. This struct is used to capture that information so when we parse the JSON back into a Go struct, we can pull out just the Value field we need.

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 InputFileKeyNotFound

type InputFileKeyNotFound struct {
	FilePath string
	Key      string
}

InputFileKeyNotFound occurs when tfvar file does not contain a value for the key specified in the function call

func (InputFileKeyNotFound) Error

func (err InputFileKeyNotFound) 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
	Reconfigure              bool                   // Set the -reconfigure flag to the terraform init command
	MigrateState             bool                   // Set the -migrate-state and -force-copy (suppress 'yes' answer prompt) flag to the terraform init command
	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)
	PluginDir                string                 // The path of downloaded plugins to pass to the terraform init command (-plugin-dir)
}

Options for running Terraform commands

func GetCommonOptions

func GetCommonOptions(options *Options, args ...string) (*Options, []string)

GetCommonOptions extracts commons terraform options

func WithDefaultRetryableErrors

func WithDefaultRetryableErrors(t testing.TestingT, originalOptions *Options) *Options

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

func (options *Options) Clone() (*Options, error)

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

type OutputKeyNotFound string

OutputKeyNotFound occurs when terraform output does not contain a value for the key specified in the function call

func (OutputKeyNotFound) Error

func (err OutputKeyNotFound) Error() string

type OutputValueNotList

type OutputValueNotList struct {
	Value interface{}
}

OutputValueNotList occurs when casting a found output value to a list of interfaces fails

func (OutputValueNotList) Error

func (err OutputValueNotList) Error() string

type OutputValueNotMap

type OutputValueNotMap struct {
	Value interface{}
}

OutputValueNotMap occures when casting a found output value to a map fails

func (OutputValueNotMap) Error

func (err OutputValueNotMap) Error() string

type PanicWhileParsingVarFile

type PanicWhileParsingVarFile struct {
	ConfigFile     string
	RecoveredValue interface{}
}

PanicWhileParsingVarFile is returned when the HCL parsing routine panics due to errors.

func (PanicWhileParsingVarFile) Error

func (err PanicWhileParsingVarFile) Error() string

type PlanStruct

type PlanStruct struct {
	// The raw representation of the plan. See
	// https://www.terraform.io/docs/internals/json-format.html#plan-representation for details on the structure of the
	// plan output.
	RawPlan tfjson.Plan

	// A map that maps full resource addresses (e.g., module.foo.null_resource.test) to the planned values of that
	// resource.
	ResourcePlannedValuesMap map[string]*tfjson.StateResource

	// A map that maps full resource addresses (e.g., module.foo.null_resource.test) to the planned actions terraform
	// will take on that resource.
	ResourceChangesMap map[string]*tfjson.ResourceChange
}

PlanStruct is a Go Struct representation of the plan object returned from Terraform (after running `terraform show`). Unlike the raw plan representation returned by terraform-json, this struct provides a map that maps the resource addresses to the changes and planned values to make it easier to navigate the raw plan struct.

func InitAndPlanAndShowWithStruct

func InitAndPlanAndShowWithStruct(t testing.TestingT, options *Options) *PlanStruct

InitAndPlanAndShowWithStruct runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct. This will fail the test if there is an error in the command.

func InitAndPlanAndShowWithStructE

func InitAndPlanAndShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error)

InitAndPlanAndShowWithStructE runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct.

func InitAndPlanAndShowWithStructNoLogTempPlanFile

func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options *Options) *PlanStruct

InitAndPlanAndShowWithStructNoLog runs InitAndPlanAndShowWithStruct without logging and also by allocating a temporary plan file destination that is discarded before returning the struct.

type ResourceCount

type ResourceCount struct {
	Add     int
	Change  int
	Destroy int
}

ResourceCount represents counts of resources affected by terraform apply/plan/destroy command.

func GetResourceCount

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

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

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

func (err TgInvalidBinary) Error() string

type UnexpectedOutputType

type UnexpectedOutputType struct {
	Key          string
	ExpectedType string
	ActualType   string
}

UnexpectedOutputType is an error that occurs when the output is not of the type we expect

func (UnexpectedOutputType) Error

func (err UnexpectedOutputType) Error() string

type UnsupportedDefaultWorkspaceDeletion

type UnsupportedDefaultWorkspaceDeletion struct{}

UnsupportedDefaultWorkspaceDeletion is returned when user tries to delete the workspace "default"

func (*UnsupportedDefaultWorkspaceDeletion) Error

type VarFileNotFound

type VarFileNotFound struct {
	Path string
}

VarFileNotFound is an error that occurs when a var file cannot be found in an option's VarFile list

func (VarFileNotFound) Error

func (err VarFileNotFound) Error() string

type WorkspaceDoesNotExist

type WorkspaceDoesNotExist string

WorkspaceDoesNotExist is returned when user tries to delete a workspace which does not exist

func (WorkspaceDoesNotExist) Error

func (err WorkspaceDoesNotExist) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL