Skip to content

Commit

Permalink
Suppress diffs on forced Redis version upgrades (#873)
Browse files Browse the repository at this point in the history
* Suppress diffs on forced Redis version upgrades

* Update test logic to handle any version of Redis.

Includes required update to godo dependency.

* Small refactor
  • Loading branch information
scotchneat committed Aug 31, 2022
1 parent 1dd07d2 commit 1a2eb03
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 12 deletions.
10 changes: 7 additions & 3 deletions digitalocean/resource_digitalocean_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -54,10 +55,13 @@ 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.
// When Redis clusters are forced to upgrade, this prevents attempting
// to recreate clusters specifying the previous version in their config.
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("engine") == redisDBEngineSlug && old == "6" && new == "5"
remoteVersion, _ := strconv.Atoi(old)
configVersion, _ := strconv.Atoi(new)

return d.Get("engine") == redisDBEngineSlug && remoteVersion > configVersion
},
},

Expand Down
30 changes: 24 additions & 6 deletions digitalocean/resource_digitalocean_database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,19 @@ 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.
// DigitalOcean only supports one version of Redis. For backwards compatibility
// the API allows for POST requests that specifies a previous version, but new
// clusters are created with the latest/only supported version, regardless of
// the version specified in the config.
// The provider suppresses diffs when the config version is <= to the latest
// version. New clusters is always created with the latest version .
func TestAccDigitalOceanDatabaseCluster_oldRedisVersion(t *testing.T) {
var database godo.Database
var (
database godo.Database
client = &godo.Client{}
redisVersion string
)

databaseName := randomTestName()

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -277,14 +284,25 @@ func TestAccDigitalOceanDatabaseCluster_oldRedisVersion(t *testing.T) {
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseClusterRedis, databaseName, "5"),
Check: resource.ComposeTestCheckFunc(
// Fetch the Databases Options and get the current supported version of
// Redis from the response.
func(*terraform.State) error {
client = testAccProvider.Meta().(*CombinedConfig).godoClient()
options, _, err := client.Databases.ListOptions(context.Background())
if err != nil {
t.Error("Error fetching database options")
}
redisVersion = options.RedisOptions.Versions[0]
return nil
},
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"),
"digitalocean_database_cluster.foobar", "version", redisVersion),
),
},
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/digitalocean/terraform-provider-digitalocean

require (
github.com/aws/aws-sdk-go v1.42.18
github.com/digitalocean/godo v1.82.0
github.com/digitalocean/godo v1.83.0
github.com/hashicorp/awspolicyequivalence v1.5.0
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.82.0 h1:lqAit46H1CqJGjh7LDbsamng/UMBME5rvmfH3Vb5Yy8=
github.com/digitalocean/godo v1.82.0/go.mod h1:BPCqvwbjbGqxuUnIKB4EvS/AX7IDnNmt5fwvIkWo+ew=
github.com/digitalocean/godo v1.83.0 h1:K9CveJyECNLwrQnGZG+ovgapr7l5OuvQ6xZSKKW9Nz0=
github.com/digitalocean/godo v1.83.0/go.mod h1:BPCqvwbjbGqxuUnIKB4EvS/AX7IDnNmt5fwvIkWo+ew=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
4 changes: 4 additions & 0 deletions vendor/github.com/digitalocean/godo/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/digitalocean/godo/databases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/digitalocean/godo/godo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ github.com/aws/aws-sdk-go/service/sts/stsiface
# github.com/davecgh/go-spew v1.1.1
## explicit
github.com/davecgh/go-spew/spew
# github.com/digitalocean/godo v1.82.0
# github.com/digitalocean/godo v1.83.0
## explicit; go 1.18
github.com/digitalocean/godo
github.com/digitalocean/godo/metrics
Expand Down

0 comments on commit 1a2eb03

Please sign in to comment.