Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit 091e6ae

Browse files
committed
Move providers into providers.tf
This allows us to manually override the file at test time
1 parent 94810b2 commit 091e6ae

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
lines changed

examples/couchbase-multi-datacenter-replication/main.tf

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
# This is an example of how to deploy two Couchbase clusters in AWS with replication between them.
44
# ---------------------------------------------------------------------------------------------------------------------
55

6-
provider "aws" {
7-
alias = "primary"
8-
9-
# Region intentionally ommitted so this example will prompt the user for a region when run via Terraform Registry
10-
# instructions
11-
}
12-
13-
provider "aws" {
14-
alias = "replica"
15-
16-
# Region intentionally ommitted so this example will prompt the user for a region when run via Terraform Registry
17-
# instructions
18-
}
19-
206
terraform {
217
required_version = ">= 0.10.3"
228
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# CONFIGURE THE PRIMARY AND REPLICA PROVIDERS FOR THIS EXAMPLE
3+
# Note that we do this in a separate file so the automated tests can override it and set custom regions in these
4+
# providers. Ideally, we'd use Terraform file overrides instead, but those do not properly override provider aliases
5+
# in Terraform 0.11. This may be fixed in Terraform 0.12.
6+
# ---------------------------------------------------------------------------------------------------------------------
7+
8+
provider "aws" {
9+
alias = "primary"
10+
11+
# Region intentionally ommitted so this example will prompt the user for a region when run via Terraform Registry
12+
# instructions
13+
}
14+
15+
provider "aws" {
16+
alias = "replica"
17+
18+
# Region intentionally ommitted so this example will prompt the user for a region when run via Terraform Registry
19+
# instructions
20+
}

examples/couchbase-multi-datacenter-replication/variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
# ---------------------------------------------------------------------------------------------------------------------
1818

1919
variable "ami_id_primary" {
20-
description = "The ID of the AMI to run in the cluster in var.aws_region_primary. This should be an AMI built from the Packer template under examples/couchbase-ami/couchbase.json. Leave blank to use one of the example AMIs we have published publicly."
20+
description = "The ID of the AMI to run in the primary cluster. This should be an AMI built from the Packer template under examples/couchbase-ami/couchbase.json. Leave blank to use one of the example AMIs we have published publicly."
2121
default = ""
2222
}
2323

2424
variable "ami_id_replica" {
25-
description = "The ID of the AMI to run in the cluster in var.aws_region_replica. This should be an AMI built from the Packer template under examples/couchbase-ami/couchbase.json. Leave blank to use one of the example AMIs we have published publicly."
25+
description = "The ID of the AMI to run in the replica cluster. This should be an AMI built from the Packer template under examples/couchbase-ami/couchbase.json. Leave blank to use one of the example AMIs we have published publicly."
2626
default = ""
2727
}
2828

@@ -37,12 +37,12 @@ variable "cluster_name_replica" {
3737
}
3838

3939
variable "ssh_key_name_primary" {
40-
description = "The name of an EC2 Key Pair that can be used to SSH to the EC2 Instances in the primary Couchbase cluster. Must be a Key Pair in var.aws_region_primary. Set to an empty string to not associate a Key Pair."
40+
description = "The name of an EC2 Key Pair that can be used to SSH to the EC2 Instances in the primary Couchbase cluster. Must be a Key Pair in the same region as the primary cluster. Set to an empty string to not associate a Key Pair."
4141
default = ""
4242
}
4343

4444
variable "ssh_key_name_replica" {
45-
description = "The name of an EC2 Key Pair that can be used to SSH to the EC2 Instances in the replica Couchbase cluster. Must be a Key Pair in var.aws_region_replica. Set to an empty string to not associate a Key Pair."
45+
description = "The name of an EC2 Key Pair that can be used to SSH to the EC2 Instances in the replica Couchbase cluster. Must be a Key Pair in the same region as the replica cluster. Set to an empty string to not associate a Key Pair."
4646
default = ""
4747
}
4848

test/couchbase_multi_datacenter_replication_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/gruntwork-io/terratest/modules/random"
1010
"github.com/gruntwork-io/terratest/modules/terraform"
1111
"github.com/gruntwork-io/terratest/modules/aws"
12+
"github.com/gruntwork-io/terratest/modules/files"
13+
"os"
14+
"github.com/gruntwork-io/terratest/modules/logger"
1215
)
1316

1417
const clusterNamePrimaryVarName = "cluster_name_primary"
@@ -23,6 +26,9 @@ const savedUniqueIdPrimary = "UniqueIdPrimary"
2326
const savedAwsRegionReplica = "AwsRegionReplica"
2427
const savedUniqueIdReplica = "UniqueIdReplica"
2528

29+
const providersFile = "providers.tf"
30+
const providersFileBackup = "providers.tf.bak"
31+
2632
func TestIntegrationCouchbaseEnterpriseMultiDataCenterReplicationUbuntu(t *testing.T) {
2733
t.Parallel()
2834
testCouchbaseMultiDataCenterReplication(t, "ubuntu", "enterprise")
@@ -90,6 +96,8 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
9096
terraformOptions := test_structure.LoadTerraformOptions(t, couchbaseMultiClusterDir)
9197
terraform.Destroy(t, terraformOptions)
9298

99+
restoreProvider(t, couchbaseMultiClusterDir)
100+
93101
amiIdPrimary := test_structure.LoadString(t, couchbaseMultiClusterDir, savedAmiIdPrimary)
94102
amiIdReplica := test_structure.LoadString(t, couchbaseMultiClusterDir, savedAmiIdReplica)
95103

@@ -119,6 +127,8 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
119127
uniqueIdPrimary := test_structure.LoadString(t, couchbaseMultiClusterDir, savedUniqueIdPrimary)
120128
uniqueIdReplica := test_structure.LoadString(t, couchbaseMultiClusterDir, savedUniqueIdReplica)
121129

130+
overrideProvider(t, couchbaseMultiClusterDir, awsRegionPrimary, awsRegionReplica)
131+
122132
terraformOptions := &terraform.Options{
123133
TerraformDir: couchbaseMultiClusterDir,
124134
Vars: map[string]interface{} {
@@ -152,3 +162,54 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
152162
})
153163
}
154164

165+
const providerOverrideTemplate = `
166+
# This file temporarily overrides the providers at test time. The original providers file should be restored at the
167+
# end of the test!
168+
169+
provider "aws" {
170+
alias = "primary"
171+
region = "%s"
172+
}
173+
174+
provider "aws" {
175+
alias = "replica"
176+
region = "%s"
177+
}
178+
`
179+
180+
// In order for the examples to work well with the Terraform Registry, where they are wrapped in a module, we cannot
181+
// define the AWS regions in those providers. This works OK for manual usage, where the user can specify the region
182+
// interactively, but not at test time. Therefore, as a workaround, we override the providers.tf file at test time
183+
// with the regions fully defined, and then put it back at the end of the test in the restoreProvider function.
184+
func overrideProvider(t *testing.T, couchbaseMultiClusterDir string, awsRegionPrimary string, awsRegionReplica string) {
185+
providersFilePath := filepath.Join(couchbaseMultiClusterDir, providersFile)
186+
providersFileBackupPath := filepath.Join(couchbaseMultiClusterDir, providersFileBackup)
187+
188+
logger.Logf(t, "Backing up %s to %s", providersFilePath, providersFileBackupPath)
189+
if err := files.CopyFile(providersFilePath, providersFileBackupPath); err != nil {
190+
t.Fatal(err)
191+
}
192+
193+
newProvidersFileContents := fmt.Sprintf(providerOverrideTemplate, awsRegionPrimary, awsRegionReplica)
194+
195+
logger.Logf(t, "Creating override proviers file at %s with contents:\n%s", providersFilePath, newProvidersFileContents)
196+
if err := files.WriteFileWithSamePermissions(providersFilePath, providersFilePath, []byte(newProvidersFileContents)); err != nil {
197+
t.Fatal(err)
198+
}
199+
}
200+
201+
// See the overrideProvider method for details
202+
func restoreProvider(t *testing.T, couchbaseMultiClusterDir string) {
203+
providersFilePath := filepath.Join(couchbaseMultiClusterDir, providersFile)
204+
providersFileBackupPath := filepath.Join(couchbaseMultiClusterDir, providersFileBackup)
205+
206+
logger.Logf(t, "Restoring %s from %s", providersFilePath, providersFileBackupPath)
207+
if err := files.CopyFile(providersFileBackupPath, providersFilePath); err != nil {
208+
t.Fatal(err)
209+
}
210+
211+
logger.Logf(t, "Deleting %s", providersFileBackupPath)
212+
if err := os.Remove(providersFileBackupPath); err != nil {
213+
t.Fatal(err)
214+
}
215+
}

0 commit comments

Comments
 (0)