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

Commit 894dabc

Browse files
committed
Try to switch to setting regions via vars
1 parent 74eeb67 commit 894dabc

File tree

4 files changed

+32
-88
lines changed

4 files changed

+32
-88
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ terraform {
1212
required_version = ">= 0.12"
1313
}
1414

15+
provider "aws" {
16+
alias = "primary"
17+
region = var.primary_region
18+
}
19+
20+
provider "aws" {
21+
alias = "replica"
22+
region = var.replica_region
23+
}
24+
1525
# ---------------------------------------------------------------------------------------------------------------------
1626
# DEPLOY THE PRIMARY COUCHBASE CLUSTER
1727
# ---------------------------------------------------------------------------------------------------------------------

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
# You must provide a value for each of these parameters.
1212
# ---------------------------------------------------------------------------------------------------------------------
1313

14+
variable "primary_region" {
15+
description = "The region to deploy the primary to"
16+
type = string
17+
}
18+
19+
variable "replica_region" {
20+
description = "The region to deploy the replica to"
21+
type = string
22+
}
23+
1424
# ---------------------------------------------------------------------------------------------------------------------
1525
# OPTIONAL PARAMETERS
1626
# These parameters have reasonable defaults.

test/couchbase_multi_datacenter_replication_test.go

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package test
22

33
import (
4-
"testing"
5-
"path/filepath"
64
"fmt"
7-
"sync"
8-
"github.com/gruntwork-io/terratest/modules/test-structure"
5+
"github.com/gruntwork-io/terratest/modules/aws"
96
"github.com/gruntwork-io/terratest/modules/random"
107
"github.com/gruntwork-io/terratest/modules/terraform"
11-
"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"
8+
"github.com/gruntwork-io/terratest/modules/test-structure"
9+
"path/filepath"
10+
"sync"
11+
"testing"
1512
)
1613

1714
const clusterNamePrimaryVarName = "cluster_name_primary"
@@ -105,8 +102,6 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
105102
terraformOptions := test_structure.LoadTerraformOptions(t, couchbaseMultiClusterDir)
106103
terraform.Destroy(t, terraformOptions)
107104

108-
restoreProvider(t, couchbaseMultiClusterDir)
109-
110105
amiIdPrimary := test_structure.LoadString(t, couchbaseMultiClusterDir, savedAmiIdPrimary)
111106
amiIdReplica := test_structure.LoadString(t, couchbaseMultiClusterDir, savedAmiIdReplica)
112107

@@ -136,15 +131,15 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
136131
uniqueIdPrimary := test_structure.LoadString(t, couchbaseMultiClusterDir, savedUniqueIdPrimary)
137132
uniqueIdReplica := test_structure.LoadString(t, couchbaseMultiClusterDir, savedUniqueIdReplica)
138133

139-
overrideProvider(t, couchbaseMultiClusterDir, awsRegionPrimary, awsRegionReplica)
140-
141134
terraformOptions := &terraform.Options{
142135
TerraformDir: couchbaseMultiClusterDir,
143-
Vars: map[string]interface{} {
144-
"ami_id_primary": amiIdPrimary,
145-
"ami_id_replica": amiIdReplica,
146-
clusterNamePrimaryVarName: formatCouchbaseClusterName("primary", uniqueIdPrimary),
147-
clusterNameReplicaVarName: formatCouchbaseClusterName("replica", uniqueIdReplica),
136+
Vars: map[string]interface{}{
137+
"primary_region": awsRegionPrimary,
138+
"replica_region": awsRegionReplica,
139+
"ami_id_primary": amiIdPrimary,
140+
"ami_id_replica": amiIdReplica,
141+
clusterNamePrimaryVarName: formatCouchbaseClusterName("primary", uniqueIdPrimary),
142+
clusterNameReplicaVarName: formatCouchbaseClusterName("replica", uniqueIdReplica),
148143
},
149144
}
150145

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

0 commit comments

Comments
 (0)