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

Commit c951de1

Browse files
authored
Merge pull request #24 from gruntwork-io/xdcr-fix
Update xdcr example to work better with Terraform Registry
2 parents 028f695 + 091e6ae commit c951de1

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +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-
region = "${var.aws_region_primary}"
9-
}
10-
11-
provider "aws" {
12-
alias = "replica"
13-
region = "${var.aws_region_replica}"
14-
}
15-
166
terraform {
177
required_version = ">= 0.10.3"
188
}
@@ -110,7 +100,7 @@ data "template_file" "user_data_primary" {
110100
cluster_port = "${module.couchbase_security_group_rules_primary.rest_port}"
111101

112102
replication_dest_cluster_name = "${var.cluster_name_replica}"
113-
replication_dest_cluster_aws_region = "${var.aws_region_replica}"
103+
replication_dest_cluster_aws_region = "${data.aws_region.replica.name}"
114104
}
115105
}
116106

@@ -433,3 +423,7 @@ data "aws_subnet_ids" "default_replica" {
433423

434424
provider = "aws.replica"
435425
}
426+
427+
data "aws_region" "replica" {
428+
provider = "aws.replica"
429+
}
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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,32 @@
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

29-
variable "aws_region_primary" {
30-
description = "The AWS region to deploy the primary Couchbase cluster into (e.g. us-east-1)."
31-
default = "us-east-1"
32-
}
33-
3429
variable "cluster_name_primary" {
3530
description = "What to name the primary Couchbase cluster and all of its associated resources"
3631
default = "couchbase-server-primary"
3732
}
3833

39-
variable "aws_region_replica" {
40-
description = "The AWS region to deploy the replica Couchbase cluster into (e.g. us-east-1)."
41-
default = "us-west-1"
42-
}
43-
4434
variable "cluster_name_replica" {
4535
description = "What to name the replica Couchbase cluster and all of its associated resources"
4636
default = "couchbase-server-replica"
4737
}
4838

4939
variable "ssh_key_name_primary" {
50-
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."
5141
default = ""
5242
}
5343

5444
variable "ssh_key_name_replica" {
55-
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."
5646
default = ""
5747
}
5848

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)