Skip to content

Commit 534fa72

Browse files
committed
Merge pull request #5 from gruntwork-io/rename-apply-options
Rename apply_options to terratest_options
2 parents f6fb8f4 + 5400b7f commit 534fa72

File tree

7 files changed

+64
-63
lines changed

7 files changed

+64
-63
lines changed

apply.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ import (
1010

1111
// Apply handles all setup required for a Terraform Apply operation but does not perform a destroy operation or do any cleanup.
1212
// The caller of this function is expected to call Destroy to clean up the Terraform template when done.
13-
func Apply(ao *ApplyOptions) (string, error) {
14-
logger := log.NewLogger(ao.TestName)
13+
func Apply(options *TerratestOptions) (string, error) {
14+
logger := log.NewLogger(options.TestName)
1515
var output string
1616

1717
// SETUP
1818
// Configure terraform to use Remote State.
19-
err := aws.AssertS3BucketExists(ao.TfRemoteStateS3BucketRegion, ao.TfRemoteStateS3BucketName)
19+
err := aws.AssertS3BucketExists(options.TfRemoteStateS3BucketRegion, options.TfRemoteStateS3BucketName)
2020
if err != nil {
21-
return output, fmt.Errorf("Test failed because the S3 Bucket '%s' does not exist in the '%s' region.\n", ao.TfRemoteStateS3BucketName, ao.TfRemoteStateS3BucketRegion)
21+
return output, fmt.Errorf("Test failed because the S3 Bucket '%s' does not exist in the '%s' region.\n", options.TfRemoteStateS3BucketName, options.TfRemoteStateS3BucketRegion)
2222
}
2323

24-
terraform.ConfigureRemoteState(ao.TemplatePath, ao.TfRemoteStateS3BucketName, ao.getTfStateFileName(), ao.TfRemoteStateS3BucketRegion, logger)
24+
terraform.ConfigureRemoteState(options.TemplatePath, options.TfRemoteStateS3BucketName, options.getTfStateFileName(), options.TfRemoteStateS3BucketRegion, logger)
2525

2626
// TERRAFORM APPLY
2727
// Download all the Terraform modules
2828
logger.Println("Running terraform get...")
29-
err = terraform.Get(ao.TemplatePath, logger)
29+
err = terraform.Get(options.TemplatePath, logger)
3030
if err != nil {
3131
return output, fmt.Errorf("Failed to call terraform get successfully: %s\n", err.Error())
3232
}
3333

3434
// Apply the Terraform template
3535
logger.Println("Running terraform apply...")
36-
if len(ao.RetryableTerraformErrors) > 0 {
37-
output, err = terraform.ApplyAndGetOutputWithRetry(ao.TemplatePath, ao.Vars, ao.RetryableTerraformErrors, logger)
36+
if len(options.RetryableTerraformErrors) > 0 {
37+
output, err = terraform.ApplyAndGetOutputWithRetry(options.TemplatePath, options.Vars, options.RetryableTerraformErrors, logger)
3838
} else {
39-
output, err = terraform.ApplyAndGetOutput(ao.TemplatePath, ao.Vars, logger)
39+
output, err = terraform.ApplyAndGetOutput(options.TemplatePath, options.Vars, logger)
4040
}
4141
if err != nil {
4242
return output, fmt.Errorf("Failed to terraform apply: %s\n", err.Error())

apply_and_destroy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package terratest
22

33
// ApplyAndDestroy wraps all setup and teardown required for a Terraform Apply operation. It returns the output of the terraform operations.
4-
func ApplyAndDestroy(ao *ApplyOptions) (string, error) {
5-
defer destroyHelper(ao, ao.getTfStateFileName())
6-
return Apply(ao)
4+
func ApplyAndDestroy(options *TerratestOptions) (string, error) {
5+
defer destroyHelper(options, options.getTfStateFileName())
6+
return Apply(options)
77
}

aws/region.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111

1212
func GetGloballyForbiddenRegions() []string {
1313
return []string{
14-
"us-west-2",
14+
"us-west-2", // Josh is using this region for his personal projects
15+
"ap-northeast-2", // This region seems to be running out of t2.micro instances with gp2 volumes
1516
}
1617
}
1718

destroy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
)
99

1010
// Destroy both destroys all the given elements of the RandomResourceCollection and calls terraform destroy.
11-
func Destroy(ao *ApplyOptions, rand *RandomResourceCollection) (string, error) {
12-
logger := log.NewLogger(ao.TestName)
11+
func Destroy(options *TerratestOptions, rand *RandomResourceCollection) (string, error) {
12+
logger := log.NewLogger(options.TestName)
1313

1414
err := rand.DestroyResources()
1515
if err != nil {
1616
return "", fmt.Errorf("Failed to destroy random resource collection: %s", err.Error())
1717
}
1818

1919
logger.Println("Running terraform destroy...")
20-
output, err := destroyHelper(ao, ao.getTfStateFileName())
20+
output, err := destroyHelper(options, options.getTfStateFileName())
2121
if err != nil {
2222
return output, fmt.Errorf("Failed to terraform destroy: %s", err.Error())
2323
}
@@ -26,9 +26,9 @@ func Destroy(ao *ApplyOptions, rand *RandomResourceCollection) (string, error) {
2626
}
2727

2828
// Helper function that calls terraform destroy
29-
func destroyHelper(ao *ApplyOptions, remoteStateS3ObjectName string) (string, error) {
30-
logger := log.NewLogger(ao.TestName)
31-
output, err := terraform.DestroyAndGetOutput(ao.TemplatePath, ao.Vars, logger)
29+
func destroyHelper(options *TerratestOptions, remoteStateS3ObjectName string) (string, error) {
30+
logger := log.NewLogger(options.TestName)
31+
output, err := terraform.DestroyAndGetOutput(options.TemplatePath, options.Vars, logger)
3232
if err != nil {
3333
logger.Printf(`Failed to terraform destroy.
3434
** WARNING ** Terraform destroy has failed which means you must manually delete any resources created by the "terraform apply" run.
@@ -37,7 +37,7 @@ Terraform Template Path: %s
3737
AWS Region: <scroll up to see it>
3838
Remote State Location: s3://%s/%s
3939
Official Error Message: %s
40-
`, ao.TemplatePath, ao.TestName, ao.TfRemoteStateS3BucketName, remoteStateS3ObjectName, err.Error())
40+
`, options.TemplatePath, options.TestName, options.TfRemoteStateS3BucketName, remoteStateS3ObjectName, err.Error())
4141
return output, err
4242
}
4343

output.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/gruntwork-io/terratest/log"
66
)
77

8-
func Output(ao *ApplyOptions, key string) (string, error) {
9-
logger := log.NewLogger(ao.TestName)
10-
return terraform.Output(ao.TemplatePath, key, logger)
8+
func Output(options *TerratestOptions, key string) (string, error) {
9+
logger := log.NewLogger(options.TestName)
10+
return terraform.Output(options.TemplatePath, key, logger)
1111
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package terratest
22

33
// The options to be passed into any terratest.Apply or Destroy function
4-
type ApplyOptions struct {
4+
type TerratestOptions struct {
55
UniqueId string // A unique identifier for this terraform run.
66
TestName string // the name of the test to run, for logging purposes.
77
TemplatePath string // the relative or absolute path to the terraform template to be applied.
@@ -12,15 +12,15 @@ type ApplyOptions struct {
1212
}
1313

1414
// Initialize an ApplyOptions struct with default values
15-
func NewApplyOptions() *ApplyOptions {
16-
return &ApplyOptions{
15+
func NewTerratestOptions() *TerratestOptions {
16+
return &TerratestOptions{
1717
TfRemoteStateS3BucketName: defaultTfRemoteStateS3BucketName,
1818
TfRemoteStateS3BucketRegion: defaultTfRemoteStateS3BuckeRegion,
1919
}
2020
}
2121

22-
// generateTfStateFileName creates a path and filename used to reference a terraform tfstate file. E.g. this is
22+
// getTfStateFileName creates a path and filename used to reference a terraform tfstate file. E.g. this is
2323
// useful with S3 for deciding where the tfstate file should be within a given bucket.
24-
func (ao *ApplyOptions) getTfStateFileName() string {
25-
return ao.UniqueId + "/terraform.tfstate"
24+
func (options *TerratestOptions) getTfStateFileName() string {
25+
return options.UniqueId + "/terraform.tfstate"
2626
}

terratest_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ func TestTerraformApplyOnMinimalExample(t *testing.T) {
4949
vars["ec2_instance_name"] = rand.UniqueId
5050
vars["ec2_image"] = rand.AmiId
5151

52-
ao := NewApplyOptions()
53-
ao.UniqueId = rand.UniqueId
54-
ao.TestName = "Test - TestTerraformApplyOnMinimalExample"
55-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example")
56-
ao.Vars = vars
52+
options := NewTerratestOptions()
53+
options.UniqueId = rand.UniqueId
54+
options.TestName = "Test - TestTerraformApplyOnMinimalExample"
55+
options.TemplatePath = path.Join(fixtureDir, "minimal-example")
56+
options.Vars = vars
5757

58-
_, err = ApplyAndDestroy(ao)
58+
_, err = ApplyAndDestroy(options)
5959
if err != nil {
6060
t.Fatalf("Failed to ApplyAndDestroy: %s", err.Error())
6161
}
@@ -77,13 +77,13 @@ func TestApplyOrDestroyFailsOnTerraformError(t *testing.T) {
7777
vars["ec2_instance_name"] = rand.UniqueId
7878
vars["ec2_image"] = rand.AmiId
7979

80-
ao := NewApplyOptions()
81-
ao.UniqueId = rand.UniqueId
82-
ao.TestName = "Test - TestApplyOrDestroyFailsOnTerraformError"
83-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
84-
ao.Vars = vars
80+
options := NewTerratestOptions()
81+
options.UniqueId = rand.UniqueId
82+
options.TestName = "Test - TestApplyOrDestroyFailsOnTerraformError"
83+
options.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
84+
options.Vars = vars
8585

86-
_, err = ApplyAndDestroy(ao)
86+
_, err = ApplyAndDestroy(options)
8787
if err != nil {
8888
fmt.Printf("Received expected failure message: %s. Continuing on...", err.Error())
8989
} else {
@@ -109,15 +109,15 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages(t *testing.T)
109109
vars["ec2_instance_name"] = rand.UniqueId
110110
vars["ec2_image"] = rand.AmiId
111111

112-
ao := NewApplyOptions()
113-
ao.UniqueId = rand.UniqueId
114-
ao.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages"
115-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
116-
ao.Vars = vars
117-
ao.RetryableTerraformErrors = make(map[string]string)
118-
ao.RetryableTerraformErrors["aws_instance.demo: Error launching source instance: InvalidKeyPair.NotFound"] = "This error was deliberately added to the template."
112+
options := NewTerratestOptions()
113+
options.UniqueId = rand.UniqueId
114+
options.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages"
115+
options.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
116+
options.Vars = vars
117+
options.RetryableTerraformErrors = make(map[string]string)
118+
options.RetryableTerraformErrors["aws_instance.demo: Error launching source instance: InvalidKeyPair.NotFound"] = "This error was deliberately added to the template."
119119

120-
output, err := ApplyAndDestroy(ao)
120+
output, err := ApplyAndDestroy(options)
121121
if err != nil {
122122
if strings.Contains(output, "**TERRAFORM-RETRY**") {
123123
fmt.Println("Expected error was caught and a retry was attempted.")
@@ -147,15 +147,15 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry(t
147147
vars["ec2_instance_name"] = rand.UniqueId
148148
vars["ec2_image"] = rand.AmiId
149149

150-
ao := NewApplyOptions()
151-
ao.UniqueId = rand.UniqueId
152-
ao.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry"
153-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
154-
ao.Vars = vars
155-
ao.RetryableTerraformErrors = make(map[string]string)
156-
ao.RetryableTerraformErrors["I'm a message that shouldn't show up in the output"] = ""
150+
options := NewTerratestOptions()
151+
options.UniqueId = rand.UniqueId
152+
options.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry"
153+
options.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
154+
options.Vars = vars
155+
options.RetryableTerraformErrors = make(map[string]string)
156+
options.RetryableTerraformErrors["I'm a message that shouldn't show up in the output"] = ""
157157

158-
output, err := ApplyAndDestroy(ao)
158+
output, err := ApplyAndDestroy(options)
159159
if err != nil {
160160
if strings.Contains(output, "**TERRAFORM-RETRY**") {
161161
t.Fatalf("Expected no terraform retry but instead a retry was attempted.")
@@ -196,13 +196,13 @@ func TestTerraformApplyAvoidsForbiddenRegion(t *testing.T) {
196196
vars["ec2_instance_name"] = rand.UniqueId
197197
vars["ec2_image"] = rand.AmiId
198198

199-
ao := NewApplyOptions()
200-
ao.UniqueId = rand.UniqueId
201-
ao.TestName = "Test - TestTerraformApplyAvoidsForbiddenRegion"
202-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example")
203-
ao.Vars = vars
199+
options := NewTerratestOptions()
200+
options.UniqueId = rand.UniqueId
201+
options.TestName = "Test - TestTerraformApplyAvoidsForbiddenRegion"
202+
options.TemplatePath = path.Join(fixtureDir, "minimal-example")
203+
options.Vars = vars
204204

205-
_, err = ApplyAndDestroy(ao)
205+
_, err = ApplyAndDestroy(options)
206206
if err != nil {
207207
t.Fatalf("Failed to ApplyAndDestroy: %s", err.Error())
208208
}

0 commit comments

Comments
 (0)