Skip to content

Commit

Permalink
database_cluster: Handle Redis version change with DiffSuppressFunc. (#…
Browse files Browse the repository at this point in the history
…534)

* database_cluster: Handle Redis version change with DiffSuppressFunc.

* Update example in docs.
  • Loading branch information
andrewsomething authored Dec 3, 2020
1 parent 3fc7257 commit 195af31
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions digitalocean/resource_digitalocean_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func resourceDigitalOceanDatabaseCluster() *schema.Resource {
// Required: true,
Optional: true,
ForceNew: true,
// Redis clusters are being force upgraded from version 5 to 6.
// Prevent attempting to recreate clusters specifying 5 in their config.
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("engine") == "redis" && old == "6" && new == "5"
},
},

"size": {
Expand Down
34 changes: 32 additions & 2 deletions digitalocean/resource_digitalocean_database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ func TestAccDigitalOceanDatabaseCluster_RedisNoVersion(t *testing.T) {
})
}

// For backwards compatibility the API allows for POST requests that specify "5"
// for the version, but a Redis 6 cluster is actually created. The response body
// specifies "6" for the version. This should be handled without Terraform
// attempting to recreate the cluster.
func TestAccDigitalOceanDatabaseCluster_oldRedisVersion(t *testing.T) {
var database godo.Database
databaseName := randomTestName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanDatabaseClusterDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseClusterRedis, databaseName, "5"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDatabaseClusterExists("digitalocean_database_cluster.foobar", &database),
testAccCheckDigitalOceanDatabaseClusterAttributes(&database, databaseName),
resource.TestCheckResourceAttr(
"digitalocean_database_cluster.foobar", "name", databaseName),
resource.TestCheckResourceAttr(
"digitalocean_database_cluster.foobar", "engine", "redis"),
resource.TestCheckResourceAttr(
"digitalocean_database_cluster.foobar", "version", "6"),
),
},
},
})
}

func TestAccDigitalOceanDatabaseCluster_RedisWithEvictionPolicy(t *testing.T) {
var database godo.Database
databaseName := randomTestName()
Expand Down Expand Up @@ -279,7 +309,7 @@ func TestAccDigitalOceanDatabaseCluster_RedisWithEvictionPolicy(t *testing.T) {
},
// Remove eviction policy
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseClusterRedis, databaseName),
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseClusterRedis, databaseName, "6"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDatabaseClusterExists("digitalocean_database_cluster.foobar", &database),
testAccCheckDigitalOceanDatabaseClusterAttributes(&database, databaseName),
Expand Down Expand Up @@ -514,7 +544,7 @@ const testAccCheckDigitalOceanDatabaseClusterRedis = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "redis"
version = "5"
version = "%s"
size = "db-s-1vcpu-1gb"
region = "nyc1"
node_count = 1
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/database_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ resource "digitalocean_database_cluster" "mysql-example" {
resource "digitalocean_database_cluster" "redis-example" {
name = "example-redis-cluster"
engine = "redis"
version = "5"
version = "6"
size = "db-s-1vcpu-1gb"
region = "nyc1"
node_count = 1
Expand Down

0 comments on commit 195af31

Please sign in to comment.